Features

Accessibility testing

Automated Accessibility Testing (A11y) allows you to test if your website follows the Web Content Accessibility Guidelines (WCAG) rules and other guidelines, to ensure your website is accessible to all types of users.

Example

The following example will start a browser in the TestingBot browser grid, navigate to a website and call the custom tb:accessibility command to fetch the accessibility results.

We will then retrieve the results from the accessibility test from inside our automated test. The results will be saved in a JSON file for inspection.

Copy code
#!/usr/bin/env ruby

require 'rubygems'
require 'selenium-webdriver'

caps = Selenium::WebDriver::Remote::Capabilities.new
caps["browserName"] = "chrome"
caps["version"] = "latest"
caps["platform"] = :WINDOWS
caps["name"] = "Accessibility Test"

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120

driver = Selenium::WebDriver.for(
	:remote,
	:url => "https://API_KEY:API_SECRET@hub.testingbot.com/wd/hub",
	:desired_capabilities => caps,
	:http_client => client)
driver.navigate.to "https://testingbot.com"
result = driver.execute_script('tb:accessibility')
file = open("path/to/report.json", "w")
file.write(result)
file.close()
driver.quit
Copy code
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.net.URL;

public class AccessibilityTest {

  public static final String KEY = "KEY";
  public static final String SECRET = "SECRET";
  public static final String URL = "http://" + KEY + ":" + SECRET + "@hub.testingbot.com/wd/hub";

  public static void main(String[] args) throws Exception {

	DesiredCapabilities caps = new DesiredCapabilities();
	caps.setCapability("browserName", "chrome");
	caps.setCapability("version", "latest-1");
	caps.setCapability("platform", "WIN10");
	caps.setCapability("name", "Accessibility Test");

	WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
	driver.get("https://testingbot.com");

	JavascriptExecutor jse = (JavascriptExecutor)driver;
	File output = new File("path/to/report.json");
	FileWriter writer = new FileWriter(output);
	String result = (String) jse.executeScript("tb:accessibility");
	writer.write(result);
	writer.flush();
	writer.close();

	driver.quit();
  }
}
Copy code
<?php

    require_once('vendor/autoload.php');
    use Facebook\WebDriver\Remote\RemoteWebDriver;
    use Facebook\WebDriver\WebDriverBy;

    $web_driver = RemoteWebDriver::create(
    "https://api_key:api_secret@hub.testingbot.com/wd/hub",
        array("platform"=>"WIN10", "browserName"=>"chrome", "version" => "latest", "name" => "Accessibility Test"), 120000
    );
    $web_driver->get("https://testingbot.com");
    $result = $web_driver->executeScript('tb:accessibility');
    $file = fopen("path/to/report.json", "w");
    fwrite($file, json_encode($result, JSON_PRETTY_PRINT));
    fclose($file);
    $web_driver->quit();
?>
Copy code
from selenium import webdriver
desired_cap = {
	'platform': 'WIN10', 
	'browserName': 'chrome', 
	'version': 'latest-1',
	'name': 'Accessibility Testing'
}
driver = webdriver.Remote(
    desired_capabilities=desired_cap,
    command_executor='http://key:secret@hub.testingbot.com/wd/hub',
    )
driver.get("https://testingbot.com")
result = driver.execute_script('tb:accessibility')
file = open("path/to/report.json", "w")
file.write(str(result))
file.close()
driver.quit()
Copy code
const webdriver = require('selenium-webdriver');
const fs = require('fs')

const testingbotKey = "api_key";
const testingbotSecret = "api_secret";

const capabilities = {
    'browserName': 'firefox',
    'platform': 'WIN10',
    'version': 'latest',
    'client_key': testingbotKey,
    'client_secret': testingbotSecret,
    'name': 'Accessibility Test'
};

async function runAccessibilityTest () {
  let driver = new webdriver.Builder()
    .usingServer('https://' + testingbotKey + ':' + testingbotSecret + '@hub.testingbot.com/wd/hub')
    .withCapabilities(capabilities)
    .build();
  await driver.get("https://testingbot.com");
  let result = await driver.executeScript('tb:accessibility');
  await fs.writeFileSync('path/to/report.json', JSON.stringify(result));
  await driver.quit();
}
runAccessibilityTest();
Copy code
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace SeleniumTest {
  class Program {
    static readonly string report = @"/path/where/you/want/to/save/report.json";

    static void Main(string[] args) {
      IWebDriver driver;
      DesiredCapabilities capability = DesiredCapabilities.Chrome();
      capability.SetCapability("key", "key");
      capability.SetCapability("secret", "secret");
      capability.SetCapability("version", "latest-1");
      driver = new RemoteWebDriver(
        new Uri("https://hub.testingbot.com/wd/hub/"), capability
      );
      driver.Navigate().GoToUrl("https://testingbot.com");
      
      string result = (string)driver.ExecuteScript("tb:accessibility");
      File.WriteAllText(report, result);

      driver.Quit();
    }
  }
}

You could write assertions to pass/fail your test depending on the accessibility issues found.

Results

After running the example test, you will find a report.json file with the results of the accessibility test.

The file will contain the following structure:

Copy code
{
		testEngine: {}
		passes: []
		inapplicable: []
		url: "..."
		timestamp: "time when the result was generated"
		testRunner: {}
		toolOptions: {}
		testEnvironment: {}
		violations: {}
		incomplete: {}
}
  • violations includes the rules that were violated and need to be fixed.
  • passes the rules that have passed successfully.
  • incomplete incomplete results, either due to an error or aborted test
  • inapplicable no matching content was found for these rules.

An overview of all accessibility rules that will be checked can be found on A11Y rules overview.