Accessibility checks in Playwright tests, including pages behind a login
The pages most likely to break are the ones a URL scanner cannot see. Your end-to-end tests already sign in, fill forms and open dialogs, so that is the place to check them.
Why a URL is not enough
A hosted scanner fetches a page from somewhere else on the internet. That works for a
marketing site and fails for almost everything behind it: an account page needs a
session, a checkout needs a basket, and a review app on localhost in CI is not
reachable from outside at all. Skiplink's own API refuses to fetch private and internal
addresses, and it is right to; a scanner that would fetch anything you gave it is a
scanner someone else can point at your network.
Your Playwright suite has none of those problems. It already signs in, already puts something in the basket, already opens the dialog that only appears after the third click. Running axe-core inside the test checks the page in exactly the state a user would see it.
The smallest version
Install the adapter alongside axe-core:
npm install --save-dev @skiplink/playwright axe-core
Then call it at the point in a test where the page is in the state you care about:
import { test } from '@playwright/test';
import { expectNoNewViolations } from '@skiplink/playwright';
test('checkout is accessible', async ({ page }) => {
await page.goto('/checkout');
await page.getByLabel('Card number').fill('4242424242424242');
await expectNoNewViolations(page);
});
No token and no network call: the engine runs in the browser your test already has open.
axe-core is a peer dependency, so the version that produced a result is the version in
your lockfile, and every result reports it as engine.axe_core. Upgrading axe is then a
pull request like any other, and a new finding after an upgrade can be traced to the
upgrade rather than to your code.
Signing in once
Playwright's usual pattern for authenticated tests is a setup project that signs in and saves the browser's storage state, which the other tests then reuse. Nothing about it is specific to accessibility; the check simply runs on whatever page the signed-in test reaches.
// tests/auth.setup.js
import { test as setup } from '@playwright/test';
setup('sign in', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email').fill(process.env.E2E_EMAIL);
await page.getByLabel('Password').fill(process.env.E2E_PASSWORD);
await page.getByRole('button', { name: 'Sign in' }).click();
await page.waitForURL('/dashboard');
await page.context().storageState({ path: 'playwright/.auth/user.json' });
});
// playwright.config.js (projects only)
projects: [
{ name: 'setup', testMatch: /.*\.setup\.js/ },
{
name: 'chromium',
use: { ...devices['Desktop Chrome'], storageState: 'playwright/.auth/user.json' },
dependencies: ['setup'],
},
],
Keep playwright/.auth/ out of the repository; it holds a live session. The account
should be a test account with test data, for the same reason.
Check states, not just pages
A page is not one thing. The account page with its "delete account" dialog open is a different document from the account page without it, and the dialog is where focus handling, labels and contrast most often go wrong. Put the check after the interaction:
test('delete account dialog', async ({ page }) => {
await page.goto('/account');
await page.getByRole('button', { name: 'Delete account' }).click();
await page.getByRole('dialog').waitFor();
await expectNoNewViolations(page);
});
A useful rule of thumb: every test that opens a menu, a dialog, an error state or a step of a multi-step form is a candidate for one more line.
The engine is injected into every frame, so an embedded payment form or a third-party widget is checked too. Usually you cannot fix those yourself, but a finding with a selector is something you can send to the vendor.
Recording what is already broken
On an existing app the first run will fail, because the app already has violations. Record them once instead of fixing everything before you can merge:
SKIPLINK_UPDATE_BASELINE=1 npx playwright test
Commit .skiplink/baseline.json. From then on the check fails only on violations that are
not in the file. Recording one page never discards the entries for pages that run did not
visit, so you can record a single spec without losing the rest.
The same file is used by the CLI and the GitHub Action, and the fingerprints are computed by shared code. Public pages scanned through the API and private pages scanned in your tests can live in one baseline, and a violation accepted in one place stays accepted in the other.
Options worth knowing
The adapter takes a handful of options, listed on the Playwright integration page:
failOndefaults tonew. Set an impact floor (critical,serious,moderate,minor) to fail only on new violations at or above it, orneverto report without failing.standarddefaults towcag22aa, the same list the API takes.alladds axe-core's best-practice rules, which are not WCAG success criteria.includeandexcluderestrict the check to, or keep it away from, parts of the page. Excluding a third-party widget you cannot change is the common case.disableRulesturns rules off entirely. Prefer the baseline: a disabled rule is off on tomorrow's page too.baselineis the file path, orfalseto fail on any violation at all. TheSKIPLINK_BASELINEenvironment variable sets it too.
What a passing test does not mean
A green test means no automatically detectable violations were found in the state the page was in when the check ran. It says nothing about states your tests never reach, and nothing about the things axe cannot judge: whether keyboard focus moves somewhere sensible when the dialog opens, whether it returns when the dialog closes, whether an error message is understandable. Those need a person, ideally with a keyboard and a screen reader. The list of criteria no scanner detects is a good checklist for that review, and what a scan does not tell you explains the gap in full.
Related
- Adding accessibility checks to a site that already has violations
- Playwright Accessibility Testing Adapter
- Running accessibility checks on pull request preview deployments
Try it on your own site: scan a URL for free, or read the docs to add a baseline to your pipeline.