English

AI test generation for Playwright: how to get tests worth keeping

On this page

AI test generation for Playwright works when you give the model a precise behavior to check, let it look at the running application, and then review the result as strictly as hand-written code. The model is fast at typing tests; deciding what is worth testing and proving that a test can fail is still your job.

This guide covers how to describe a test so the output is useful, a worked example, and a review checklist for generated Playwright code.

Why does AI test generation for Playwright go wrong?

Most poor generated tests come from one of four gaps:

  • No view of the app. A model that only reads source code guesses labels and roles. Agents that drive a real browser, such as the planner and generator agents that now ship with Playwright, check locators against the live page instead.
  • A vague request. “Test the login page” gives a happy-path click-through with an assertion on whatever happened to be on screen.
  • No definition of success. If you do not say what proves the behavior, the model often asserts that the page URL changed, which almost never fails.
  • No negative check. Nobody confirms that the test goes red when the feature breaks.

The fixes are a better request and a stricter review, not a better model.

How should you describe a test to an AI agent?

Write the request as a short spec. Five parts are enough:

  1. Goal: the user outcome, in one sentence.
  2. Start state and data: who is signed in, what exists already, which test data to use.
  3. Steps: what a user does, in their words, not in selectors.
  4. Oracle: the visible result that proves success or the expected error.
  5. Constraints: locator preferences, what may and may not be mocked, file location, one behavior per test.

A worked example for a checkout:

Goal: a customer whose card is declined sees a clear error and keeps their cart.
Start: signed-in customer with one item in the cart. Use the declined test card from fixtures/cards.ts.
Steps: open the cart, continue to payment, enter the declined card, pay.
Oracle: an alert reads "Your card was declined."; the cart still shows one item.
Constraints: role, label and text locators only; no CSS or XPath; no fixed waits;
do not mock the payment page; one test per behavior; write to tests/checkout.spec.ts.

Notice what the spec leaves out: selectors, timings and implementation detail. The agent finds those on the page. You own the behavior and the oracle.

For larger features, ask for a plan first: a list of scenarios with a one-line oracle each. Reviewing ten lines of plan is cheaper than reviewing ten generated tests that cover the wrong things. Playwright’s own planner agent works this way, writing a Markdown plan in a specs/ folder before the generator writes code.

Example A request an agent can test against

Declined card

Goal
A customer whose card is declined sees a clear error and keeps the cart
Start and data
Signed in, one item, the declined test card
Steps
Open the cart, continue to payment, enter the card, pay
Oracle
An alert reads “Your card was declined.” and the cart still shows one item
Constraints
Role, label and text locators; no fixed waits
Five parts; the oracle is what makes the test able to fail.

What does a good generated test look like?

From the spec above, a test you would keep looks like this:

test("keeps the cart after a declined card", async ({ page }) => {
  await page.goto("/cart");
  await page.getByRole("button", { name: "Continue to payment" }).click();
  await page.getByLabel("Card number").fill(cards.declined);
  await page.getByRole("button", { name: "Pay now" }).click();
  await expect(page.getByRole("alert")).toHaveText("Your card was declined.");
  await page.goto("/cart");
  await expect(page.getByRole("listitem")).toHaveCount(1);
});

It uses user-facing locators, waits through assertions rather than timers, states one behavior in its name, and checks two things a user would notice.

How do you review AI-generated Playwright tests?

Use the same checklist on every generated file:

Check What to look for Why
Locators getByRole, getByLabel, getByText, getByTestId before CSS or XPath Playwright’s documentation recommends user-facing attributes because the DOM changes
Waiting expect(...).toBeVisible() and other web-first assertions; no waitForTimeout Fixed sleeps are slow on a fast run and flaky on a slow one
Oracle An assertion on the outcome in the spec, not only on the URL A URL check passes even when the page shows an error
Isolation Each test sets up its own state; no order dependence Parallel runs and retries break shared state
Data Test data from fixtures, not values copied from today’s screen Copied values break when the data changes
Scope Third-party pages are not tested; your own payment page is not mocked away Test what you control, and do not mock the thing under test
Naming One behavior per test, named after it A failure should say what broke without reading the code

Then run two checks no review can replace:

  • Prove it can fail. Change the expected text, disable the button or point the test at an older build. A test that still passes is not testing the behavior.
  • Prove it is stable. Run it repeatedly, for example npx playwright test tests/checkout.spec.ts --repeat-each=10. A test that fails once in ten is not ready.

How do you keep generated tests maintainable?

Generated code ages like any other code. Three habits keep it healthy:

  • Keep the spec next to the test. A short comment or a plan file in the repository tells the next reader, and the next agent, what the test is for. When the UI changes, you regenerate against the intent, not against old selectors.
  • Extract repetition after the third copy. Agents happily repeat sign-in steps in every file. Move them into a fixture or a helper once a pattern repeats.
  • Treat repairs as code changes. When a test breaks after a UI change, a repair should change how the test finds an element, never what it asserts. The self-healing test scripts guide covers how to review those repairs.

If your code or test data is sensitive, decide early which model sees it. The local AI test agent guide lists what an agent sends and what you can keep on your machine.

Where does an agent fit in your workflow?

The pattern that holds up is simple: an agent plans and writes, a person reviews, and CI runs ordinary test files with no model involved. Generated tests should be plain Playwright code in your repository, so they keep working if you change models or stop using the agent.

That is the workflow behind Probeflow Desktop, a local-first AI test agent that writes and runs Playwright tests in your repository, currently in preview. You describe a check in plain words, the agent explores the app, drafts a plan and writes real tests, and you can stop it for review before the plan, the cases, the scripts or the run. For a project with no tests yet it starts from a Playwright template, and the AI-written Playwright tests it produces run in CI without it. The guide to giving the agent its first test task shows the steps.

Sources

Frequently asked questions

Can AI write Playwright tests that are ready to merge?

It can write a good first draft when it can see the running application and you describe the behavior, the data and the expected outcome. Treat the output like a pull request from a new colleague: read it, run it several times and check that it fails when the feature is broken.

What should I put in a prompt to generate a Playwright test?

The user goal, the starting state and test data, the steps a user takes, the observable result that proves success, and the constraints: which locators to prefer, what not to mock and where the file goes. Ask for one behavior per test.

Why do AI-generated Playwright tests become flaky?

The usual causes are fixed waits instead of web-first assertions, CSS or XPath selectors tied to page structure, tests that depend on each other or on shared data, and assertions on text that changes. All four are visible in review.

How do I know a generated test actually tests something?

Break the behavior on purpose, for example by changing the expected message or disabling the button, and run the test. If it still passes, its assertion is too weak or checks the wrong element.