Workflow recipe
Generate Playwright tests with AI agents, then review them
Approve a test plan before any code exists, generate tests from it and review them like production code.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkOn this page
To generate Playwright tests with AI, update to a recent Playwright, generate the test agent definitions for your coding agent and write a seed test that puts the app in a known state. Ask for a Markdown test plan and edit it, generate tests from the approved plan, run them, and review the tests like production code before merging.
Since version 1.56, Playwright ships three test agents. The planner explores your app and writes a Markdown test plan. The generator turns the plan into Playwright Test files, checking selectors and assertions against the live page as it goes. The healer runs failing tests and proposes fixes. They drive the browser through Playwright’s MCP tools and run inside the coding agent you already use.
This recipe covers one user journey end to end and puts a person in charge at two points: approving the plan before any code is written, and reviewing the tests before they are merged. Generated tests that nobody reads are worse than no tests, because they look like coverage.
TerminalAbout 10 min
1. Make sure the project is on a recent Playwright
npm install -D @playwright/test@latest
npx playwright install --with-deps chromium
npx playwright --version # must be 1.56 or laterIf you have no Playwright project yet, npm init playwright@latest creates one with a config and an example test.
TerminalAbout 5 min
2. Generate the agent definitions for your coding agent
init-agents writes the agent instructions and MCP configuration for the tool you name. Regenerate them whenever you upgrade Playwright, because the tools and instructions change between versions.
npx playwright init-agents --loop=claudeThis writes three subagents to .claude/agents/ (playwright-test-planner, playwright-test-generator and playwright-test-healer) and an .mcp.json that registers Playwright’s test MCP server.
npx playwright init-agents --loop=codexThis writes agent definitions under .codex/agents/ for Codex’s subagents.
npx playwright init-agents --loop=vscodeThe VS Code agentic experience needs VS Code 1.105 or later.
It also creates a specs/ folder for plans and, if you do not have one, a seed test. Commit these files so the whole team uses the same definitions.
EditorAbout 15 min
3. Write a seed test that gets the app into a known state
The planner runs the seed test first and treats it as the template for every generated test. Put login, test data and any fixtures here, so the agents start from the same state your real tests do.
import { test, expect } from "@playwright/test";
test("seed", async ({ page }) => {
// Start every scenario signed in as the standard test customer.
await page.goto("/login");
await page.getByLabel("Email").fill(process.env.E2E_USER_EMAIL!);
await page.getByLabel("Password").fill(process.env.E2E_USER_PASSWORD!);
await page.getByRole("button", { name: "Sign in" }).click();
await expect(page.getByRole("heading", { name: "Dashboard" })).toBeVisible();
});Set baseURL in playwright.config.ts and keep credentials in environment variables. If your project already has a fixtures file, import test from it instead, as Playwright’s own example does.
Claude CodeAbout 20 min
4. Ask the planner for a plan, then edit it yourself
Start your app, open Claude Code in the repository and ask for a plan for one journey. Give it the user story and the boundaries.
The plan is plain Markdown: scenarios with numbered steps and expected results. Read it as you would a colleague’s test design. Delete scenarios that duplicate existing tests, add the business rules the agent could not know, and fix expected results that describe what the app does today rather than what it should do. Commit the edited plan.
Claude CodeAbout 15 min
5. Generate the tests from the approved plan
The generator walks through each scenario in a real browser, confirms the locators and assertions as it goes, and writes test files under tests/. Each file starts with comments pointing back to the spec and seed.
TerminalAbout 10 min
6. Run the tests and heal only what is genuinely broken
npx playwright test tests/ --reporter=listFor failures, ask the healer to investigate, and keep its changes in a separate commit so you can review them:
Run the suite three times in a row (npx playwright test --repeat-each=3) to catch flaky tests before anyone else does.
Pull requestAbout 15 min
7. Review the tests like production code
Open a pull request with the plan and the tests, and review with these questions:
- Does every assertion check something a user would notice, not just that an element exists?
- Would the test fail if the feature broke? Try breaking it locally: change a label or skip a save call, and check a test goes red.
- Are locators based on roles and labels rather than CSS classes or positions?
- Is test data created by the test or the seed, not assumed to exist?
Only merge tests you would have been happy to write yourself.
Check it worked
Verification checklist
0 of 7 done
When it goes wrong
If the agents are not available in Claude Code, check that .claude/agents/ contains the three Playwright files and that the Playwright MCP server in .mcp.json is approved. Claude Code asks for approval of project-scoped MCP servers the first time; run claude mcp list to see their status.
If the planner wanders into unrelated parts of the app, the scope in your prompt is too loose. Name the journey, the start page and what to leave out.
If generated tests pass but assert very little, the expected results in the plan were vague. Make them specific (“the confirmation shows the new street and postcode”) and regenerate.
If the healer “fixes” a test by loosening an assertion, reject the change. A healer can only see that the page changed, not whether the change was intended.
Variations
Without the planner, you can write the plan yourself in the same Markdown format and use only the generator. Teams with existing test case management often do this, pasting a test case into specs/.
For exploratory checks rather than committed tests, the webapp-testing and playwright-cli skills let an agent drive a browser directly and report what it found.
Related Playwright testing guides
Playwright test agents explains the planner, generator and healer in depth. When a generated test starts failing on and off, fix flaky tests with the Playwright healer. To explore an app before you plan, try exploratory testing with Playwright MCP, and use AI-assisted test design to decide what the journey should cover. The AI for test engineers learning path links these up, and more recipes are in the AI workflow examples hub.
Outcome and tools
- Outcome
- A Markdown test plan and runnable specs for one user journey, reviewed by a person.
- For
- QA engineers, Developers
- Variants for
- Claude Code, Codex
- Agent Skills
Related
- Playwright test agents: planner, generator and healer
How Playwright's planner, generator and healer test agents work, how to set them up with Claude Code or VS Code, and how to review the tests they write for you.
- Write unit tests for legacy code with a coding agent
An agent writes characterisation tests, you check for weak assertions and prove them with mutation testing.
- AI for test engineers
An AI course for testers: apply generative AI to everyday test automation, from AI-assisted test design to generating, healing and reviewing Playwright tests.
- AI in testing
Using AI to plan, generate and maintain tests, and testing AI features themselves.