Accessibility examples
Run accessibility checks with the companion package.
These snippets show how accessibility checks fit into Playwright PHP tests when the playwright-php/accessibility companion package is installed.
This page is not a core runnable script. The companion package is documented separately because it is installed in addition to playwright-php/playwright.
Install the companion package
composer require --dev playwright-php/accessibility
PHPUnit smoke test
Copy this into tests/Browser/AccessibilitySmokeTest.php:
<?php
declare(strict_types=1);
namespace App\Tests\Browser;
use Playwright\Accessibility\AssertsAccessibility;
use Playwright\Accessibility\AxeBuilder;
use Playwright\Testing\PlaywrightTestCase;
final class AccessibilitySmokeTest extends PlaywrightTestCase
{
use AssertsAccessibility;
public function testHomepageHasNoAutomatedViolations(): void
{
$this->page->goto('https://app.example.test/');
$this->assertIsAccessible(
(new AxeBuilder($this->page))->within('#main-content')
);
}
}
Run it:
vendor/bin/phpunit --filter AccessibilitySmokeTest
Expected result: PHPUnit passes when the selected page has no automated accessibility violations. When it fails, the assertion output should point to rule IDs and selectors.
What it demonstrates
AxeBuilderruns an axe-core audit against the current page.within()scopes the audit to the part of the page you own.AssertsAccessibilityturns violations into a test assertion.
Start with a few stable pages. Do not audit every page after every click.
Go next
- Learn the workflow: Accessibility Testing
- See the group page: Quality
- Check the package: Accessibility package
- Use semantic selectors: Locators