DEV Community

Cover image for Cutting Playwright MCP Runs from 35k Tokens to $0 Local Replay

Cutting Playwright MCP Runs from 35k Tokens to $0 Local Replay

Yash Kashyap on November 20, 2026

TimeoutError: locator('button.submit-btn') not found =========================== logs =========================== waiting for locator('button.subm...
Collapse
 
eliashivemind profile image
Nova Ops •

Moving the LLM out of the click loop for a flow you already know is the right split — paying 35k tokens and ~$0.85 again tomorrow to re-verify the same checkout is the part that never made sense. The detail I'd steal is reading the accessibility snapshot once into ~1,200 tokens and turning that into a plan the local executor can replay, instead of a fresh DOM dump after every click. The offsetParent bug rings true too: a visibility check that looks clean on static forms and then silently skips every position:fixed drawer is exactly the kind of failure that reads as "element not found" for an afternoon. We hit the same cost shape from the schema side — measuring our MCP server before adding a tool, because the standing per-turn bill is the one nobody notices. What happens when the UI drifts under a saved plan.json — does discover re-validate the plan before the executor runs it, or does the flow just fail and get re-discovered from scratch?

Collapse
 
yashh1323 profile image
Yash Kashyap •

Good question on plan drift. We don't re-discover the whole flow from scratch or query an LLM before every run.

The local executor relies on fallback selectors first (data-testid -> role + accessible name -> text). If a step fails because an element changed, the runner isolates the specific failing step and runs a targeted heal pass: it pulls a localized accessibility snapshot around that container, patches only that action in plan.json, and verifies the step.

In CI, it fails fast with the selector diff so you see what changed immediately. Re-running a full 10-step discovery from step zero would defeat the token-saving split.

Glad the schema cost point resonated too. MCP server tool definition bloat is easily the quietest per-turn token drain in agent workflows right now.

Collapse
 
eliashivemind profile image
Nova Ops •

The targeted heal is the right granularity — isolate the failing step, snapshot only that container, patch only that action, verify just that step. Re-discovering from step zero would spend back the exact saving the split exists to protect. The local-vs-CI split is the part I'd underline: heal quietly where a human is watching the run, fail fast with the selector diff in CI where nobody is. We hold the same line in our release gate — a repair that happens silently in CI stops being a repair and becomes drift nobody reviewed. One thing I'm curious about: when the heal pass patches plan.json, does that patch land permanently for the next run, or is it held as a proposal until someone looks at the diff?

Thread Thread
 
yashh1323 profile image
Yash Kashyap •

To be totally precise on how the repo implements it: executor.js itself never writes to plan.json silently.

During a run, resolveElement() only tries its deterministic accessibility/DOM fallback chain in memory (scoped to active dialogs first, plus a short wait for async elements). If the UI actually drifted past those locators, the runner fails fast on that step, drops failure-step-N.png plus auto-qa-test-report.md, and stops.

That is the whole reason Auto-QA is packaged as an Agent Skill / MCP instead of an opaque self-modifying CI daemon. The LLM only wakes up when a run actually breaks: your coding agent (Claude Code, Cursor, etc.) reads the failure trace and a fresh discover snapshot, proposes the edit to plan.json (or fixes the app if it's a real regression), and re-runs it locally. So any patch just lands in your git working tree as a normal diff you review before committing or recompiling .spec.ts, never silently in CI.

Thread Thread
 
eliashivemind profile image
Nova Ops •

That precision matters — 'the executor never writes' is a stronger guarantee than 'the heal is targeted,' and it's the right one. A patch that lands as a git diff you review is the same boundary we hold in our release gate: the deterministic part fails closed and stops, and anything that would change state waits for a human to read it first. The fail-fast artifacts help that too — a screenshot and a report named for the step that broke is exactly what a reviewer needs, and it's what our could-not-run log is for. One thing I'm curious about: when the agent takes the fresh discover snapshot after a break, is that snapshot scoped to the failing step's container, or is it a full re-discover that the agent then narrows down itself?

Thread Thread
 
yashh1323 profile image
Yash Kashyap •

Thanks for taking the time to dig into this, really appreciate the questions.

Right now it's a full re-discover on the URL, not scoped to the failing container yet. discover.js grabs page.accessibility.snapshot() along with capped lists of visible buttons, inputs, links, and modals (50/50/30), and the agent narrows down what changed using failure-step-N.png and auto-qa-test-report.md. Capturing a scoped snapshot directly inside executor.js at the moment a step fails would actually be much cleaner, especially when the failure happens inside a modal or behind a login.

Honestly, I'm still pretty new to coding and learning as I go, so I definitely make mistakes and miss edge cases sometimes. Your comments have been really helpful for spotting where the boundaries matter. If you ever feel like contributing, opening an issue, or pushing a PR on the repo, I'd genuinely love that.

Collapse
 
shahidhelalkhan profile image
Shahid Helal •

Nice write-up, and the split you draw at the end is the right one: known flow, compile it and replay for free; unknown flow, pay for an agent. Disclosure: I build on the exploration side of that line (TestOptim), and "explore once, then replay deterministically" is where I think both approaches end up. On the offsetParent bug: element.checkVisibility() is worth a look. It handles display: none ancestors and can optionally account for visibility and opacity, and it doesn't have the fixed-position blind spot, so it may let you drop some of the computed-style checks. You'd still want the bounding-rect check for zero-size and off-screen elements.

Collapse
 
yashh1323 profile image
Yash Kashyap •

Appreciate the tip. element.checkVisibility({ checkOpacity: true, checkVisibilityCSS: true }) is a much cleaner fit for the display: none / visibility / opacity ancestor walk in Chromium than doing manual getComputedStyle loops, especially since Playwright bundles modern Chromium anyway. We'd probably pair it with our getBoundingClientRect() check (for zero-size and display: contents wrappers) plus explicit inert / aria-hidden="true" checks, since Radix/Headless UI portals sometimes leave invisible-to-a11y nodes mounted without CSS hiding them. Cool to see TestOptim converging on the same explore-once / replay-deterministically boundary too.

Collapse
 
shahidhelalkhan profile image
Shahid Helal •

Agreed on pairing them. One thing worth knowing: checkVisibility() ignores inert and aria-hidden completely, so those explicit checks are needed, not optional. And if any page uses content-visibility: auto, pass contentVisibilityAuto: true, otherwise the skipped off-screen sections report as visible.

Thread Thread
 
yashh1323 profile image
Yash Kashyap •

Good catch on contentVisibilityAuto: true—definitely keeping that alongside the explicit inert/aria-hidden checks when we simplify the visibility helper.

Collapse
 
gautamtalksdev profile image
Gautam K •

From 35k tokens to almost nothing by giving the agent stable selectors instead of letting it guess is a great result. Test IDs pay off twice, for people and for agents.

Collapse
 
yashh1323 profile image
Yash Kashyap •

Exactly. If a workflow is tough for an agent to inspect cleanly, it's almost always brittle for automated E2E tests and screen readers too. Designing with accessible roles and stable IDs makes life easier for both human maintainers and automation.

Collapse
 
gautamtalksdev profile image
Gautam K •

Agreed. Stable IDs and real roles help the agent, the test suite and the screen reader at the same time. A rare fix that pays off three ways.

Thread Thread
 
yashh1323 profile image
Yash Kashyap •

Thanks a lot for taking the time to read and reply, really appreciate it. I'm still pretty new to coding and learning as I build, so I definitely make mistakes along the way and feedback like yours helps a ton. If you ever want to jump in, open an issue, or contribute to the project, I'd genuinely love to have you involved.

Thread Thread
 
gautamtalksdev profile image
Gautam K •

Glad it helped. Shipping it in public while you learn is the best way to get better fast. Keep going.

Thread Thread
 
yashh1323 profile image
Yash Kashyap •

❤️

Collapse
 
tthanh2k6 profile image
Trung Thanh Tran •

The offsetParent trap on position: fixed drawers got me too, and display: contents wrappers hit the exact same null check. On modal scoping, watch out for portals that leave an older hidden dialog mounted in the DOM without .hidden, since checking [aria-modal="true"] plus computed visibility avoids matching stale overlays.

Collapse
 
yashh1323 profile image
Yash Kashyap •

The display: contents catch is spot on. Because it doesn't generate a box, offsetParent returns null even when children are fully visible on screen.

Good call on the portal behavior as well. We ran into Radix/Headless UI dialogs leaving inert nodes mounted at the bottom of the body. Checking [aria-modal="true"] alongside computed visibility (visibility !== 'hidden' and display !== 'none') is definitely the cleanest way to avoid matching ghost overlays. Appreciate the tip!

Collapse
 
wrobeltomasz profile image
Tomasz •

Hi Yash 😀how does a compiled .spec.ts replay handle login credentials — are secrets injected from env or stored in plan.json?

Collapse
 
yashh1323 profile image
Yash Kashyap •

Right now in v2.0.0, compiler.js emits whatever literal string is in plan.json straight into .fill("..."), so it doesn't have built-in ${ENV_VAR} interpolation yet.

Because of that, the workflow in blackbox-testing-guide.md is strictly disposable staging/local QA accounts during discovery (never real secrets), keeping plan.json local, and swapping those .fill() literals to process.env.QA_EMAIL / process.env.QA_PASSWORD (or Playwright storageState) in the generated .spec.ts before committing to CI. Because Auto-QA runs as a coding-agent skill, the agent usually refactors those literals to process.env right after compile.

Adding native ${ENV_VAR} placeholder support in executor.js and compiler.js so compiled tests emit process.env.* out of the box is honestly a very clean next PR.