Using with Playwright
See and use a fully working example in the Alfa examples repository.
The following code sets up a Playwright test and runs an accessibility audit using the Code Checker. The result is logged to the console and the test fails if any issues on the page are found.
import { test, expect } from "@playwright/test";
import { Audit, Logging } from "@siteimprove/alfa-test-utils";
import { Playwright } from "@siteimprove/alfa-playwright";
test("page is accessible", async ({ page }) => {
// Navigate to the page to audit
await page.goto("http://localhost:8080/");
// Get the document and turn it into a page representation that the Code Checker can work with
const document = await page.evaluateHandle(() => window.document);
const alfaPage = await Playwright.toPage(document);
// Run an accessiblity audit
const alfaResult = await Audit.run(alfaPage);
// Log the result of the audit
Logging.fromAudit(alfaResult).print();
// Fail the test if there are any failed rules
const failingRules = alfaResult.resultAggregates.filter(
(aggregate) => aggregate.failed > 0,
);
expect(
failingRules.size,
`The page has ${failingRules.size} failing rules`,
).toBe(0);
});