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 Accessibility 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.
  // This supposes that the server is already running.
  // TODO: Replace with your own page
  await page.goto("http://localhost:8080/");

  // Get the document and scrape it.
  const document = await page.evaluateHandle(() => window.document);
  const alfaPage = await Playwright.toPage(document);

  // Run an accessibility 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);
});

Previous: UsageNext: Reporting