Run A Basic Automated Web Accessibility Test, With Axe-Core Selenium (Java)

Automated Web Accessibility Test with With Axe-Core & Selenium

Why Should You Automate Web Accessibility Testing?

Simply because manual testing takes time and is costly. Automation saves you time, effort and money. Leveraging automated tools and software is a great way to offload that workload. By automating your web accessibility tests, you will have more time to focus on what matters the most for your business. 

5 Great Benefits Of Automated Web Accessibility:

  • By implementing an automated solution, you reduce human error.
  • Automation helps to ensure that accessibility standards are met consistently across a website.
  • Identify and fix accessibility issues early in the development process, saving you time and money in the long run.
  • Integrating accessibility tests as part of the build process.
  • Automation increases the coverage of accessibility testing, allowing more aspects of a website’s accessibility to be checked.

What Is Axe-Core?

Axe-core is the world’s leading automated accessibility testing engine, it’s simple to use, lightweight and highly configurable.

It is used for checking accessibility violations on websites and HTML-based user interfaces. Axe-core can find 57% of WCAG issues automatically. It will also return elements as ‘incomplete’ where Axe-core is not certain, for further manual review. Axe-core needs a browser to run, which is why it’s often used with automation tools like Selenium.

The Benefits Of Using Axe-Core: 

  • No False positives.
  • It’s fast, secure and lightweight.
  • It’s open source; everyone can use it for free.
  • It’s maintained by Deque Systems, an accessibility behemoth.
  • It integrates seamlessly to your current functional/acceptance tests.
  • It determines which rules to check based on the evaluation context.
  • It works with modern web browsers, tools, frameworks, libraries and environments.
  • It supports static fixtures, in-memory fixtures, integration tests and iframes of any depth.

Axe-core supports the following web browsers:

  • Microsoft Edge v40 and above
  • Google Chrome v42 and above
  • Mozilla Firefox v38 and above
  • Apple Safari v7 and above

It supports the following accessibility standards:

  • WCAG 2.0
  • WCAG 2.1
  • Section 508

What Is Selenium?

Selenium is an open-source user interface automation testing suite for web applications. Selenium can automate all modern web browsers and it runs on all major operating systems. Selenium scripts can be written in Python, Java, C# and more.

Three Main components for the Selenium suite:

  • Selenium WebDriver
  • Selenium IDE
  • Selenium Grid

Selenium WebDriver

The Selenium WebDriver is a tool that allows developers to automate web browsers. It’s mainly used for automating testing of web applications. You can create scripts that automate tasks in a web browser, such as clicking buttons and links, filling forms and verifying that certain elements are present on a page. It allows you to quickly and easily test the functionality of a website. Selenium also gives you the option of either running a web browser headless or non-headless. The key difference being that a headless browser runs in the background without a graphical user interface (GUI) and is usually faster. And a non-headless browser runs as a real browser with a graphical user interface.

Selenium IDE

The Selenium IDE is a browser extension for Google Chrome and Firefox. It’s beginner friendly and works out of the box. With its simple UI, you can easily automate website testing in a couple of clicks. It has extensive control flow commands like if, while and times. It can also be extended with plugins for more commands and integration with third-party services. 

Selenium Grid

The Selenium Grid is used to run multiple instances of WebDriver scripts on remote machines. This is useful if you need more processing power, it’s also a great way to test on different browser and platform versions.

Setting Up Environment to run an Automated Web Accessibility Audit

Let’s see how you can set up your environment to run an automated web accessibility test using Axe-core, Selenium and Chrome Driver in Java:

1. You should already have Java JDK installed and downloaded ChromeDriver, as we will be using Chrome as our web browser to test web accessibility.

2. We will be using Maven as our project manager, so you should already have created a Maven project.

3. Now let’s open the pom.xml file inside the Maven project you’ve just created and add the following dependencies:

<dependency>
  <groupId>org.webdriver</groupId>
  <artifactId>selenium-java</artifactId>
  <version>3.141.59</version>
</dependency>

<dependency>
  <groupId>com.deque.axe-selenium</groupId>
  <artifactId>axe-selenium-java</artifactId>
  <version>4.0.0</version>
</dependency>

4. Now run nvm install inside your Maven project directory to install the dependencies.

5. Now you should be able to run Axe-core and Selenium inside your Maven project. 

Let’s run a test script! 

Running A Basic Test In Java

Here’s a simple script written in Java that uses Axe core, Selenium and Chrome Driver.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import com.deque.axe.AXE;
import org.json.JSONArray;
import org.json.JSONObject;

public class AccessibilityTest {
public static void main(String[] args) {
    // Set up Chrome driver
    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
    WebDriver driver = new ChromeDriver();

    // Open the page to test
    driver.get("http://www.example.com");

    // Run Axe-Core
    JSONObject response = new AXE.Builder(driver, "axe.min.js").analyze();

    // Print the results
    JSONArray violations = response.getJSONArray("violations");
    if (violations.length() > 0) {
        System.out.println("Accessibility issues found:");
        for (int i = 0; i < violations.length(); i++) {
            JSONObject violation = violations.getJSONObject(i);
            System.out.println(violation.getString("help"));
        }
    } else {
        System.out.println("No accessibility issues found.");
    }

    // Close the driver
    driver.quit();
}
}

This is how the script works:

1. This script launches a Chrome Driver instance.

2. It loads the website we want to test.

3. It then runs Axe-core and injects axe.min.js into the webpage.

4. Finally, it outputs the results to the console.

Conclusion

Running automated web accessibility tests is easier than ever with Axe core and Selenium. Manual testing takes time, effort and money. Implementing an automated solution is the best thing you do for the future of your business.

Overall, automating web accessibility tests will improve the accessibility of your website and make it more inclusive for users with disabilities.

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

Save Time with Dashboards – An Innovative Approach to Measure Accessibility KPIs


“What’s measured, improves”

Related Articles

Why Do You Need a VPAT

Why Do You Need a VPAT?

VPAT, short for Voluntary Product Accessibility Template, is a reporting format that gauges the level of accessibility for a product. It evaluates how usable your

Read More

Need help? Book a call at a time to suit you

Schedule a 30 minute free appointment to talk to one of our experts. We will take this time to understand what you want to achieve before we go into business.
Skip to content