1. Overview

Accessibility testing is crucial for ensuring that software applications are usable by everyone, including people with impairments. Performing accessibility tests manually can be time-consuming and error-prone. Therefore, automating accessibility testing with Selenium can streamline the process, making it easier to detect and fix accessibility issues early.

In this tutorial, we’ll explore how to automate accessibility testing with Selenium.

2. What Is Automated Accessibility Testing?

Automated accessibility testing is the process of using automated tools and scripts to evaluate how well a software application meets accessibility standards, such as WCAG (Web Content Accessibility Guidelines).

It helps identify accessibility barriers that could prevent people with disabilities from using the software effectively.

By automating these tests, teams can quickly detect issues related to screen reader compatibility, keyboard navigation, color contrast, and other accessibility aspects, ensuring that the application is more inclusive and compliant with legal requirements.

3. Why Selenium for Accessibility Automation?

Selenium WebDriver is a popular open-source test automation framework. It helps in automating popular browsers such as Chrome, Firefox, Edge, and Safari and offers greater flexibility and compatibility with different testing frameworks.

With the release of Selenium 4, it’s also fully W3C compliant. In many countries, legal requirements and industry standards, such as the Web Content Accessibility Guidelines, mandate that web applications must be accessible. By using Selenium for automated accessibility testing, organizations can efficiently evaluate their compliance with these regulations and standards.

4. How to Get Started With Selenium Accessibility Testing?

In this section, we’ll learn how to perform accessibility testing using Selenium on cloud grids offered by platforms such as LambdaTest.

LambdaTest is an AI-driven test execution platform that lets developers and testers perform accessibility automation using frameworks like Selenium and Cypress on over 3000+ real environments.

To perform Selenium accessibility testing on LambdaTest, we need to enable the Accessibility Automation plan.

4.1. Test Scenario

The following test scenario for accessibility testing will run on the LambdaTest eCommerce Playground website:

  1. Navigate to the LambdaTest eCommerce Playground website.
  2. Hover on the My Account dropdown and click on Login.
  3. Enter a valid username and password.
  4. Perform assertion to check that the logout link is displayed on successful login.

4.2. Setting up the Project

Let’s add the following Selenium dependency in the pom.xml file:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.23.1</version>
</dependency>

For the latest version, we can check out the Maven Central Repository.

Now, we’ll need to create a new ECommercePlayGroundAccessibilityTests class:

public class ECommercePlayGroundAccessibilityTests {

    private RemoteWebDriver driver;
    //..
}

Let’s define a setup() method that helps in instantiating the RemoteWebDriver:

@BeforeTest
public void setup() {
    final String userName = System.getenv("LT_USERNAME") == null 
      ? "LT_USERNAME" : System.getenv("LT_USERNAME");
    final String accessKey = System.getenv("LT_ACCESS_KEY") == null 
      ? "LT_ACCESS_KEY" : System.getenv("LT_ACCESS_KEY");
    final String gridUrl = "@hub.lambdatest.com/wd/hub";
    try {
        this.driver = new RemoteWebDriver(new URL(
          "http://" + userName + ":" + accessKey + gridUrl), 
          getChromeOptions());
    } catch (final MalformedURLException e) {
        System.out.println(
          "Could not start the remote session on LambdaTest cloud grid");
    }
    this.driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
}

This method requires the LambdaTest Username and Access Key to run the tests on the LambdaTest cloud grid. We can find these credentials from the LambdaTest Account Settings > Password & Security.

While instantiating the RemoteWebDriver, we’ll need to pass the capabilities like BrowserVersion, PlatformName, and more for running the tests:

public ChromeOptions getChromeOptions() {
    final var browserOptions = new ChromeOptions();
    browserOptions.setPlatformName("Windows 10");
    browserOptions.setBrowserVersion("127.0");
    final HashMap<String, Object> ltOptions = new HashMap<String, Object>();
    ltOptions.put("project", 
      "Automated Accessibility Testing With Selenium");
    ltOptions.put("build", 
      "LambdaTest Selenium Playground");
    ltOptions.put("name", 
      "Accessibility test");
    ltOptions.put("w3c", true);
    ltOptions.put("plugin", 
      "java-testNG");
    ltOptions.put("accessibility", true);
    ltOptions.put("accessibility.wcagVersion", 
      "wcag21");
    ltOptions.put("accessibility.bestPractice", false);
    ltOptions.put("accessibility.needsReview", true);

    browserOptions.setCapability(
      "LT:Options", ltOptions);

    return browserOptions;
}

For accessibility testing on LambdaTest, we’ll need to add capabilities such as accessibility, accessibility.wcagVersion, accessibility.bestPractice, and accessibility.needsReview.

To generate the capabilities, we can refer to the LambdaTest Automation Capabilities Generator.

4.3. Test Implementation

We’ll use the two test methods, testNavigationToLoginPage() and testLoginFunction() to implement the test scenario.

Below is the code snippet for the testNavigationToLoginPage() method:

@Test(priority = 1)
public void testNavigationToLoginPage() {
    driver.get("https://ecommerce-playground.lambdatest.io/");
    WebElement myAccountLink = driver.findElement(By.cssSelector(
      "#widget-navbar-217834 > ul > li:nth-child(6) > a"));
    Actions actions = new Actions(driver);
    actions.moveToElement(myAccountLink).build().perform();
    WebElement loginLink = driver.findElement(By.linkText("Login"));
    loginLink.click();

    String pageHeaderText = driver.findElement(By.cssSelector(
      "#content > div > div:nth-child(2) > div h2")).getText();
    assertEquals(pageHeaderText, "Returning Customer");
}

This test implements the first two steps of the test scenario, i.e., navigating to the LambdaTest eCommerce Playground website and hovering over the My account link. This test method executes first as the priority is set to “1”.

The moveToElement() method of the Actions class of Selenium WebDriver hovers over the My account link.

Once the menu opens, the Login link is located using the linkText, and a click action is performed on it. Finally, an assertion is performed to check that the Login page loads successfully.

Now let’s look at the code snippet for the testLoginFunction() method:

@Test(priority = 2)
public void testLoginFunction() {
    WebElement emailAddressField = driver.findElement(By.id(
      "input-email"));
    emailAddressField.sendKeys("[email protected]");
    WebElement passwordField = driver.findElement(By.id(
      "input-password"));
    passwordField.sendKeys("Password123");
    WebElement loginBtn = driver.findElement(By.cssSelector(
      "input.btn-primary"));
    loginBtn.click();

    WebElement myAccountLink = driver.findElement(By.cssSelector(
      "#widget-navbar-217834 > ul > li:nth-child(6) > a"));
    Actions actions = new Actions(driver);
    actions.moveToElement(myAccountLink).build().perform();
    WebElement logoutLink = driver.findElement(By.linkText("Logout"));
    assertTrue(logoutLink.isDisplayed());
}

This test covers the final steps of logging in with valid credentials and verifying the Logout link. After entering credentials in the E-Mail Address and Password fields, the login button is clicked. A mouse hover is performed on the My Account link using the moveToElement() method, and an assertion checks if the Logout link is visible.

4.4. Test Execution

The following screenshot from the LambdaTest Web Automation Dashboard shows both of the accessibility tests:

lambdatest accessibility test execution

5. Conclusion

Accessibility testing helps uncover the defects related to the usability of the website or web-based applications. It helps in making the website usable to all the users including the ones with disabilities.

It’s a must to follow the guidelines laid by WCAG to make the websites or the web applications accessible. Post that, it’s essential to perform accessibility testing of the websites to ensure the websites and web applications are usable for everyone.

The source code used in this article is available over on GitHub.