Instructions for Java and Playwright

This page is not available in the language you requested. You have been redirected to the English version of the page.
Link to this page copied to clipboard

Configuring your tests with Java and Playwright

Not for use with personal data
  1. 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>
  2. 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;
  3. 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());
    important

    Watcher requires a Playwright persistent context. It is not compatible with non-persistent Browser launches. On Playwright Java 1.49 or later, you must also explicitly call setHeadless(false) (or setHeadless(true) with setChannel("chrome") or setChannel("chromium")). The default chromium-headless-shell mode is not supported.

    This code snippet creates an AxeWatcherPage instance that wraps your Playwright Page with accessibility testing enabled.

    To capture a screenshot whenever violations are found, add .setTakeScreenshots(true) to your AxeWatcherOptions. To also save screenshots locally, chain .setScreenshotDir("./axe-screenshots"). See Capture Violation Screenshots for more information.

    Be sure to set ACCESSIBILITY_API_KEY and PROJECT_ID in 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 set SERVER_URL to the base URL of that instance (for example, https://axe-eu.deque.com); otherwise, omit SERVER_URL and the default https://axe.deque.com will be used.

  4. 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.