5. Web UI tests: Playwright QA¶
AI assistants write Playwright tests quickly, but they guess selectors. The test looks right, then fails on the first run, or breaks the next time someone renames a CSS class. k11 Playwright QA gives the agent the facts it can't guess, and keeps the suite working after a redesign.
| It does | So that |
|---|---|
Verified locators: opens the real page and returns locators that match exactly one element, most stable first (getByTestId, getByRole, getByLabel…) |
tests don't fail on their first run |
| Story → spec: turns a user story, Gherkin or acceptance criteria into a Playwright Test skeleton, after reviewing the story (Chapter 4) | tests check what was agreed |
| Structured runs: per-test results, including the exact locator a failing test was waiting for | failures point at the cause |
| Self-healing: after a UI change, finds the element the test meant and suggests a verified replacement with a confidence score, the reasons and the runner-up | redesigns don't cost a week of test fixing |
| Mobile web: every page tool takes a device profile (iPhone, Pixel, Galaxy, iPad…) | the mobile layout is tested, not just the desktop one |
Set up¶
npx -y k11-playwright-qa-mcp install-browsers # once per machine: the Chromium build the server uses
Install the plugin (/plugin install playwright-qa@k11-qa-tools, Chapter 3). Your project needs @playwright/test.
From story to passing test¶
/story-to-test http://localhost:3000/login As a customer, I want to sign in so that I can see my orders.
- Valid credentials show "Welcome back"
- A wrong password shows "Invalid email or password"
The command runs this workflow:
review_user_story: the questions a careful tester would ask. If there are any, the agent asks and waits.parse_user_story: scenarios and a skeleton, onetest()per scenario and onetest.step()per step.explore_page: verified locators for the elements each step needs.write_test: writes the spec intotests/(it refuses paths outsidetests/and won't overwrite without being asked).run_tests: runs it and fixes what fails, using the failing locator the result names.
If you told it to go ahead with open questions, each becomes an // OPEN QUESTION: comment in the spec and is listed in the
final report.
Pages behind a login¶
Log in once and save the session:
save_login_state { url: "https://shop.example.test/login",
steps: [ { action: "fill", locator: "page.getByLabel('Email')", value: "${ENV:APP_USER}" },
{ action: "fill", locator: "page.getByLabel('Password')", value: "${ENV:APP_PASSWORD}" },
{ action: "click", locator: "page.getByRole('button', { name: 'Sign in' })" } ],
name: "default" }
That writes .qa-mcp/auth/default.json (and adds .qa-mcp/auth/ to .gitignore: never commit it). Every page tool then
accepts a session with that storage_state, plus up to 25 steps (goto, click, fill, press, select, check,
uncheck, wait_for) to reach a page deeper in a flow. Secrets are written as ${ENV:NAME} and masked in every result.
Self-healing¶
Healing compares the broken page with a fingerprint taken while the test passed, not with a guess.
- Baseline. While tests pass,
capture_baselinerecords each element's role, accessible name, test id, id,name, type, label, placeholder, classes, text and href in.qa-mcp/baselines.json. Commit this file: CI heals against it. - Detect.
run_testsreports the locator a failing test was waiting for. - Match.
heal_locatorscores every visible element on the current page against the fingerprint: exact matching for stable attributes, synonym-aware fuzzy matching for text and ids ("Sign in" ≈ "Log in",btn≈button). - Verify. The best match gets the most stable locator that matches exactly one element on the live page.
- Decide.
healedonly when confidence is at least 0.55 and clearly ahead of the runner-up; otherwiseneeds_reviewwith the top candidates, ornot_found.
A real result, after a redesign renamed every id and changed "Sign in" to "Log in":
{
"status": "healed",
"original": "page.locator('#login-btn')",
"suggestion": "page.getByRole('button', { name: 'Log in' })",
"confidence": 0.87,
"reasons": ["name equivalent: \"Sign in\" → \"Log in\"", "id similar (50%)", "role matches", "type matches"],
"candidates": [
{ "locator": "page.getByRole('button', { name: 'Log in' })", "score": 0.87 },
{ "locator": "page.getByRole('button', { name: 'Continue with Google' })", "score": 0.42 }
]
}
It chose the right button over a look-alike, and the replacement is a role-based locator that would have survived the redesign in the first place.
Healing in CI: heal-ci¶
npx -y k11-playwright-qa-mcp heal-ci # dry run: report suggestions only
npx -y k11-playwright-qa-mcp heal-ci --apply # edit the specs
- Runs the suite.
- For each locator failure, heals against the committed baseline on the page the test was on (from
.qa-mcp/heal.config.json, or the test's ownpage.goto), logging in first if the config says so. - Applies a change only at confidence 0.7 or more (
--min-confidence) and clearly ahead of the runner-up. - Re-runs the healed tests and reverts any change whose test still fails, in every Playwright project.
- Leaves failures that aren't about locators alone: they may be real bugs.
- Writes
.qa-mcp/heal-report/heal-report.md(a pull request description) andheal-report.json(the evidence).
| Option | Default |
|---|---|
--apply |
dry run |
--min-confidence <n> |
0.7 |
--config <file> |
.qa-mcp/heal.config.json |
--report-dir <dir> |
.qa-mcp/heal-report |
--no-verify |
re-run healed specs (don't skip it) |
The repository's docs/heal-locators.yml runs it on a schedule and opens the pull request; docs/heal.config.example.json shows
how to log in.
Mobile web¶
Phones get a different layout: a hamburger menu, hidden columns, separate buttons. Every page tool takes a device (any of
Playwright's built-in profiles; list_devices lists them):
explore_page { url, device: "iPhone 15" } the elements a phone user actually sees
check_locator { url, locator, devices: ["desktop", "iPhone 15", "Pixel 7"] } unique and visible on every device?
heal_locator { ..., device: "Pixel 7", check_devices: ["desktop"] } warns if a mobile fix breaks desktop
Baselines are kept per device. In heal-ci, each failure is healed on the device its Playwright project emulates, the devices
must agree on one replacement, and a change is kept only if the test then passes in every project. Pages are emulated in
Chromium; keep a WebKit project for Safari-specific rendering. Native apps need Appium (Chapter 7).
Evidence for the gate¶
heal-report.json is the ui_heal evidence kind. Under the default rules:
| Rule | Outcome | When |
|---|---|---|
ui.real_failures |
block | UI tests failed for reasons other than locators |
ui.unresolved_locators |
block | broken locators that weren't safely healed (reverted, needs review, not found) |
ui.healed |
conditional | locators were healed and verified: approve the heal pull request |
ui.suggested |
conditional | heals were suggested in a dry run but not applied |
In a pull request pipeline, run heal-ci without --apply: the gate then sees what's broken and what could be healed,
and nothing edits the branch under review.