Accessibility testing : Avoid tab index values greater than 0

Avoid using tab index values greater than 0.

Language: Java

Framework: Selenium 4

copy
1/​/​Assumptions: 2/​/​1. The web application has elements with specified tab index values 3/​/​2. We're testing for accessibility using Selenium 4 with Java 4/​/​3. local driver is installed properly 56import org.openqa.selenium.By;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.chrome.ChromeDriver;1011public class AccessibilityTesting {12 public static void main(String[] args) {13 /​/​Create instance of webdriver14 WebDriver driver = new ChromeDriver();15 16 /​/​Navigation to web application17 driver.get("https:/​/​www.example.com");1819 /​/​get all elements with tab index greater than 020 List<WebElement> elementsWithTabIndex = driver.findElements(By.cssSelector("[tabindex]:not([tabindex='0'])"));2122 /​/​Assert that there are no elements with tab index greater than 023 if (elementsWithTabIndex.size() > 0) {24 System.out.println("Test failed. There are elements with tabindex greater than 0.");25 } else {26 System.out.println("Test passed. All tab indexes are less than 0.");27 }28 29 /​/​Close browser window30 driver.quit();31 }32}3334/​/​Assuming that the remote client uses ChromeDriver with Selenium 4 and the desired capability is set to allow javascript3536import org.openqa.selenium.WebDriver;37import org.openqa.selenium.chrome.ChromeDriver;38import org.openqa.selenium.remote.DesiredCapabilities;39import org.openqa.selenium.remote.RemoteWebDriver;40import java.net.URL;4142public class AccessibilityTesting {43 public static void main(String[] args) throws Exception {44 /​/​Create instance of ChromeDriver45 WebDriver driver = new ChromeDriver();46 47 /​/​Navigation to web application48 driver.get("https:/​/​www.example.com");49 50 /​/​Create desired capabilities for remoteWebDriver51 DesiredCapabilities capabilities = new DesiredCapabilities();52 capabilities.setBrowserName("chrome");53 capabilities.setPlatform(Platform.LINUX); /​/​or WINDOWS or MAC54 capabilities.setCapability("enableVNC", true); /​/​to allow manual intervention55 56 /​/​Create instance of RemoteWebDriver57 RemoteWebDriver remoteDriver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);58 59 /​/​Navigation to web application60 remoteDriver.get("https:/​/​www.example.com");6162 /​/​get all elements with tabindex greater than 063 List<WebElement> elementsWithTabIndex = remoteDriver.findElements(By.cssSelector("[tabindex]:not([tabindex='0'])"));6465 /​/​Assert that there are no elements with tab index greater than 066 if (elementsWithTabIndex.size() > 0) {67 System.out.println("Test failed. There are elements with tabindex greater than 0.");68 } else {69 System.out.println("Test passed. All tab indexes are less than 0.");70 }71 72 /​/​Close browser window73 driver.quit();74 }75}

Language: Python

Framework: Selenium 4

copy
1from selenium import webdriver23# Set up Chrome driver4options = webdriver.ChromeOptions()5options.add_argument('disable-infobars')6driver = webdriver.Chrome(chrome_options=options)78# Navigate to page9driver.get('https:/​/​www.example.com/​')1011# Check for tab index values greater than 012elements = driver.find_elements_by_xpath("/​/​*[@tabindex and @tabindex > '0']")1314# Fail test if there are any tabindex values greater than 015if elements:16 raise AssertionError("Tab index values greater than 0 found on page")1718# Quit the driver19driver.quit()2021# To connect to remote client with desired capabilities, add the following code22# options = webdriver.ChromeOptions()23# options.add_argument('disable-infobars')24# desired_capabilities = options.to_capabilities()25# driver = webdriver.Remote(command_executor='http:/​/​127.0.0.1:4444/​wd/​hub', desired_capabilities=desired_capabilities)

Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing