The agent crawls a site, transcribes the flow and picks the assertions . Your job is to read the diff and ship.
Drive a flow, create tests and debug
Have your coding agent turn a CLI-driven session into a runnable Playwright spec.
You watched an agent run a browser session in the previous lesson. It opened a browser, searched, and clicked around. It's wild, isn't it?
But so far no test came out of it. Let's extend this agent flow with an add-to-cart step and turn the transcript into a runnable spec.
As you've seen, every playwright-cli action prints the equivalent Playwright code. If you drive a flow once, all the information to generate a test is available.
There's one caveat, though: the CLI only prints actions. Assertions are your (or your agent's) job.
Prompt a complete cycle
Open the agent of your choice in your workshop project and prompt:
Open https://www.playwright-workshop.online/, search for snowboard, click the first snowboard,
add it to the cart and validate the cart. Generate a new Playwright test:
tests/snowboard.spec.ts. Use playwright-cli to drive the browser. Add
meaningful assertions to the test.
The agent reads the skill, drives the flow with playwright-cli, and collects every printed await page.… line into a spec. Yours will look different, but it'll have roughly this shape:
import { test, expect } from "@playwright/test";
test("adds a snowboard to the cart from search", async ({ page }) => {
await page.goto("https://www.playwright-workshop.online/");
await page
.getByRole("searchbox", { name: "Search products" })
.fill("snowboard");
await page.keyboard.press("Enter");
await page.getByRole("link", { name: "All Mountain Snowboard" }).click();
await page.getByRole("button", { name: "Add item to cart" }).click();
await expect(page.getByLabel("Open cart")).toContainText("1");
});
Run it (or let your agent run it):
$ npx playwright test tests/snowboard.spec.ts
Is this actually a good test?Select this container via test id: judge-the-generated-test
Your generated test will look different! Stop and read your generated spec as if it landed in a PR for review. The CLI drove the browser, and the agent picked the locators and the assertion.
But you'll always be the one who decides whether this ships.
Find things you'd push back on before approving it.
Focus on quality:
- Locators: does each one uniquely identify the element you mean, or is it "whichever happens to match first"?
- Assertions: does
toContainText("1")on the entire Open cart button actually prove a snowboard is in the cart? And does the test use the right web-first assertions? - Missing checks: did the user reach a product page? Is the cart's content asserted, or only its counter?
The agent wasn't wrong. It did exactly what the prompt asked. But it only delivers a draft.
Reviewing what comes back is essential. You're still in charge!
FYI: when the page snapshot doesn't give your agent enough information to write an assertion, the CLI has two helpful commands:
playwright-cli generate-locator <ref>prints the locator you'd pass toexpect(...).playwright-cli eval "el => el.dataset.testid" <ref>reads the attributes the snapshot strips out (id,class,data-*). That's useful when agenericelement in the YAML doesn't have a name to go by.
When to use AI generation — and when not
So, your agent can hand you a spec draft after receiving one(!) vague prompt. Whether you should take it depends on what you're testing and whether the run was worth the tokens.
Every run takes seconds to minutes and tokens billed to your API key. Spend that budget where the agent pays it back.
A deterministic recorder, no API call. You drive every line and add the assertions where they matter.
Honestly, that's totally up to you.
Earn trust slowly. Hand-write or drive from playwright codegen. Let the agent only write the tests you'd otherwise skip to save time.
Let the agent draft most specs. You step in for critical paths and edit what doesn't fit. You know how easy it is to lose control.
But! Always review the outcome. Always. Always. Always! You're still in charge!
The quality bar is the same as in a code review: would you ship it as-is? If yes, ship it. If not, you have a low-effort starting point. Keep editing until you would.
When generation goes sideways
Coding agents can look confident and still totally mess up. You'll see locators that match three things, overlays that swallow clicks, or assertions that fire too early. When the generated test fails or the agent gets stuck mid-flow, the easiest fix is to hand the problem back to the agent with the same tools it used to build the test.
tests/snowboard.spec.ts fails on the cart assertion. Use playwright-cli to
drive the page, find what's actually there, and propose a fix.
That prompt alone can get you surprisingly far. The agent opens a browser again and inspects the page directly:
playwright-cli snapshot: what's actually rendered, with the same refs the spec would use.playwright-cli console: app errors the test never showed.playwright-cli requests: did a request fail, return the wrong shape, or land too late?playwright-cli show --annotate: when the agent can't find something on its own, it can ask you to point at it.
You don't need new commands or a special mode. Agents are pretty good at figuring this stuff out these days, and the Playwright skill tells them what to do. Give them the tools and the expected outcome, and they'll keep iterating.
But there are some fairly new agentic debugging tools if things get very tricky.
How your agent can debug your Playwright tests
If you're dealing with failing Playwright tests, you can ask your agent to debug them for you.
--debug=cli only really means two things running at once. The runner pauses your test and prints a session name (e.g. tw-abcdef). The agent uses that name to attach into the paused page.
npx playwright test --debug=cliRuns in the background, paused at the first line of your failing test. Holds everything the test had wired up and prints a session name when it pauses.
playwright-cli attach tw-abcdefThe agent's session, joined to the paused test by the session name. Same toolbox as anywhere else, but it drives the test's actual page.
Go with the flow and generate more tests
Generate a new test for Chaos mode
Re-run the snowboard prompt, but this time point the agent at the workshop store with chaos enabled:
Open https://www.playwright-workshop.online/?chaos, search for snowboard, click the first
product, add it to the cart and validate the cart. Generate a Playwright test
at tests/snowboard-chaos.spec.ts. Use playwright-cli to drive the browser.
?chaos enables real-world friction. There will be a newsletter overlay that pops up on the homepage. The agent has to deal with all of it without your help. Will it manage?
Skim the playwright-cli skill and references
Open the Playwright CLI skill (e.g. .claude/skills/playwright-cli/SKILL.md) in your editor. You don't have to memorize it. That's your agent's job. But scan the section headers (Core, Navigation, Storage, Network, DevTools…) so you know what's there when you write the next prompt. Spot the commands you've already seen the agent use, and the ones you haven't.
Pay special attention to the reference files (e.g. references/test-generation.md). Ideally, the agent loads these on demand, but sometimes you need to point it there.