Heal a Broken Test
Why This Matters
A red test asks one question: is the application wrong, or is the test wrong? A button got a new id, a page loads a bit slower: the test is wrong, and fixing it is maintenance. The cart total is off by a cent: the application may be wrong, and “fixing” the test hides a bug.
The healer agent repairs tests quickly and well. That is exactly why it is dangerous: a model that wants to make the test pass will happily change the expected value too. This lesson shows the healer at work on a real repair, and the guardrails that make it stop and ask, or refuse, when a fix changes what the test checks.
What you will learn
- The healer’s workflow: run, debug, inspect, fix, run again
- A real heal, line by line: what changed and why
- How the healer classifies every change: locator, wait, assertion, step, other
- What happens to that heal under
explore,guidedandstrict - The rules no heal may break, under any profile
- When the right answer is not a fix but a bug report
The healer’s tools
| Tool | What it does |
|---|---|
healer_run_tests | Runs the suite with the manifest’s run command (seed tests first) and returns the results, linked to the plan (lesson 14) |
healer_debug_test | Runs one test verbosely, with more of its output |
healer_inspect_page | Checks a list of locators against the live page: found, or missing with a suggested replacement |
healer_fix_test | Writes the fix, after checking it against the rules, the heal policy and (in strict) the locked locators; keeps a .bak backup |
verify_test_stability | Proves the fixed test is stable and fails for the right reason (lesson 17) |
Plus the browser tools: capture_page, take_screenshot, console_logs, network_monitor.
A real heal
Scenario 2.3 of the shop plan checks the required billing fields: open the checkout, click Place Order without filling anything in, and expect the browser to ask for the first name. This was the test before the heal:
"""Plan 2.3: Required billing fields (specs/shopping-cart-and-checkout.md)."""
import time
from selenium.webdriver.common.by import By
from conftest import tid
def test_required_fields(shop):
# 1. Open the checkout with a product in the cart
shop.add_to_cart(10)
shop.wait_for_cart(1, "$199.99")
shop.open_checkout()
# expect: The Billing Information, Payment Method and Order Summary sections are shown
for section in ("Billing Information", "Payment Method", "Order Summary"):
assert shop.driver.find_element(By.XPATH, f"//h3[normalize-space()='{section}']").is_displayed()
# 2. Click "Place Order" without filling anything in
shop.click((By.ID, "placeOrderBtn"))
time.sleep(1)
# expect: The order is not placed
assert not shop.driver.find_elements(*tid("payment-success"))
# expect: The browser asks for First Name, the first required field
first_name = shop.find("billing-firstname")
assert first_name.get_attribute("required") is not None
assert shop.driver.execute_script("return arguments[0].validity.valid;", first_name) is False
assert shop.driver.execute_script("return arguments[0].validationMessage;", first_name)
It failed: #placeOrderBtn matched nothing. The healer ran it with healer_debug_test, checked the page with healer_inspect_page, and found the button under its test id, [data-testid="place-order"]. It also noticed the time.sleep(1), which is not allowed (lesson 13). Its fix, saved by healer_fix_test, with the old version kept as test_required_fields.py.bak:
"""Plan 2.3: Required billing fields (specs/shopping-cart-and-checkout.md)."""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from conftest import tid
def test_required_fields(shop):
# 1. Open the checkout with a product in the cart
shop.add_to_cart(10)
shop.wait_for_cart(1, "$199.99")
shop.open_checkout()
# expect: The Billing Information, Payment Method and Order Summary sections are shown
for section in ("Billing Information", "Payment Method", "Order Summary"):
assert shop.driver.find_element(By.XPATH, f"//h3[normalize-space()='{section}']").is_displayed()
# 2. Click "Place Order" without filling anything in
shop.click(tid("place-order"))
# expect: The browser asks for First Name, the first required field
first_name = shop.find("billing-firstname")
assert first_name.get_attribute("required") is not None
WebDriverWait(shop.driver, 5).until(
lambda d: d.execute_script("return arguments[0].validationMessage;", first_name)
)
assert shop.driver.execute_script("return arguments[0].validity.valid;", first_name) is False
# expect: The order is not placed
assert not shop.driver.find_elements(*tid("payment-success"))
Read the diff the way a reviewer would:
-import time
-
from selenium.webdriver.common.by import By
+from selenium.webdriver.support.ui import WebDriverWait
...
- shop.click((By.ID, "placeOrderBtn"))
- time.sleep(1)
- # expect: The order is not placed
- assert not shop.driver.find_elements(*tid("payment-success"))
+ shop.click(tid("place-order"))
# expect: The browser asks for First Name, the first required field
first_name = shop.find("billing-firstname")
assert first_name.get_attribute("required") is not None
+ WebDriverWait(shop.driver, 5).until(
+ lambda d: d.execute_script("return arguments[0].validationMessage;", first_name)
+ )
assert shop.driver.execute_script("return arguments[0].validity.valid;", first_name) is False
- assert shop.driver.execute_script("return arguments[0].validationMessage;", first_name)
+ # expect: The order is not placed
+ assert not shop.driver.find_elements(*tid("payment-success"))
Four things happened:
- A locator changed:
#placeOrderBtnbecame the test idplace-order. That is maintenance. - The sleep became a wait for the condition the next lines need: the browser has a validation message for the first name.
- An assertion moved into that wait:
assert ...validationMessageis gone; the wait now times out if there is no message. Still a check, but a different one. - A check moved: “the order is not placed” is now checked after the browser complained, instead of one second after the click. That is better: checking for the absence of something right after a click proves little.
All four are improvements. But only the first is “just a locator”. The others change how the test checks, and one changes what it checks.
How the healer classifies a fix
healer_fix_test compares the fix with the current test, line by line, with locators and timeouts masked out, and labels every change:
| Kind | Example |
|---|---|
locator | (By.ID, "placeOrderBtn") → tid("place-order") |
wait | A wait added, removed, or its timeout or locator changed |
assertion | An assertion or expected value added, removed or changed; also an expected value inside a wait (wait_for_cart(1, "$89.99")) |
step-removed / step-added | An action on the page (click, type, navigate) removed or added |
import, cosmetic | Imports; whitespace and comments |
other | Anything else: helpers, variables, logic |
For this heal the classifier reports one locator change, one wait, one assertion (the removed validationMessage assert), and three “other” lines (the removed sleep, and the lambda and closing bracket of the new wait).
The same heal under each profile
The heal policy of the active profile (lesson 16) decides what the healer may write on its own.
explore (heal policy free). The fix is written. The healer reports what it changed.
guided (assertions-need-approval). The fix touches an assertion, so it is not written until you agree:
Fix not written: under the heal policy "assertions-need-approval" (profile) this fix needs the user's approval,
because it changes more than locators and waits:
line 27 (removed, assertion / expected value): assert shop.driver.execute_script("return arguments[0].validationMessage;", first_name)
Show these lines to the user and explain why the test (not the app) is wrong. Only if the user approves it in the
chat, call healer_fix_test again with their words as "userApproval". If the app is broken, report it instead of
healing the test.
The healer now has to explain to you why the test, not the app, is wrong. Here that is easy: the check moved into the wait. You reply “OK, the validation message check moved into the wait, that’s fine”, and the healer calls healer_fix_test again with your words as userApproval. Under guided your words go into the audit log with the heal.
strict (locators-only). The fix is refused outright:
Fix not written: the heal policy "locators-only" (profile) lets a heal change only locators and waits.
This fix changes what the test checks or does:
line 27 (removed, assertion / expected value): assert shop.driver.execute_script("return arguments[0].validationMessage;", first_name)
If the app is broken, report it: do not heal the test. If the expected behaviour really changed, update the test
plan (planner_save_plan), have the user approve it, and generate the test again with generator_write_test.
Under strict a heal is maintenance, nothing more. The healer could still write the locator part of this fix alone. Anything that changes how the test checks goes back through the plan.
What no heal may do, under any profile
Some fixes are refused whatever the profile, because they break Selenium’s rules for AI agents (lesson 13):
- A sleep. The healer’s message: find the condition the test waits for instead.
- A longer timeout. A fix that raises a wait above the test’s old maximum is refused (
no-longer-timeout). That rule is the healer’s own: it compares the fix with the test as it was. - Selenium 3 APIs, driver paths, driver managers,
DesiredCapabilities.
And one more check, under guided and strict: if the test file was changed outside the agent (edited by hand, or by another AI tool) since the agent last wrote it, the healer refuses to heal it until you confirm that edit. Otherwise the policy would compare the fix with a version nobody approved.
Bug or change? The question only you can answer
The healer agent’s own instructions list the options for a wrong expected value: update it if the application changed correctly, fix the selector if it pointed at the wrong element, report a bug if the application is broken, and mark a test as skipped (with a reason) when a feature is genuinely broken.
Take the totals test from lesson 14. The shop now shows Tax (21%): $60.89 where the plan says $60.90. Possible causes:
| Cause | Right action |
|---|---|
| The shop now rounds the tax down: a bug | Report it. The test is right; it stays red. |
| The business decided to round down: a change | Update the plan, have it approved, regenerate the test. |
| The test reads the wrong element | Fix the locator: maintenance. |
Only the third is a heal. The first two are decisions about the application, and they are yours. Under explore nothing stops the healer from “fixing” the first case; that is why explore is for trying things out, and a team suite runs at least guided.
Exercise
Start from the generated test for scenario 1.1 (lesson 7). Simulate a UI change: edit the test so it uses a selector that no longer exists, By.ID, "cart-btn", instead of the shop’s cart button.
- Ask the healer to run and fix the test under
explore. What did it change? Check the.bakfile. - Switch to
guided(“switch the profile to guided”; lesson 16 explainsagent_profile). Restore your broken version and ask again. What happens, and why? (Hint: who edited the file last?) - Now ask the healer to “fix” the test by changing the expected total to
$0.00. What doesguidedsay? Understrict?
Show suggested answer
- Under
explorethe healer finds the cart button withhealer_inspect_page, changes the locator back to the test id, and writes the fix. The.bakfile holds your broken version. - Under
guidedthe healer first refuses because you edited the file by hand after the agent wrote it: “changed since the generator or healer last wrote it”. It asks you to confirm the edit. After your confirmation (asuserApproval) it heals; the change is a locator change, whichguidedallows without asking. - Changing an expected value is an assertion change.
guidedrefuses without your approval and asks the healer to explain why the test, not the app, is wrong; there is no good explanation, so the right answer is to say no.strictrefuses whatever you say: change the plan and regenerate instead. This holds also when the expected total sits insideshop.wait_for_cart(...): an expected value in a wait is a check too.
Exam-style questions
1. A heal changes assert cart == "1 items" into assert cart == "0 items". Under guided, what happens?
- A. The fix is written; it is only a text change
- B. The fix is refused until the user approves it in the chat
- C. The fix is refused whatever the user says
- D. The test is deleted
Show answer
strict it would be C.2. A test times out after 10 seconds. The healer proposes WebDriverWait(driver, 20). What happens?
- A. Allowed under
explore, refused understrict - B. Refused under every profile: a longer timeout hides the race
- C. Allowed: waits are always allowed
- D. Allowed with the user’s approval
Show answer
no-longer-timeout is a rule, not a policy. Wait for the condition that was not true yet.3. The cart total in a test fails by one cent after a release. What should the healer do first?
- A. Change the expected value so the build is green
- B. Find out whether the application changed on purpose or is wrong, and ask the user; report a bug if the app is wrong
- C. Increase the timeout
- D. Skip the test without a reason