Skip to content
CLI tool · Testing

Playwright for AI agents

The Playwright CLI for coding agents: run end-to-end tests, rerun failures, target one browser, and take screenshots, with rules for snapshots and test data.

playwrightby MicrosoftOfficial MCP option
What it is

What Playwright does for an agent

Playwright is Microsoft's open-source framework for end-to-end testing across Chromium, Firefox, and WebKit. Its CLI runs the test suite, installs browsers, and takes quick screenshots.

Tests give an agent a clear signal: the exit code says pass or fail, and the line reporter prints which test failed and why, without opening a browser window.

Official site: playwright.dev

Install

Install Playwright

The install commands from the official docs. Check it worked with npx playwright --version.

Add Playwright Test to a project
npm init playwright@latest
Install a browser and its system dependencies
npx playwright install --with-deps chromium
Auth

Signing in Playwright for an agent

No account is needed. Tests that log in should read test credentials from environment variables and save session state to a gitignored file.

Common agent tasks

What agents use Playwright for

Example commands with placeholder names. Swap in your own project, app, or file names.

  1. Run the suite with compact output

    playwright
    npx playwright test --reporter=line
  2. Run one file in one browser

    playwright
    npx playwright test tests/login.spec.ts --project=chromium
  3. Rerun only the tests that failed last time

    playwright
    npx playwright test --last-failed
  4. Screenshot a page

    playwright
    npx playwright screenshot http://localhost:3000 home.png
Safety

Safety rules for agents

Safe to run without asking

  • npx playwright test (against local or staging)
  • npx playwright screenshot
  • npx playwright --version

Ask a human first

  • npx playwright test --update-snapshots
  • running tests against production
  • editing or skipping a failing test
  • Run tests against local or staging environments, never production data.
  • Only update visual snapshots when asked, and say which ones changed.
  • Do not skip or loosen a failing test to make the run pass; fix the code or report the failure.
  • Avoid interactive commands such as show-report, codegen, and --ui in an agent session; they open windows or servers that wait for a person.
Agent instructions

AGENTS.md or CLAUDE.md snippet

Paste this into the instruction file your agent reads at the start of every session, then adjust the rules to your setup.

AGENTS.md
## Playwright (playwright)

Use `playwright` for running and debugging end-to-end browser tests.

- Check it is installed with `npx playwright --version`. If it is missing, ask before installing it.
- Auth: none needed.
- Run without asking (read-only): `npx playwright test (against local or staging)`, `npx playwright screenshot`, `npx playwright --version`.
- Ask a human first (changes or deletes things): `npx playwright test --update-snapshots`, `running tests against production`, `editing or skipping a failing test`.
- Run tests against local or staging environments, never production data.
- Only update visual snapshots when asked, and say which ones changed.
- Do not skip or loosen a failing test to make the run pass; fix the code or report the failure.
- Avoid interactive commands such as show-report, codegen, and --ui in an agent session; they open windows or servers that wait for a person.
- Prefer JSON output where the command supports it, and keep output short.
CLI or MCP server?

Playwright or an MCP server?

An official MCP option exists. Microsoft publishes an official Playwright MCP server that lets an agent drive a browser through accessibility snapshots. Use it to explore a page interactively; use the CLI to run the test suite.

Rule of thumb: in a terminal agent that a human has signed in, the CLI is usually faster and cheaper in context. In a chat client without a shell, or when you want a fixed set of typed tools, use an MCP server.

Same category
All Testing tools
FAQ

Playwright questions

How do I install Playwright?

Add Playwright Test to a project: npm init playwright@latest. Other official options: Install a browser and its system dependencies. Check the install with npx playwright --version, and see the Microsoft docs for other platforms.

How does an AI agent authenticate Playwright?

No account is needed. Tests that log in should read test credentials from environment variables and save session state to a gitignored file. There is nothing to set up: an agent can use it as soon as npx playwright --version succeeds.

Which Playwright commands are safe for an agent to run?

Commands that only read state are safe to run without asking: npx playwright test (against local or staging); npx playwright screenshot; npx playwright --version. Ask a human before anything that changes or deletes something: npx playwright test --update-snapshots; running tests against production; editing or skipping a failing test.

Is there an MCP server for Playwright?

Microsoft publishes an official Playwright MCP server that lets an agent drive a browser through accessibility snapshots. Use it to explore a page interactively; use the CLI to run the test suite.