AxeWatcherController Class

Link to AxeWatcherController Class copied to clipboard

Control when and how accessibility testing occurs in your Selenium Java tests with axe Watcher

Free Trial
Not for use with personal data

The AxeWatcherController class gives you fine-grained control over when and how axe Watcher performs accessibility analysis during your Selenium tests. It provides methods to start, stop, analyze, and flush test results, allowing you to customize the testing process to suit your specific needs.

When running automated tests, axe Watcher can operate in two modes:

  • Automatic mode (default): Automatically analyzes each page visited by your test suite
  • Manual mode: Gives you control over which pages and states are analyzed

The AxeWatcherController is especially useful when you want to exclude certain pages from analysis or focus on specific page states.

Accessing the Controller

You don't instantiate AxeWatcherController directly. Instead, access it through the wrapped WebDriver instance:

WebDriver driver = watcher.wrapDriver(new ChromeDriver(chromeOptions));
AxeWatcherController controller = ((AxeWatcherDriver) driver).axeWatcher();

Methods

stop()

Stops automatic analysis of pages by axe Watcher. After calling this method, axe Watcher will not analyze pages automatically until you call start().

Returns:

  • AxeWatcherController - The current instance for method chaining

Example:

WebDriver driver = watcher.wrapDriver(new ChromeDriver(chromeOptions));
((AxeWatcherDriver) driver).axeWatcher().stop();

// These pages won't be analyzed automatically
driver.get("https://example.com/login");
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("submit")).click();

start()

Resumes automatic analysis of pages by axe Watcher after it has been stopped. This will reactivate the automatic analysis of pages as your test interacts with the application.

Returns:

  • AxeWatcherController - The current instance for method chaining

Example:

WebDriver driver = watcher.wrapDriver(new ChromeDriver(chromeOptions));

// Disable automatic analysis for login process
((AxeWatcherDriver) driver).axeWatcher().stop();
driver.get("https://example.com/login");
driver.findElement(By.id("login-button")).click();

// Re-enable automatic analysis for the main application
((AxeWatcherDriver) driver).axeWatcher().start();

// These pages will be automatically analyzed
driver.findElement(By.id("dashboard-link")).click();
driver.findElement(By.id("settings-button")).click();

analyze()

Triggers a manual accessibility analysis of the current page. This method can be called explicitly to force an accessibility scan on the current page state, regardless of whether automatic analysis is enabled or disabled.

Returns:

  • AxeWatcherController - The current instance for method chaining

Example:

WebDriver driver = watcher.wrapDriver(new ChromeDriver(chromeOptions));

// Navigate to a page
driver.get("https://example.com");

// Open a modal dialog
driver.findElement(By.id("open-modal-button")).click();

// Explicitly analyze the page with the modal open
((AxeWatcherDriver) driver).axeWatcher().analyze();

flush()

Flushes the results of the accessibility analysis to axe Developer Hub. This method should be called at the end of each test (typically in an @AfterEach or equivalent method) to ensure that all results are sent to axe Developer Hub.

Returns:

  • AxeWatcherController - The current instance for method chaining

Example:

@AfterEach
public void tearDown() {
    if (driver instanceof AxeWatcherDriver) {
        ((AxeWatcherDriver) driver).axeWatcher().flush();
    }
    driver.quit();
}

Method Chaining

All methods in AxeWatcherController support method chaining, allowing you to combine multiple operations:

((AxeWatcherDriver) driver).axeWatcher()
    .stop()                  // Stop automatic analysis
    .analyze()               // Perform a manual analysis
    .start()                 // Resume automatic analysis
    .flush();                // Flush results to axe Developer Hub

Important Notes

  1. iFrames: Controller methods like analyze(), start(), and stop() will not work when called from within an iframe. Always ensure you're in the top-level frame when using these methods.

  2. Page Protocols: Controller methods will not work on pages with protocols other than http:, https:, or file:.

  3. Flush Results: Always call flush() at the end of your tests to ensure all accessibility results are sent to axe Developer Hub.

  4. State Awareness: The controller keeps track of its state, so calling stop() when already stopped or start() when already started has no effect.

See Also