Playwright Flaky Selector Auditor & Auto-Heal CLI Kit
Offline zero-dependency Python CLI for JS/TS & Python Playwright flakiness audits, SARIF 2.1.0 CI gates, and two-stage DOM locator healing.
Playwright test suites rarely fail because the application is broken. Far more often, CI turns red because a CSS-in-JS hash changed from .css-1x2y3z to .css-9k4m1p, a new wrapper <div> broke div > button:nth-child(2), or a waitForTimeout(1500) race lost to a slower CI runner. AI-powered PR agents can fix these tests automatically, but many engineering teams cannot ship proprietary DOM snapshots and internal test suites to third-party LLM endpoints.
Why this problem wastes time
Three subtle gaps break naive grep-based Playwright linters in real repositories:
- Multiline chains and comment false positives. Real Playwright tests wrap across lines (
await page\n .frameLocator('#pay')\n .locator('button.css-1x2y3z')\n .click()) and mention forbidden patterns inside comments. A line-by-line regex misses the multiline call while firing on comments, and blindly rewriting.locator(...)drops theframeLocator('#pay')receiver chain. - The broken selector paradox. If a selector already failed in CI because a class hash was renamed, an exact
querySelectorlookup against the captured DOM returns zero elements and suggests nothing. You need a two-stage resolver: exact CSS matching for upgrading live brittle selectors, plus fuzzy token healing against interactive ARIA elements when a selector returns zero matches. - Ambiguous replacements. Suggesting
getByRole('button', { name: 'Save' })when a page contains twoSavebuttons causes Playwright strict-mode failures on the very next run unless you check DOM uniqueness first.
Step-by-step workflow
A deterministic offline healing pipeline works in three stages:
- Scan tests with a comment-safe, balanced-parenthesis parser so multiline locators, template strings, and receiver chains stay intact across both JS/TS (
*.spec.ts) and Playwright Python (*_test.py). - Capture a DOM snapshot on failure via Playwright's
afterEachhook (page.content()), keeping all inspection local. - Resolve replacements using WAI-ARIA accessible-name priority (
aria-label->aria-labelledby-><label for>-> ancestor<label>-> element text) and verifymatches_in_dom === 1.
Here is a self-contained Python 3 snippet using the standard library that masks JS/TS comments while preserving line offsets and extracts multiline .locator(...) spans:
import re
def mask_js_comments(source: str) -> str:
chars = list(source)
n, i = len(source), 0
while i < n:
ch = source[i]
if ch in ('"', "'", "`"):
q = ch
i += 1
while i < n:
if source[i] == "\\":
i += 2
continue
if source[i] == q:
i += 1
break
i += 1
continue
if source[i : i + 2] == "//":
while i < n and source[i] != "\n":
chars[i] = " "
i += 1
continue
if source[i : i + 2] == "/*":
chars[i : i + 2] = [" ", " "]
i += 2
while i < n and source[i : i + 2] != "*/":
if chars[i] != "\n":
chars[i] = " "
i += 1
if i + 1 < n:
chars[i : i + 2] = [" ", " "]
i += 2
continue
i += 1
return "".join(chars)
Common pitfalls & edge cases
- HTML5 void tags inside
<label>: When parsingpage.content()dumps in Python'shtml.parser, unclosed void elements such as<input>,<br>, and<img>corrupt the tag stack unless explicitly tracked, breaking<label>Coupon <input></label>accessible-name lookup. - Plain
<a>vs<a href>: An anchor without anhrefattribute does not expose an implicitlinkARIA role; treating it as alinkgeneratesgetByRole('link')locators that fail in Playwright. - HTML entities and whitespace: Accessible names such as
Complete Order & Paymust be unescaped (html.unescape) and whitespace-collapsed before generatinggetByRoleoptions. - SARIF 2.1.0 CI integration: Standardizing findings into SARIF lets GitHub Code Scanning annotate pull requests directly without running external browsers during static lint checks.
Automating it in seconds
Use the Free Browser Sandbox on the tool page to paste a single Playwright TypeScript snippet alongside an HTML DOM snapshot, flag PW001–PW007 flakiness issues, verify DOM uniqueness (matches_in_dom), and preview unified patch diffs or export Markdown/JSON reports.
When you want repository-wide automation, the Pro Offline Bundle delivers pw_audit.py (recursive JS/TS + Playwright Python scanning, SARIF 2.1.0 output, and safe --apply patching with .bak backups), trace_diff.py (two-stage exact + fuzzy DOM resolver), capture_dom_on_fail.ts, and a ready-to-drop .github/workflows/playwright-audit.yml CI workflow—all offline with zero external dependencies.