Example: seed test with fixture approach
Preferred seed test structure using a fixture to share navigation setup:
```ts
// tests/fixtures.ts
import { test as baseTest } from '@playwright/test';
export { expect } from '@playwright/test';
export const test = baseTest.extend({
page: async ({ page }, use) => {
await page.goto('https://example.com/');
await use(page);
},
});
```
```ts
// tests/seed.spec.ts
import { test } from './fixtures';
test('seed', async ({ page }) => {
// Fixture already navigates. This empty body tells agents where to start.
});
```
Example: playwright-cli session for generation
Example showing how to start a playwright-cli session and generate code:
```bash
# Start a session
playwright-cli open https://example.com/login
# Take a snapshot to see elements
playwright-cli snapshot
# Output shows: e1 [textbox "Email"], e2 [textbox "Password"], e3 [button "Sign In"]
# Fill form fields - generates code automatically
playwright-cli fill e1 "user@example.com"
# Ran Playwright code:
# await page.getByRole('textbox', { name: 'Email' }).fill('user@example.com');
playwright-cli fill e2 "password123"
# Ran Playwright code:
# await page.getByRole('textbox', { name: 'Password' }).fill('password123');
playwright-cli click e3
# Ran Playwright code:
# await page.getByRole('button', { name: 'Sign In' }).click();
```
Example: collecting generated code into test file
Example showing how to collect generated code into a Playwright test:
```typescript
import { test, expect } from '@playwright/test';
test('login flow', async ({ page }) => {
// Generated code from playwright-cli session:
await page.goto('https://example.com/login');
await page.getByRole('textbox', { name: 'Email' }).fill('user@example.com');
await page.getByRole('textbox', { name: 'Password' }).fill('password123');
await page.getByRole('button', { name: 'Sign In' }).click();
// Add assertions
await expect(page).toHaveURL(/.*dashboard/);
});
```
Playwright Test Generator agent tools available
The Playwright Test Generator agent provides tools for browser automation including: browser_click, browser_drag, browser_evaluate, browser_file_upload, browser_handle_dialog, browser_hover, browser_navigate, browser_press_key, browser_select_option, browser_snapshot, browser_type, browser_verify_element_visible, browser_verify_list_visible, browser_verify_text_visible, browser_verify_value, browser_wait_for, generator_read_log, generator_setup_page, and generator_write_test.
Playwright Test Generator workflow steps
To generate a test: (1) Obtain the test plan with all steps and verification specification, (2) Run generator_setup_page to set up the page for the scenario, (3) For each step and verification, use Playwright tools to execute it in real-time with the step description as the intent, (4) Retrieve the generator log via generator_read_log, (5) Immediately after reading the log, invoke generator_write_test with the generated source code.
Generated test file structure and naming requirements
Generated test files must contain a single test. The file name must be filesystem-friendly based on the scenario name. The test must be placed in a describe block matching the top-level test plan item. The test title must match the scenario name. The file should include a comment with the step text before each step execution, and must not duplicate comments if a step requires multiple actions.
Generated test file metadata requirements
Generated test files must include comments specifying the spec file path and seed file path at the top of the file, in the format: // spec: path/to/specs/plan.md and // seed: path/to/tests/seed.spec.ts
playwright-cli open command with browser parameter
Use playwright-cli open --browser=chrome, --browser=firefox, --browser=webkit, or --browser=msedge to specify which browser to launch when creating a session.
playwright-cli mobile emulation
Use playwright-cli open --mobile to emulate a generic mobile device (Pixel 10 for Chromium, iPhone 17 for WebKit). Alternatively, use --device="iPhone 15" or other specific device names for targeted mobile emulation.
playwright-cli persistent profile usage
Use playwright-cli open --persistent to launch a browser with a persistent profile that persists across sessions. By default, profiles are in-memory. Use --profile=/path/to/profile to specify a custom profile directory.
playwright-cli attach to browser via Playwright Extension
Use playwright-cli attach --extension=chrome to connect to a browser via the Playwright Extension.
playwright-cli attach via CDP endpoint
Use playwright-cli attach --cdp=chrome or --cdp=msedge to connect to a running Chrome or Edge browser by channel name. Alternatively, use --cdp=http://localhost:9222 to connect via a specific CDP endpoint URL.
playwright-cli named browser sessions
Use the -s flag to create and manage named browser sessions. For example, playwright-cli -s=mysession open example.com --persistent creates a named session, and subsequent commands use -s=mysession to interact with that session. Use playwright-cli -s=mysession close to stop a named browser and playwright-cli -s=mysession delete-data to delete user data for the session.
playwright-cli close and clean up commands
Use playwright-cli close to close the current browser session. Use playwright-cli close-all to close all browsers. Use playwright-cli kill-all to forcefully kill all browser processes. Use playwright-cli delete-data to delete user data for the default session.
playwright-cli snapshot command options
Use playwright-cli snapshot to take a snapshot with a timestamp-based filename. Use --filename=after-click.yaml to save to a specific file. Use a selector like playwright-cli snapshot "#main" to snapshot a specific element. Use --depth=4 to limit snapshot depth for efficiency. Use --boxes to include bounding box information for each element as [box=x,y,width,height]. Use playwright-cli find "Add to cart" to search a snapshot and return matching nodes with 3 lines of context around each match.
playwright-cli targeting elements with refs, selectors, and locators
Interact with elements using refs from snapshots (e.g., playwright-cli click e15), CSS selectors (e.g., playwright-cli click "#main > button.submit"), role locators (e.g., playwright-cli click "getByRole('button', { name: 'Submit' })"), or test IDs (e.g., playwright-cli click "getByTestId('submit-button')").
playwright-cli core interaction commands
Core interaction commands include: click, dblclick, type, fill (with optional --submit flag to press Enter), drag, drop (with --path or --data options), hover, select, upload, check, uncheck. Use playwright-cli find "text" to search snapshots for text or use --regex for regexp searches with /flags/i syntax for case-insensitive and other regex flags.
playwright-cli eval command for JavaScript execution
Use playwright-cli eval "document.title" to evaluate JavaScript in the page context. Use playwright-cli eval "el => el.textContent" e5 to evaluate JavaScript with an element reference to access element properties like id, class, or custom attributes.
playwright-cli dialog handling
Use playwright-cli dialog-accept to accept a dialog. Use playwright-cli dialog-accept "confirmation text" to accept with specific text. Use playwright-cli dialog-dismiss to dismiss a dialog.
playwright-cli keyboard and mouse commands
Keyboard commands: playwright-cli press Enter or ArrowDown, playwright-cli keydown Shift, playwright-cli keyup Shift. Mouse commands: playwright-cli mousemove 150 300, playwright-cli mousedown and mouseup (optionally with 'right' for right button), playwright-cli mousewheel 0 100.
playwright-cli navigation commands
Navigation commands: playwright-cli go-back, playwright-cli go-forward, playwright-cli reload, and playwright-cli resize 1920 1080 for viewport resizing.
playwright-cli screenshot and PDF export
Use playwright-cli screenshot to capture a screenshot of the entire page or playwright-cli screenshot e5 for a specific element. Use --filename=page.png to specify output filename and --hires for high-resolution output. Use playwright-cli pdf --filename=page.pdf to export to PDF.
playwright-cli tab management commands
Use playwright-cli tab-list to list all tabs, playwright-cli tab-new to open a new tab, playwright-cli tab-new https://example.com/page to open a new tab and navigate, playwright-cli tab-close to close the current tab, playwright-cli tab-close 2 to close a specific tab, and playwright-cli tab-select 0 to select a tab by index.
playwright-cli storage state management
Use playwright-cli state-save to save browser state and playwright-cli state-load auth.json to load a previously saved state. This covers cookies, localStorage, and sessionStorage persistence.
playwright-cli cookie commands
Cookie commands: playwright-cli cookie-list to list all cookies, playwright-cli cookie-list --domain=example.com to filter by domain, playwright-cli cookie-get session_id to get a specific cookie, playwright-cli cookie-set session_id abc123 to set a cookie, playwright-cli cookie-set session_id abc123 --domain=example.com --httpOnly --secure to set with options, playwright-cli cookie-delete session_id to delete one, and playwright-cli cookie-clear to clear all.
playwright-cli localStorage commands
Use playwright-cli localstorage-list to list all localStorage items, playwright-cli localstorage-get theme to retrieve a value, playwright-cli localstorage-set theme dark to set a value, playwright-cli localstorage-delete theme to delete an item, and playwright-cli localstorage-clear to clear all localStorage.
playwright-cli sessionStorage commands
Use playwright-cli sessionstorage-list to list all sessionStorage items, playwright-cli sessionstorage-get step to retrieve a value, playwright-cli sessionstorage-set step 3 to set a value, playwright-cli sessionstorage-delete step to delete an item, and playwright-cli sessionstorage-clear to clear all sessionStorage.
playwright-cli route and network mocking
Use playwright-cli route "**/*.jpg" --status=404 to mock responses with a specific status code. Use playwright-cli route "https://api.example.com/**" --body='{"mock": true}' to mock with custom response body. Use playwright-cli route-list to view active routes, playwright-cli unroute "**/*.jpg" to remove specific routes, and playwright-cli unroute to remove all routes.
playwright-cli console and request inspection
Use playwright-cli console to view all console messages, playwright-cli console warning to filter by type. Use playwright-cli requests to view all network requests and playwright-cli request 5 to view details of a specific request.
playwright-cli run-code command
Use playwright-cli run-code "async page => await page.context().grantPermissions(['geolocation'])" to execute custom Playwright code. Use --filename=script.js to load code from a file.
playwright-cli video recording
Use playwright-cli video-start video.webm to begin recording video. Use playwright-cli video-chapter "Chapter Title" --description="Details" --duration=2000 to add chapters to the video. Use playwright-cli video-show-actions --duration=600 --position=top-right to annotate each action with a callout and highlight (optional --position values include top-right). Use playwright-cli video-hide-actions to stop showing action annotations. Use playwright-cli video-stop to finish recording.
playwright-cli show command for UI review with annotation
Use playwright-cli show --annotate to launch a dashboard for interactive UI review and design feedback. Users can annotate the page by drawing boxes and typing comments, and you receive the annotated screenshot, snapshot of marked regions, and user notes.
playwright-cli generate-locator command
Use playwright-cli generate-locator e5 --raw to generate a Playwright locator for an element from its ref or selector, returning the raw locator string.
playwright-cli highlight command
Use playwright-cli highlight e5 to show a persistent highlight overlay for an element. Use --style="outline: 3px dashed red" to apply custom styling. Use playwright-cli highlight e5 --hide to hide a single element's highlight or playwright-cli highlight --hide to hide all page highlights.
playwright-cli raw output mode
Use the --raw global option to strip page status, generated code, and snapshot sections from output, returning only the result value. This is useful for piping command output to other tools. Commands that don't produce output return nothing when --raw is used.
playwright-cli JSON output format
Use the --json flag to wrap every reply as structured JSON output, useful for programmatic parsing of command results.
playwright-cli installation methods
If the global playwright-cli command is not available, use npx playwright cli or python -m playwright cli as a local version. To install globally, run npm install -g @playwright/cli@latest. Check version with npx --no-install playwright --version or python -m playwright --version.
playwright-cli Windows URL ampersand escaping
On Windows, cmd.exe and PowerShell treat & as a command separator, truncating URLs with multiple query parameters. Escape & with ^& in cmd.exe (e.g., playwright-cli goto "https://example.com/?a=1^&b=2") or use --% in PowerShell (e.g., playwright-cli --% goto "https://example.com/?a=1&b=2").
playwright-cli snapshot automatic output after commands
After each command, playwright-cli provides a snapshot of the current browser state showing Page URL, Page Title, and a link to the snapshot file (saved with timestamp-based names like page-2026-02-14T19-22-42-679Z.yml).
playwright-cli detach from attached browser
Use playwright-cli -s=msedge detach to detach from an attached browser session, leaving the external browser running.