Testing With Playwright
The AudioEye Accessibility Testing SDK Playwright Library gives you the ability to write Playwright tests to test for accessibility issues in your web application.
Pre-requisites
Visit the Getting Started page to configure your .npmrc with your AudioEye entitlement token.
Installation
npm install -D @audioeye/testing-sdk-playwright
We require @playwright/test to be version 1.39.0 or greater.
Setup
We use Playwright fixtures to evaluate accessibility issues. Fixtures work
by extending the built in test runner. To use the AudioEye Playwright SDK, you will need to add our fixtures to your
existing Playwright tests (see Playwright's documentation on
using a fixture). This can be done by either:
- Importing the extended
testrunner exported from@audioeye/testing-sdk-playwrightdirectly into your tests. - Adding our fixtures to the
testrunner in your Playwright test code and importing that exportedtestrunner into your tests.
- Adding fixtures to the test runner
- Using the extended test runner directly
// tests/fixtures.ts
import { type AudioEyeFixtures, AudioEyeA11y } from '@audioeye/testing-sdk-playwright';
import { test as base } from '@playwright/test';
export const test = base.extend<AudioEyeFixtures>({
accessibility: async ({ page }, use) => {
await use(new AudioEyeA11y(page));
},
a11y: async ({ accessibility }, use) => {
await use(accessibility);
},
});
// tests/example.spec.ts
import { expect } from '@playwright/test';
import { test } from './fixtures';
test('Example Playwright test using AudioEye\'s Testing SDK', async ({
page,
accessibility,
}) => {
await page.goto('http://localhost:3000/');
const results = await accessibility.evaluate();
expect(results.resultCodes).toEqual([
...
]);
});
import { test } from '@audioeye/testing-sdk-playwright';
import { expect } from '@playwright/test';
test('Example Playwright test using AudioEye\'s Testing SDK', async ({
page,
accessibility,
}) => {
await page.goto('http://localhost:3000/');
const results = await accessibility.evaluate();
expect(results.resultCodes).toEqual([
...
]);
});
We provide fixtures named accessibility and a11y which can be used interchangeably.
Usage
Use the SDK to test for accessibility issues.
- Using your fixtures
- Using AudioEye's provided test runner
import { expect } from '@playwright/test';
import { test } from './fixtures';
test("Example Playwright test using AudioEye's Testing SDK", async ({ page, accessibility }) => {
await page.goto('http://localhost:3000/');
const results = await accessibility.evaluate(page.locator('.navbar__right'));
expect(results.conformanceLevel('A').resultCodes).toEqual(['Link_VisualIndicator_Missing', 'Svg_Name_Missing']);
expect(Object.keys(results.conformanceLevel('AA').resultsGroupedByWcagSuccessCriteriaLevel)).toEqual(['A']);
expect(Object.keys(results.resultsGroupedByWcagSuccessCriteriaLevel)).toEqual(['A', 'AAA']);
});
import { test } from '@audioeye/testing-sdk-playwright';
import { expect } from '@playwright/test';
test("Example Playwright test using AudioEye's Testing SDK", async ({ page, accessibility }) => {
await page.goto('http://localhost:3000/');
const results = await accessibility.evaluate(page.locator('.navbar__right'));
expect(results.conformanceLevel('A').resultCodes).toEqual(['Link_VisualIndicator_Missing', 'Svg_Name_Missing']);
expect(Object.keys(results.conformanceLevel('AA').resultsGroupedByWcagSuccessCriteriaLevel)).toEqual(['A']);
expect(Object.keys(results.resultsGroupedByWcagSuccessCriteriaLevel)).toEqual(['A', 'AAA']);
});
We provide fixtures named accessibility and a11y which can be used interchangeably.
SDK API
evaluate
Evaluate the accessibility of the page or element using AudioEye's rule suite.
import { test } from '@audioeye/testing-sdk-playwright';
import { expect } from '@playwright/test';
test("Example Playwright test using AudioEye's Testing SDK", async ({ page, accessibility }) => {
await page.goto('http://localhost:3000/');
const accessibilityResults = await accessibility.evaluate();
});
Parameters
› locator (Locator)
The locator to evaluate. Defaults to the root of the page.
Returns
evaluate returns a Class of type A11yResults.
A11yResults API
conformanceLevel
Filter the accessibility results by the conformance level. The conformance level can be 'A', 'AA', or 'AAA'. Higher conformance levels include all lower conformance levels.
const results = accessibilityResults.conformanceLevel('AA');
Parameters
› level (String)
The conformance level to filter the results by. Either A, AA, or AAA.
Returns
A new instance of A11yResults.
resultCodes
Get the result codes for the accessibility results as a string array.
const resultCodes = accessibilityResults.resultCodes;
// ['Img_Name_WeakName']
Returns
An array of strings representing the result codes.
resultsGroupedByWcagSuccessCriteriaLevel
Group the accessibility results grouped by the WCAG success criteria level.
accessibilityResults.resultsGroupedByWcagSuccessCriteriaLevel();
// {A: [ ... ], AA: [ ... ], AAA: [ ... ]}
Returns
An object with the test results grouped by WCAG success criteria level.