The Playwright Agent CLI.
A command line for browser automation built for coding agents. @playwright/cli gives token-efficient commands, ref-based accessibility snapshots, a persistent browser daemon, installable skills, and isolated sessions - so an AI agent can drive a real browser without burning its context window. This session teaches it end to end, practised on the live TTACart demo app.
Foundations
What is the Agent CLI?
The Playwright Agent CLI is a separate tool from the classic npx playwright test runner. The test runner is for humans authoring specs; the Agent CLI is for an AI agent driving a browser one command at a time, with output kept small so it fits in a limited context window.
Concise output
Commands return compact results, and capabilities are discovered through installable skills instead of long help text - so the agent spends tokens on reasoning, not noise.
Persistent browser
A background browser process stays alive across commands, so there is no per-command startup cost.
Deterministic targets
An accessibility snapshot gives each element a stable ref (like e21). The agent acts on the ref, not on brittle pixels.
Isolated state
Multiple named sessions run side by side, each with its own cookies, storage, and tabs.
App under test
We practise on TTACart
Every command targets the same live demo store - login, inventory, cart, and a multi-step checkout - so you can paste and watch the agent CLI act on a real page.
TTACart store
Open it, snapshot it, and drive the login to checkout flow from the terminal.
Login → cart → checkout
Use the credentials listed on the TTACart login screen - never hard-code secrets into shared scripts.
Stable refs
Clear roles and test ids mean the accessibility snapshot returns clean, reusable element refs.
Getting started
Install and verify
# install globally (provides the playwright-cli binary) npm install -g @playwright/cli # print the resolved configuration playwright-cli config-print # install on-demand skills the agent can use playwright-cli install --skills
Core concept
Ref snapshots - how the agent sees the page
Instead of guessing selectors, the agent asks for a snapshot. It gets an accessibility tree where every actionable element has a short ref. The agent then acts on that ref - deterministic and resilient.
# open the store (headed so you can watch) playwright-cli open https://app.thetestingacademy.com/playwright/ttacart/ --headed # capture the accessibility snapshot - elements come back with refs playwright-cli snapshot # act on a ref returned by the snapshot (example refs) playwright-cli fill e12 "your-username" playwright-cli click e18 # the Login button playwright-cli snapshot # re-snapshot to see the next screen
Hands-on
Drive TTACart end to end
A full login to checkout, one command at a time. Read the actual refs from each snapshot; the refs below are illustrative.
playwright-cli open https://app.thetestingacademy.com/playwright/ttacart/ --headed playwright-cli snapshot playwright-cli fill e12 "<username from login screen>" playwright-cli fill e14 "<password from login screen>" playwright-cli click e18 # Login playwright-cli snapshot # inventory page playwright-cli click e27 # Add to cart playwright-cli click e09 # Cart playwright-cli click e31 # Checkout playwright-cli fill e40 "Pramod" # first name playwright-cli fill e41 "Dutta" # last name playwright-cli fill e42 "560001" # postal code playwright-cli click e44 # Continue playwright-cli click e50 # Finish playwright-cli screenshot # capture the confirmation playwright-cli close
Reference
The command surface
| Group | Commands |
|---|---|
| Core actions | open goto close click dblclick fill type select check uncheck hover drag upload |
| Read | snapshot screenshot pdf eval resize |
| Dialogs | dialog-accept dialog-dismiss |
| Navigation | go-back go-forward reload |
| Keyboard & mouse | press keydown keyup mousemove mousedown mouseup mousewheel |
| Tabs | tab-list tab-new tab-select tab-close |
| Storage | state-save state-load cookie-* localstorage-* sessionstorage-* |
| Network | network route route-list unroute |
| DevTools | console run-code tracing-start tracing-stop video-start video-stop show |
| Sessions | -s=<name> list close-all kill-all delete-data |
| Config | --headed --browser --persistent --profile --config attach --extension install --skills config-print |
State
Sessions and saved auth
Run isolated browsers in parallel, and save a logged-in state once so every later session starts authenticated.
# run a named, isolated session playwright-cli -s=cart open https://app.thetestingacademy.com/playwright/ttacart/ --headed # list running sessions playwright-cli list # save the logged-in state, then reuse it later playwright-cli -s=cart state-save auth.json playwright-cli -s=checkout state-load auth.json # tidy up playwright-cli close-all playwright-cli kill-all
Network & storage
Mock, inspect, and persist
# inspect network activity playwright-cli network # mock or block a request with a route playwright-cli route "**/api/**" playwright-cli route-list playwright-cli unroute "**/api/**" # read and set cookies / storage playwright-cli cookie-list playwright-cli localstorage-get tta-cart playwright-cli sessionstorage-set key value
Developer tools
Console, tracing, and video
console & eval
Read console messages and run code in the page with run-code / eval.
tracing
tracing-start then tracing-stop records a trace you can open in the viewer.
video & show
video-start / video-stop capture the run; show opens the dashboard.
playwright-cli tracing-start # ... drive the flow ... playwright-cli tracing-stop trace.zip playwright-cli show # open the dashboard / viewer playwright-cli pdf checkout.pdf # save the page as PDF
For agents
Skills installed on demand
Rather than dumping a giant help text into the agent's context, the CLI exposes capabilities as skills the agent installs only when it needs them. This keeps the context window lean.
# install the skill set the agent can call playwright-cli install --skills # the agent discovers and uses skills as needed, # instead of reading verbose --help for every command
For agents
Use it from a coding agent
The Agent CLI is designed for tools like Claude Code and Copilot working inside large codebases. The agent loops: snapshot to perceive, decide the next action, run one command, then re-snapshot - all with output small enough to leave room for reasoning.
snapshot
Get the ref tree of the current page.
one command
click / fill / type on a specific ref.
re-snapshot
Confirm the result, then loop or assert.
Reference
Quick cheat sheet
| Command | Purpose |
|---|---|
| npm install -g @playwright/cli | Install the agent CLI. |
| playwright-cli open <url> --headed | Open a page in a visible browser. |
| playwright-cli snapshot | Accessibility snapshot with element refs. |
| playwright-cli click <ref> | Click an element by ref. |
| playwright-cli fill <ref> "text" | Fill an input by ref. |
| playwright-cli type "text" | Type into the focused element. |
| playwright-cli press Enter | Press a key. |
| playwright-cli screenshot | Capture a screenshot. |
| playwright-cli -s=<name> ... | Run in a named, isolated session. |
| playwright-cli state-save / state-load | Save / reuse logged-in state. |
| playwright-cli tracing-start / -stop | Record a trace. |
| playwright-cli install --skills | Install agent skills. |
| playwright-cli list / close-all / kill-all | Manage sessions. |
| playwright-cli config-print | Print resolved config. |
Hands-on
Six Agent CLI drills on TTACart
Open & snapshot
Install the CLI, open TTACart headed, and read the first snapshot - find the login refs.
Log in by ref
Use fill + click on refs with the on-screen credentials, then state-save the session.
Checkout
Add an item, go to the cart, and complete checkout one command at a time.
Sessions
Run two named sessions in parallel; state-load auth into both and compare.
Trace it
tracing-start, drive the flow, tracing-stop, then show the result.
Promote to a spec
Once the flow works, write the durable @playwright/test spec and commit it.
Sources
Official references
| Reference | URL |
|---|---|
| Agent CLI - Introduction | https://playwright.dev/agent-cli/introduction |
| Agent CLI - Getting started | https://playwright.dev/agent-cli/getting-started |
| Snapshots & capabilities | https://playwright.dev/agent-cli/snapshots |
| Sessions & dashboard | https://playwright.dev/agent-cli/sessions |
| Classic Playwright test CLI | https://playwright.dev/docs/test-cli |
| TTACart demo app | https://app.thetestingacademy.com/playwright/ttacart/ |