axe-playwright injectaxe failed page closed crash error
Fixes axe-playwright injectAxe page-closed errors by correcting test sequencing and ruling out tab crashes. Use it when injectAxe fails because the page is gone. Not for violations found after a successful injection.
axe-playwright injectaxe failed page closed crash error - how to fix it
TL;DR
Stop the page from closing under the test: await every navigation properly, do not close the context before injectAxe resolves, and rule out a renderer crash from a huge DOM. The page closed error means Playwright lost the target between actions. One line of why: injectAxe needs a live page, and if the test moved on or the renderer crashed, there is no page to inject into.
The error, verbatim
Error: injectAxe failed: page closed
at injectAxe (node_modules/axe-playwright/dist/index.js)
at test (tests/a11y.spec.js:12:5)
Fix it step by step
Step 1: Reproduce with trace
npx playwright test tests/a11y.spec.js --trace on | tail -10Expected: The page closed error repeats at the injectAxe call.
Step 2: Check for missing awaits
rg -n 'goto|click|injectAxe' tests/a11y.spec.js | head -15Expected: Shows the action sequence; a missing await before injectAxe is the classic bug.
Step 3: Verify the page is not crashing
npx playwright test tests/a11y.spec.js --headed | tail -5Expected: Headed run shows whether the tab crashes (huge DOM) or just closes early (test bug).
Step 4: Fix the sequencing and re-run
npx playwright test tests/a11y.spec.js | tail -4Expected: Spec runs injectAxe on a live page and reports real results.
Step 5: Re-run twice to rule out flakes
npx pa11y https://example.com/ | tail -2Expected: Two consecutive clean runs before calling it fixed; scan tools flake under load, so one green run is not proof.
When to use this skill
- The scan tool itself fails or crashes instead of reporting violations
- Your a11y CI step errors out before any rule results appear
- You run this tooling (pa11y, lighthouse, cypress-axe, axe-playwright) in automation
When NOT to use this skill
- The tool runs fine and reports real violations, use the rule-specific skills instead
- The failure is in your app code, not the scanner
Compatibility
axe-playwright 1.x/2.x with Playwright 1.4x. The injectAxe/checkA11y pairing mirrors cypress-axe. Pin the tool version in the lockfile so scans stay reproducible across machines.
Variant phrasings
axe-playwright page closed
Same error, same sequencing fix.
injectAxe target crashed
The variant where the renderer died, usually DOM size.
same failure locally and in CI
Scan tool failures are environmental; a fix that works on a laptop must also be verified under CI conditions.
Why it happens
Playwright tests that forget an await on goto or click race ahead to injectAxe while the page is still navigating, and navigating tears down the execution context, producing page closed. The other shape is a genuine renderer crash from an enormous DOM, which also presents as the page disappearing. The stack alone does not distinguish them, so the triage is awaits first, then DOM size, then browser stability. Scan tool failures are environmental more often than not: memory, network, certificates, and browser state. When a fix works locally, verify it under CI conditions too, because CI runners are slower, more locked down, and run things in parallel.
Edge cases
- Always await goto, click, and waitForLoadState before injectAxe, the race is the cause nine times out of ten.
- A page that crashes headed too has a DOM or memory problem, not a test problem.
- Reuse one browser context per spec file, context churn increases the odds of the race.
- Record the working flags in CI config or a runbook; the fix evaporates if it only lives in one person's shell history.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_iG2gOuNTw3cgvvu5zQQ2vQ
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.