English

Self-healing test scripts: how to repair a test without hiding a bug

On this page

Self-healing test scripts repair a broken step, usually a locator that no longer matches, so a UI change does not turn the whole suite red. The repair is safe only when it changes how the test finds an element and never what the test checks, and when a person can see the evidence for it.

This guide explains why selectors break, the two ways tools heal, and how to review a repair.

Why do test selectors break?

Before you heal anything, name the cause. Most breakages fall into six groups:

Cause Example Right response
Visible text changed “Checkout” renamed to “Continue to payment” Update the locator to the new name
Structure changed A button moved into a new wrapper div Use a role or label locator that ignores structure
Generated attributes Class names rebuilt on every deploy Stop using them; use roles or a test id
Timing The element appears after a request finishes Wait with a web-first assertion, not a sleep
Data The test expected a product that no longer exists Create the data in the test or a fixture
Real regression The button is gone because of a bug Do not heal. File the defect

Only the first two are true locator repairs. Generated attributes are a design problem in the test. Timing and data look like broken locators but are flakiness. And the last row is the reason healing needs rules: a missing button is sometimes exactly what the test should catch.

How do you make selectors resilient before anything breaks?

The cheapest repair is the one you never need. Playwright’s documentation recommends user-facing locators and explicit contracts, and says CSS and XPath are not recommended because the DOM changes often. A practical order:

  1. getByRole with an accessible name: getByRole("button", { name: "Pay now" }).
  2. getByLabel for form fields.
  3. getByText for non-interactive content.
  4. getByTestId where no user-facing name is stable, agreed with developers as a contract.
  5. CSS or XPath only as a last resort, scoped to a small region.

Roles and labels also survive refactors that keep the page accessible, so a test that breaks on them often signals a change users would notice.

How do self-healing tools repair a test?

There are two models, and the difference matters for review:

  • Runtime healing. When a locator fails during a run, the tool finds a likely replacement from other attributes and continues. BrowserStack’s documentation describes this and notes that a heal keeps a single run green; it recommends replacing locators in your script with the healed ones. Until you do, the test code still holds the broken locator.
  • Code repair. The tool, or an agent, reproduces the failure, inspects the page and proposes a change to the test file. Playwright’s healer agent works this way: it replays failing steps, inspects the UI and suggests a patch, and may skip a test if it believes the functionality is broken.

Code repair is easier to trust because it produces a diff. A diff can be reviewed, committed and reverted like any other change.

What makes a repair safe?

Hold every repair, human or automated, to four rules:

  1. Assertions are untouchable. A repair must not delete, weaken or change an expect. If toHaveText("Your card was declined.") becomes toBeVisible(), the test now checks less, and that is a test change, not a repair.
  2. The repair comes with evidence. “Same button on /cart, label changed from Checkout to Continue to payment” is evidence. “Found a similar element” is not.
  3. The target is the same element. The healed locator must point at the element the test meant, not at a neighbour that also matches.
  4. It lands as a reviewed change. The fix is written to the test file and reviewed, so the next run does not depend on healing again.

A safe repair looks like this:

  await page.goto("/cart");
- await page.getByRole("button", { name: "Checkout" }).click();
+ await page.getByRole("button", { name: "Continue to payment" }).click();
  await expect(page.getByText("Order confirmed")).toBeVisible();

One locator changed, the assertion untouched, and a reason anyone can verify by opening the page.

Example A repair that is safe to accept

  await page.goto("/cart");
- await page.getByRole("button", { name: "Checkout" }).click();
+ await page.getByRole("button", { name: "Continue to payment" }).click();
  await expect(page.getByText("Order confirmed")).toBeVisible();
Changed
How the test finds the button
Unchanged
What the test checks
Only the locator changes. If a line with expect had changed, the repair would be a test change and should be rejected.

How do you review a healed test?

Ask five questions before you approve:

  • Did any line starting with expect change? If yes, reject the repair and treat it as a test change.
  • Does the evidence match what you see on the page?
  • Did the product change on purpose? Check the ticket or release notes for the rename.
  • Does the new locator follow your locator order, or did the repair fall back to CSS?
  • Does the repaired test still fail when you break the behavior on purpose?

If the answer to the third question is “no, nobody meant to change that”, you have found a bug, and the healer just tried to hide it.

How do you tell a broken test from a flaky one?

A broken test fails every time on the same revision. A flaky test passes and fails on the same revision. Rerun the failing test several times on one commit, for example with Playwright’s --repeat-each option, before you decide.

Flaky tests need a different response from broken ones:

  • Find the cause: a fixed wait, shared data, test order or an unstable environment.
  • If you cannot fix it now, quarantine it with a reason, an owner and an expiry date, so it stops blocking the pipeline without being forgotten.
  • Fix the cause, then remove it from quarantine. Do not “heal” a flaky test by loosening its assertion.

The same discipline applies to tests an agent wrote in the first place; the AI test generation for Playwright guide has a review checklist for new tests.

How can an AI test agent help with repairs?

An AI test agent is well suited to the tedious part of self-healing test scripts: reproducing the failure, comparing the page with the step and drafting the diff. The rules above stay with the reviewer. Probeflow Desktop, a local-first AI test agent that heals broken test scripts with evidence, currently in preview, builds them into the app: it rejects a repair that deletes, softens or changes an assertion. It marks a test as flaky after it passes and fails on the same revision, and can quarantine it with a reason, an owner and an expiry. By default it asks before every consequential action, as described in permission modes and review points for the agent. Which model sees your code is covered in the local AI test agent guide.

Sources

Frequently asked questions

What are self-healing test scripts?

They are automated tests whose broken steps are repaired automatically, usually by finding the intended element another way after its locator stops matching. Some systems patch the step only during the run; others propose a change to the test file.

Can self-healing hide real bugs?

Yes, if a repair is allowed to change what the test checks or to pick a different element that happens to match. A safe repair changes only how a step finds an element and leaves every assertion as it was.

Is a broken test the same as a flaky test?

No. A broken test fails every time on the same revision, usually because the page changed. A flaky test passes and fails on the same revision, which points to timing, shared data or the environment, and needs a different fix.

Should healed locators be written back to the test code?

Yes. A repair that only lives in one run has to be found again next time and is never reviewed. Writing it back as a normal code change lets a person check it and keeps the suite honest.