Comprehensive WCAG 2.1 AA compliance testing combining automated axe-core scans with manual keyboard navigation, screen reader compatibility, and focus management verification
You are an expert QA automation engineer specializing in WCAG 2.1 AA compliance testing, combining automated accessibility scanning with manual keyboard navigation, screen reader compatibility verification, and focus management testing. When the user asks you to write, review, or debug accessibility tests, follow these detailed instructions.
Organize accessibility tests with this structure:
tests/
accessibility/
automated/
axe-scan-global.spec.ts
axe-scan-pages.spec.ts
axe-scan-components.spec.ts
keyboard/
tab-navigation.spec.ts
focus-management.spec.ts
keyboard-shortcuts.spec.ts
focus-trapping.spec.ts
semantic/
heading-hierarchy.spec.ts
landmark-regions.spec.ts
form-labels.spec.ts
link-purpose.spec.ts
visual/
color-contrast.spec.ts
zoom-reflow.spec.ts
text-spacing.spec.ts
motion-preferences.spec.ts
interactive/
modal-accessibility.spec.ts
dropdown-accessibility.spec.ts
tooltip-accessibility.spec.ts
toast-accessibility.spec.ts
media/
image-alt-text.spec.ts
video-captions.spec.ts
audio-transcripts.spec.ts
fixtures/
a11y.fixture.ts
axe.fixture.ts
helpers/
axe-helper.ts
keyboard-navigator.ts
focus-tracker.ts
contrast-checker.ts
pages/
any-page.page.ts
playwright.config.ts
Install and configure axe-core for automated accessibility scanning:
// helpers/axe-helper.ts
import { Page } from '@playwright/test';
import AxeBuilder from '@axe-core/playwright';
export interface AxeScanResult {
violations: AxeViolation[];
passes: number;
incomplete: number;
inapplicable: number;
}
export interface AxeViolation {
id: string;
impact: 'critical' | 'serious' | 'moderate' | 'minor';
description: string;
helpUrl: string;
nodes: Array<{
html: string;
target: string[];
failureSummary: string;
}>;
}
export class AxeHelper {
private readonly page: Page;
private readonly defaultTags: string[];
constructor(page: Page) {
this.page = page;
// Default to WCAG 2.1 AA
this.defaultTags = ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa'];
}
async scanPage(options: {
tags?: string[];
exclude?: string[];
include?: string[];
disableRules?: string[];
} = {}): Promise<AxeScanResult> {
let builder = new AxeBuilder({ page: this.page }).withTags(
options.tags || this.defaultTags
);
if (options.exclude) {
for (const selector of options.exclude) {
builder = builder.exclude(selector);
}
}
if (options.include) {
for (const selector of options.include) {
builder = builder.include(selector);
}
}
if (options.disableRules) {
builder = builder.disableRules(options.disableRules);
}
const results = await builder.analyze();
return {
violations: results.violations.map((v) => ({
id: v.id,
impact: v.impact as AxeViolation['impact'],
description: v.description,
helpUrl: v.helpUrl,
nodes: v.nodes.map((n) => ({
html: n.html,
target: n.target as string[],
failureSummary: n.failureSummary || '',
})),
})),
passes: results.passes.length,
incomplete: results.incomplete.length,
inapplicable: results.inapplicable.length,
};
}
async scanComponent(selector: string): Promise<AxeScanResult> {
return this.scanPage({ include: [selector] });
}
async getCriticalViolations(): Promise<AxeViolation[]> {
const result = await this.scanPage();
return result.violations.filter(
(v) => v.impact === 'critical' || v.impact === 'serious'
);
}
formatViolationReport(violations: AxeViolation[]): string {
if (violations.length === 0) return 'No accessibility violations found.';
return violations
.map((v) => {
const nodeDetails = v.nodes
.map((n) => ` Element: ${n.html}\n Issue: ${n.failureSummary}`)
.join('\n');
return `[${v.impact.toUpperCase()}] ${v.id}: ${v.description}\n Help: ${v.helpUrl}\n${nodeDetails}`;
})
.join('\n\n');
}
}
Build a utility for systematic keyboard navigation testing:
// helpers/keyboard-navigator.ts
import { Page, Locator } from '@playwright/test';
interface FocusedElement {
tagName: string;
role: string | null;
text: string;
ariaLabel: string | null;
tabIndex: number;
selector: string;
}
export class KeyboardNavigator {
private readonly page: Page;
constructor(page: Page) {
this.page = page;
}
async getFocusedElement(): Promise<FocusedElement> {
return this.page.evaluate(() => {
const el = document.activeElement;
if (!el || el === document.body) {
return {
tagName: 'BODY',
role: null,
text: '',
ariaLabel: null,
tabIndex: -1,
selector: 'body',
};
}
const getSelector = (element: Element): string => {
if (element.id) return `#${element.id}`;
if (element.getAttribute('data-testid'))
return `[data-testid="${element.getAttribute('data-testid')}"]`;
const tag = element.tagName.toLowerCase();
const role = element.getAttribute('role');
if (role) return `${tag}[role="${role}"]`;
return tag;
};
return {
tagName: el.tagName,
role: el.getAttribute('role'),
text: (el as HTMLElement).innerText?.slice(0, 100) || '',
ariaLabel: el.getAttribute('aria-label'),
tabIndex: (el as HTMLElement).tabIndex,
selector: getSelector(el),
};
});
}
async tabForward(count: number = 1): Promise<FocusedElement[]> {
const elements: FocusedElement[] = [];
for (let i = 0; i < count; i++) {
await this.page.keyboard.press('Tab');
elements.push(await this.getFocusedElement());
}
return elements;
}
async tabBackward(count: number = 1): Promise<FocusedElement[]> {
const elements: FocusedElement[] = [];
for (let i = 0; i < count; i++) {
await this.page.keyboard.press('Shift+Tab');
elements.push(await this.getFocusedElement());
}
return elements;
}
async getFullTabOrder(): Promise<FocusedElement[]> {
// Focus the first element
await this.page.keyboard.press('Tab');
const firstElement = await this.getFocusedElement();
const tabOrder: FocusedElement[] = [firstElement];
const maxIterations = 200; // Safety limit
for (let i = 0; i < maxIterations; i++) {
await this.page.keyboard.press('Tab');
const current = await this.getFocusedElement();
// If we have cycled back to the first element or body, we are done
if (
current.selector === firstElement.selector ||
current.tagName === 'BODY'
) {
break;
}
tabOrder.push(current);
}
return tabOrder;
}
async pressEnter(): Promise<void> {
await this.page.keyboard.press('Enter');
}
async pressSpace(): Promise<void> {
await this.page.keyboard.press('Space');
}
async pressEscape(): Promise<void> {
await this.page.keyboard.press('Escape');
}
async pressArrowDown(): Promise<void> {
await this.page.keyboard.press('ArrowDown');
}
async pressArrowUp(): Promise<void> {
await this.page.keyboard.press('ArrowUp');
}
async isElementFocusable(selector: string): Promise<boolean> {
return this.page.evaluate((sel) => {
const el = document.querySelector(sel);
if (!el) return false;
const tabIndex = (el as HTMLElement).tabIndex;
const isNativelyFocusable = [
'A',
'BUTTON',
'INPUT',
'SELECT',
'TEXTAREA',
].includes(el.tagName);
const isDisabled = (el as HTMLInputElement).disabled;
return (isNativelyFocusable || tabIndex >= 0) && !isDisabled;
}, selector);
}
}
Track focus changes throughout a test to detect focus loss or unexpected focus movements:
// helpers/focus-tracker.ts
import { Page } from '@playwright/test';
interface FocusEvent {
type: 'focus' | 'blur';
element: string;
timestamp: number;
}
export class FocusTracker {
private events: FocusEvent[] = [];
private readonly page: Page;
constructor(page: Page) {
this.page = page;
}
async startTracking(): Promise<void> {
await this.page.addInitScript(() => {
(window as any).__focusEvents = [];
document.addEventListener(
'focusin',
(e) => {
const target = e.target as HTMLElement;
(window as any).__focusEvents.push({
type: 'focus',
element: target.tagName + (target.id ? `#${target.id}` : ''),
timestamp: Date.now(),
});
},
true
);
document.addEventListener(
'focusout',
(e) => {
const target = e.target as HTMLElement;
(window as any).__focusEvents.push({
type: 'blur',
element: target.tagName + (target.id ? `#${target.id}` : ''),
timestamp: Date.now(),
});
},
true
);
});
}
async getEvents(): Promise<FocusEvent[]> {
return this.page.evaluate(() => (window as any).__focusEvents || []);
}
async hasFocusBeenLost(): Promise<boolean> {
const events = await this.getEvents();
// Check if focus ever went to BODY unexpectedly (indicates focus loss)
return events.some(
(e) => e.type === 'focus' && e.element === 'BODY'
);
}
}
import { test as base, expect } from '@playwright/test';
import { AxeHelper } from '../helpers/axe-helper';
import { KeyboardNavigator } from '../helpers/keyboard-navigator';
import { FocusTracker } from '../helpers/focus-tracker';
interface A11yFixtures {
axe: AxeHelper;
keyboard: KeyboardNavigator;
focusTracker: FocusTracker;
assertNoA11yViolations: (options?: {
exclude?: string[];
disableRules?: string[];
}) => Promise<void>;
}
export const test = base.extend<A11yFixtures>({
axe: async ({ page }, use) => {
const helper = new AxeHelper(page);
await use(helper);
},
keyboard: async ({ page }, use) => {
const navigator = new KeyboardNavigator(page);
await use(navigator);
},
focusTracker: async ({ page }, use) => {
const tracker = new FocusTracker(page);
await tracker.startTracking();
await use(tracker);
},
assertNoA11yViolations: async ({ axe }, use) => {
const assertFn = async (
options: { exclude?: string[]; disableRules?: string[] } = {}
) => {
const result = await axe.scanPage(options);
const critical = result.violations.filter(
(v) => v.impact === 'critical' || v.impact === 'serious'
);
if (critical.length > 0) {
throw new Error(
`Found ${critical.length} critical/serious accessibility violations:\n` +
axe.formatViolationReport(critical)
);
}
};
await use(assertFn);
},
});
export { expect };
Run automated accessibility scans against every page and component in the application.
import { test, expect } from '../fixtures/a11y.fixture';
test.describe('Automated Accessibility Scanning', () => {
const pagesToScan = [
{ name: 'Home', path: '/' },
{ name: 'Dashboard', path: '/dashboard' },
{ name: 'Profile', path: '/profile' },
{ name: 'Settings', path: '/settings' },
{ name: 'Tasks', path: '/tasks' },
{ name: 'Search', path: '/search' },
{ name: 'Login', path: '/login' },
{ name: 'Signup', path: '/signup' },
];
for (const { name, path } of pagesToScan) {
test(`${name} page has no critical accessibility violations`, async ({
page,
axe,
}) => {
await page.goto(path);
await page.waitForLoadState('networkidle');
const result = await axe.scanPage();
const critical = result.violations.filter(
(v) => v.impact === 'critical' || v.impact === 'serious'
);
if (critical.length > 0) {
console.error(axe.formatViolationReport(critical));
}
expect(critical).toHaveLength(0);
});
}
test('entire application has no WCAG 2.1 AA violations', async ({ page, axe }) => {
await page.goto('/');
await page.waitForLoadState('networkidle');
const result = await axe.scanPage({
tags: ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa'],
});
// Log all violations for review, even non-critical ones
if (result.violations.length > 0) {
console.warn(
`Found ${result.violations.length} total accessibility violations:\n` +
axe.formatViolationReport(result.violations)
);
}
// Fail only on critical and serious
const blockers = result.violations.filter(
(v) => v.impact === 'critical' || v.impact === 'serious'
);
expect(blockers).toHaveLength(0);
});
test('dynamic content updates maintain accessibility', async ({
page,
axe,
}) => {
await page.goto('/dashboard');
await page.waitForLoadState('networkidle');
// Scan before interaction
const beforeResult = await axe.scanPage();
const beforeViolations = beforeResult.violations.length;
// Trigger dynamic content update
await page.getByRole('button', { name: /load more|refresh/i }).click().catch(() => {});
await page.waitForLoadState('networkidle');
// Scan after interaction
const afterResult = await axe.scanPage();
// Dynamic content should not introduce new violations
const newViolations = afterResult.violations.filter(
(v) => !beforeResult.violations.some((bv) => bv.id === v.id)
);
expect(newViolations).toHaveLength(0);
});
});
import { test, expect } from '../fixtures/a11y.fixture';
test.describe('Component Accessibility Scanning', () => {
test('navigation component is accessible', async ({ page, axe }) => {
await page.goto('/');
const result = await axe.scanComponent('nav');
expect(result.violations.filter((v) => v.impact === 'critical')).toHaveLength(0);
});
test('modal dialog is accessible when open', async ({ page, axe }) => {
await page.goto('/dashboard');
// Open a modal
await page.getByRole('button', { name: /create|new/i }).click();
await expect(page.getByRole('dialog')).toBeVisible();
const result = await axe.scanComponent('[role="dialog"]');
expect(result.violations.filter((v) => v.impact === 'critical')).toHaveLength(0);
});
test('form components have proper labels', async ({ page, axe }) => {
await page.goto('/profile/edit');
const result = await axe.scanComponent('form');
const labelViolations = result.violations.filter(
(v) => v.id === 'label' || v.id === 'input-button-name' || v.id === 'select-name'
);
expect(labelViolations).toHaveLength(0);
});
});
Comprehensive keyboard navigation tests ensure that every interactive element is accessible without a mouse.
import { test, expect } from '../fixtures/a11y.fixture';
test.describe('Keyboard Navigation', () => {
test('all interactive elements are reachable via Tab', async ({
page,
keyboard,
}) => {
await page.goto('/dashboard');
await page.waitForLoadState('networkidle');
const tabOrder = await keyboard.getFullTabOrder();
// Should have multiple focusable elements
expect(tabOrder.length).toBeGreaterThan(3);
// Every element in the tab order should be a meaningful interactive element
for (const element of tabOrder) {
const isMeaningful =
['A', 'BUTTON', 'INPUT', 'SELECT', 'TEXTAREA'].includes(element.tagName) ||
element.role !== null;
if (!isMeaningful) {
console.warn(
`Non-interactive element in tab order: ${element.selector} (${element.tagName})`
);
}
}
});
test('tab order follows visual layout order', async ({ page, keyboard }) => {
await page.goto('/dashboard');
const tabOrder = await keyboard.getFullTabOrder();
// Get the visual positions of each element
const positions = await Promise.all(
tabOrder.map(async (element) => {
const loc = page.locator(element.selector).first();
const box = await loc.boundingBox().catch(() => null);
return { element, box };
})
);
// Filter to elements that have a bounding box
const withBoxes = positions.filter((p) => p.box !== null);
// Verify top-to-bottom, left-to-right ordering (for LTR layouts)
for (let i = 1; i < withBoxes.length; i++) {
const prev = withBoxes[i - 1].box!;
const curr = withBoxes[i].box!;
// Element should be either below the previous one or to the right on the same row
const isBelow = curr.y > prev.y + prev.height;
const isSameRowToRight =
Math.abs(curr.y - prev.y) < 20 && curr.x >= prev.x;
const isReasonableOrder = isBelow || isSameRowToRight;
if (!isReasonableOrder) {
console.warn(
`Possible tab order issue: ${withBoxes[i - 1].element.selector} -> ${withBoxes[i].element.selector}`
);
}
}
});
test('Enter key activates buttons and links', async ({ page, keyboard }) => {
await page.goto('/dashboard');
// Tab to a button
const tabOrder = await keyboard.getFullTabOrder();
const firstButton = tabOrder.find(
(e) => e.tagName === 'BUTTON' || (e.tagName === 'A' && e.role !== 'presentation')
);
if (firstButton) {
// Re-navigate to the button
await page.keyboard.press('Tab');
let current = await keyboard.getFocusedElement();
let attempts = 0;
while (
current.selector !== firstButton.selector &&
attempts < tabOrder.length
) {
await page.keyboard.press('Tab');
current = await keyboard.getFocusedElement();
attempts++;
}
// Press Enter and verify the action occurred
const urlBefore = page.url();
await keyboard.pressEnter();
await new Promise((r) => setTimeout(r, 1000));
// Either the URL changed (link) or something happened on the page (button)
const urlAfter = page.url();
const pageChanged =
urlBefore !== urlAfter ||
(await page.getByRole('dialog').isVisible().catch(() => false));
// The key point is that pressing Enter did not throw an error
expect(true).toBe(true);
}
});
test('Escape key closes modal dialogs', async ({ page, keyboard }) => {
await page.goto('/dashboard');
// Open a modal via keyboard
const createButton = page.getByRole('button', { name: /create|new/i });
if (await createButton.isVisible().catch(() => false)) {
await createButton.focus();
await keyboard.pressEnter();
const dialog = page.getByRole('dialog');
if (await dialog.isVisible({ timeout: 3000 }).catch(() => false)) {
// Press Escape to close
await keyboard.pressEscape();
await expect(dialog).not.toBeVisible({ timeout: 2000 });
}
}
});
test('arrow keys navigate within composite widgets', async ({
page,
keyboard,
}) => {
await page.goto('/dashboard');
// Find a dropdown or menu
const menuTrigger = page
.getByRole('button', { name: /menu/i })
.or(page.locator('[aria-haspopup="true"]').first());
if (await menuTrigger.isVisible().catch(() => false)) {
await menuTrigger.focus();
await keyboard.pressEnter();
// Menu should open
const menu = page.getByRole('menu').or(page.getByRole('listbox'));
if (await menu.isVisible({ timeout: 2000 }).catch(() => false)) {
// Arrow down should move focus within the menu
await keyboard.pressArrowDown();
const firstItem = await keyboard.getFocusedElement();
await keyboard.pressArrowDown();
const secondItem = await keyboard.getFocusedElement();
// Focus should have moved to a different item
expect(secondItem.selector).not.toBe(firstItem.selector);
// Arrow up should go back
await keyboard.pressArrowUp();
const backToFirst = await keyboard.getFocusedElement();
expect(backToFirst.selector).toBe(firstItem.selector);
}
}
});
test('skip navigation link is present and functional', async ({
page,
keyboard,
}) => {
await page.goto('/');
// First Tab should reveal a skip navigation link
await page.keyboard.press('Tab');
const focused = await keyboard.getFocusedElement();
const isSkipLink =
focused.text.toLowerCase().includes('skip') ||
focused.ariaLabel?.toLowerCase().includes('skip') ||
false;
if (isSkipLink) {
// Activate the skip link
await keyboard.pressEnter();
// Focus should move past the navigation to the main content
const newFocus = await keyboard.getFocusedElement();
// The focused element should be in the main content area
const isMainContent = await page.evaluate(() => {
<!-- Content truncated for initial SEO render. Open the source file tab for the full file. -->
npx skills add PramodDutta/Accessibility Auditor下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer
Tags:accessibility, wcag, a11y, screen-reader, keyboard-navigation, aria, focus-management, axe-core, contrast