Instructions for Java and Selenium

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 Selenium

Free Trial
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>3.23.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.selenium.AxeWatcher;
    import com.deque.axe_core.selenium.AxeWatcherDriver;
  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"));
    AxeWatcher watcher = new AxeWatcher(options);
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions = watcher.configure(chromeOptions);
    WebDriver wrappedDriver = watcher.wrapDriver(new ChromeDriver(chromeOptions));

    This code snippet creates a new WebDriver instance called wrappedDriver that you need to cast to use the axe controller methods.

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

  4. At the end of your testing session, call flush():

    ((AxeWatcherDriver) wrappedDriver).axeWatcher().flush();

    Calling flush() indicates that the test run is finished, and the results can be processed and presented to the user.