Instructions for Java and Playwright
Configuring your tests with Java and Playwright
-
Add a new dependency for the Java Watcher package to your
pom.xml(for Maven) dependencies section (the Java Watcher package is available on Maven Central):<dependencies> <!-- Add this dependency: --> <dependency> <groupId>com.deque.axe_core</groupId> <artifactId>watcher</artifactId> <version>4.4.0</version> <!-- Update this as needed --> </dependency> </dependencies> -
Add imports for Java Watcher to your testing code:
import com.deque.axe_core.commons.AxeWatcherOptions; import com.deque.axe_core.playwright.AxeWatcherPlaywright; import com.deque.axe_core.playwright.AxeWatcherPage; -
Add setup code for Java Watcher, including your personal API key and project ID (both stored in the environment for security and flexibility):
AxeWatcherOptions options = new AxeWatcherOptions() .setApiKey(System.getenv("ACCESSIBILITY_API_KEY")) .setProjectId(System.getenv("PROJECT_ID")); // Optional: uncomment and set SERVER_URL if using a regional, private cloud, or on-premises instance: // options.setServerUrl(System.getenv("SERVER_URL")); AxeWatcherPlaywright watcher = new AxeWatcherPlaywright(options); BrowserType.LaunchPersistentContextOptions launchOptions = watcher.configure(new BrowserType.LaunchPersistentContextOptions()); // Watcher requires a persistent context. You must set headless explicitly on Playwright 1.49+: launchOptions.setHeadless(false); BrowserContext context = browserType.launchPersistentContext(userDataDir, launchOptions); AxeWatcherPage page = watcher.wrapPage(context.newPage());importantWatcher requires a Playwright persistent context. It is not compatible with non-persistent
Browserlaunches. On Playwright Java 1.49 or later, you must also explicitly callsetHeadless(false)(orsetHeadless(true)withsetChannel("chrome")orsetChannel("chromium")). The defaultchromium-headless-shellmode is not supported.This code snippet creates an
AxeWatcherPageinstance that wraps your PlaywrightPagewith accessibility testing enabled.To capture a screenshot whenever violations are found, add
.setTakeScreenshots(true)to yourAxeWatcherOptions. To also save screenshots locally, chain.setScreenshotDir("./axe-screenshots"). See Capture Violation Screenshots for more information.Be sure to set
ACCESSIBILITY_API_KEYandPROJECT_IDin your environment to your personal API key (found in your axe Account, API KEYS tab) and your project ID (shown at the top of these instructions when you created your project or available from the Projects page by choosing Configure project under Settings). If your organization uses a regional, private cloud, or on-premises Axe Developer Hub instance, also setSERVER_URLto the base URL of that instance (for example,https://axe-eu.deque.com); otherwise, omitSERVER_URLand the defaulthttps://axe.deque.comwill be used. -
At the end of your testing session, call
flush():page.axeWatcher().flush();Calling
flush()indicates that the test run is finished, and the results can be processed and presented to the user.To attribute violations to specific tests, call
setTestContext()before your test logic:page.axeWatcher().setTestContext( "src/test/java/com/acme/LoginTest.java", java.util.Arrays.asList("LoginTest", "rejects bad password") );See Attributing Violations to Tests for more information.
