Instructions for Java and Selenium
Configuring your tests with Java and Selenium
-
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.5.1</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.selenium.AxeWatcher; import com.deque.axe_core.selenium.AxeWatcherDriver; -
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")); 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
WebDriverinstance calledwrappedDriverthat you need to cast to use the axe controller methods.To run your tests in Microsoft Edge instead, pass an
EdgeOptionsinstance toconfigure()and anEdgeDriverinstance towrapDriver()(both are imported fromorg.openqa.selenium.edge). Everything else, including the controller methods andflush(), works the same way:AxeWatcherOptions options = new AxeWatcherOptions() .setApiKey(System.getenv("ACCESSIBILITY_API_KEY")) .setProjectId(System.getenv("PROJECT_ID")); AxeWatcher watcher = new AxeWatcher(options); EdgeOptions edgeOptions = new EdgeOptions(); edgeOptions = watcher.configure(edgeOptions); WebDriver wrappedDriver = watcher.wrapDriver(new EdgeDriver(edgeOptions));importantMicrosoft Edge support requires Watcher 4.5.1 or later and Selenium 4 or later. Selenium 3's
EdgeOptionstargets the legacy EdgeHTML browser, which Axe Watcher doesn't support, so callingconfigure()withEdgeOptionson Selenium 3 throws anIllegalStateException. Chrome for Testing and Chromium remain supported on Selenium 3.141.59 and later.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():((AxeWatcherDriver) wrappedDriver).axeWatcher().flush();Calling
flush()indicates that the test run is finished, and the results can be processed and presented to the user.
