Workflow recipe
Fix flaky E2E tests with the Playwright healer, safely
Prove the flake, run Playwright's healer agent on a branch, check each change for red flags, keep a heal log and merge by hand.
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 fix flaky tests with AI, prove the test is flaky and capture a Playwright trace, then run the healer agent on a dedicated branch with that evidence and clear limits. Confirm the fix with the repeated run that found the flake, review the diff against a list of red flags and log every change before a person merges it.
Playwright’s healer agent takes the name of a failing test, replays it, looks at the current page and proposes a patch: a new locator, a different wait, a data fix. It re-runs the test until it passes or its guardrails stop it. If it decides the feature itself is broken, it can skip the test instead.
That last part is why this recipe exists. A healer that makes a red test green has done one of two things: fixed a fragile test, or hidden a real bug. From the outside the two look the same. This workflow keeps the speed of the healer and adds the checks that tell those cases apart.
TerminalAbout 10 min
1. Prove the test is flaky, and capture a trace
Before you heal anything, separate “flaky” from “broken”. A test that fails every time after a UI change is broken, and the fix may belong in the app. A test that fails one run in ten is flaky, and the cause is usually timing, shared state or test data.
Run the suspect test many times with traces kept for failures:
npx playwright test tests/checkout.spec.ts -g "applies discount code" \
--repeat-each=20 --workers=4 --trace=retain-on-failureWrite down the result, for example “3 of 20 failed, all on the total assertion”. Open one failing trace with npx playwright show-trace and look at what the page showed at the moment of failure. Try it again with --workers=1. If the failures disappear with one worker, the tests are probably sharing data, and no locator change will fix that.
TerminalAbout 5 min
2. Set up the healer on a dedicated branch
If you have not generated the agent definitions yet, do it now, choosing the loop for your agent:
npx playwright init-agents --loop=claudeThe other values are vscode, codex and opencode. Regenerate the definitions whenever you upgrade Playwright, since they change with each release.
Then create a branch just for this heal, so the healer’s changes arrive as one reviewable diff:
git switch -c heal/checkout-discount-flakeNever run the healer on your main branch or on a branch with unrelated work. You want git diff to show exactly what the healer touched and nothing else.
Claude CodeAbout 15 min
3. Run the healer with your evidence and limits
Start your agent in the project and give the healer the test name, what you found in step 1 and clear limits. Without limits, healers tend to “fix” flakiness by loosening assertions or adding long timeouts.
Replace the bracketed parts. The rule about stopping instead of skipping matters: Playwright documents that the healer may skip a test it thinks is broken, and you want that decision made by a person.
TerminalAbout 10 min
4. Confirm the fix with the same run that found the flake
A single green run proves nothing for a flaky test. Repeat the exact command from step 1:
npx playwright test tests/checkout.spec.ts -g "applies discount code" \
--repeat-each=20 --workers=4 --trace=retain-on-failureYou want 20 of 20. If the original failure rate was low, run 50. Then run the whole file and the full suite once, because a fix to a shared fixture or helper can break neighbours.
Last, check that the test still catches the bug it was written for. Break the behaviour on purpose (change the discount in a local copy of the app, or edit the expected value in the test) and confirm the test fails. If it still passes, the healer weakened it, whatever the diff looks like.
EditorAbout 10 min
5. Review the diff against the red flags
Read git diff as you would a colleague’s pull request. These patterns need a reason before you accept them:
- An assertion removed, or an expected value changed. This is the most common way a healer hides a bug.
toBeVisible()replaced with something weaker, such as checking the element exists.test.skip,test.fixmeortest.failadded anywhere.waitForTimeout, a raisedtimeout, or{ force: true }on a click. These mask timing problems without fixing them.- Locators that moved from roles or labels to CSS classes,
nth()or text that changes with copy edits. - Edits to shared fixtures,
playwright.config.tsor the seed test.
Good heals usually look boring: a locator updated to match a renamed button, a web-first assertion (await expect(...)) replacing a manual check, or a test that now waits for a network response before reading the total.
GitHubAbout 10 min
6. Log the change and open a pull request for a human
Keep a heal log in the repository, for example tests/HEAL_LOG.md, with one entry per heal. It turns a pile of silent patches into a record you can learn from, and it shows when the same area keeps breaking.
## 2026-11-20 checkout.spec.ts › applies discount code
- Before: 3 of 20 runs failed at the order total assertion.
- Cause: total was read before the /api/cart response finished.
- Change: wait for the cart response, then assert with expect(...).toHaveText().
- Assertions changed: none.
- After: 50 of 50 passed; test fails when the discount is broken on purpose.
- Healed by: playwright-test-healer, reviewed by: [NAME]Commit the test change and the log entry together and open a pull request. Ask for a review from someone who knows the feature. Do not set the pull request to merge automatically, and do not let a CI job push healer output straight to your main branch.
If the healer concluded the app is broken, do not merge a skip. Open a bug with the trace attached, and quarantine the test with a test.fixme that links to the issue, so the gap is visible.
Check it worked
Verification checklist
0 of 7 done
When it goes wrong
If the healer keeps proposing longer timeouts, the real cause is usually that the test reads the page before the app is ready. Point it at the network call or the loading state to wait for, and repeat the rule against fixed waits.
If the test passes with one worker and fails with several, tests are sharing data, such as one user account or one cart. The fix is in fixtures or test data (a fresh user per test, unique names), and the healer often cannot see that from a single test. Use the systematic-debugging skill or work through the trace yourself.
If the healer changes an expected value and says “the app now shows X”, check with the product owner or the commit history whether X is intended. A changed price, label or count can be a deliberate change or a regression, and only someone who knows the feature can say.
If the test only fails in CI, compare the environments: CPU, screen size, locale, time zone and test data. Capture a trace in CI with --trace=retain-on-failure and give the healer the trace findings in the prompt.
Variations
To heal in CI, run the healer in a scheduled job that opens a pull request per heal and never pushes to the main branch. Keep the same rules in the prompt, and require a reviewer from the owning team.
Without the healer, the same loop works with any coding agent and the playwright-cli skill: hand it the trace findings and the rules from step 3. The review in step 5 and the log in step 6 stay the same.
For a suite with many flaky tests, start with the ones that fail most often in the last 30 days of CI, and heal one at a time. Batch heals produce diffs too big to review properly.
Related Playwright testing guides
The healer is one of the three Playwright test agents, and the same setup lets you generate Playwright tests with AI. The glossary entries for flaky tests and self-healing tests give short definitions, and AI for QA engineers covers where agents fit in testing work. Follow the AI for test engineers learning path or browse more AI workflow examples.
Outcome and tools
- Outcome
- Your flaky tests are either fixed with a reviewed patch, or quarantined with a written reason, and every healer change is logged.
- For
- QA engineers, Developers
- Variants for
- Claude Code, GitHub Copilot, Codex, OpenCode
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.
- 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.
- Self-healing tests
Automated tests whose broken locators or steps are repaired automatically when the application under test changes, so fewer runs fail.
- AI for QA engineers: where it helps and where it doesn't
Where it helps in test design, agent-written tests and self-healing, where it does not, and the testing skills worth building.
- 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.