Accessibility testing : Text alternative for image buttons or links

If an image or icon is used as a button or link, the image has a text alternative sufficient to describe the purpose of the button or link.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming the website's home page has image buttons or links23import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;78public class AccessibilityTesting {9 public static void main(String[] args) {10 11 /​/​Assuming ChromeDriver is installed and set in PATH12 WebDriver driver = new ChromeDriver();13 14 /​/​Assuming website is hosted locally15 driver.get("http:/​/​localhost:8080/​home");16 17 /​/​maximize browser window18 driver.manage().window().maximize();19 20 /​/​Assuming there are only image buttons and links on the home page21 /​/​find all image buttons and links using img tag22 /​/​find all image links using a tag23 WebElement[] imageButtonsLinks = driver.findElements(By.tagName("img")).toArray(new WebElement[0]);24 WebElement[] imageLinks = driver.findElements(By.tagName("a")).toArray(new WebElement[0]);2526 /​/​iterate through all image buttons and links27 for (WebElement imageButtonLink : imageButtonsLinks) {28 29 /​/​check if the alt attribute is present30 if (imageButtonLink.getAttribute("alt") == null || imageButtonLink.getAttribute("alt").isEmpty()) {31 /​/​if alt attribute is not present or empty, then fail32 System.out.println("FAIL: Alt text missing for image button or link - " + imageButtonLink.getAttribute("src"));33 }34 }35 36 /​/​iterate through all image links37 for (WebElement imageLink : imageLinks) {38 39 /​/​check if the alt attribute is present40 if (imageLink.getAttribute("alt") == null || imageLink.getAttribute("alt").isEmpty()) {41 /​/​if alt attribute is not present or empty, then fail42 System.out.println("FAIL: Alt text missing for image link - " + imageLink.getAttribute("href"));43 }44 }45 46 /​/​Assuming the Remote WebDriver is to be used with desired capabilities47 /​/​uncomment below code to use remote client with desired capabilities48 /​*49 DesiredCapabilities capabilities = DesiredCapabilities.chrome();50 /​/​add desired capabilities here (if any)51 /​/​...52 WebDriver driver = null;53 try {54 driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);55 } catch (MalformedURLException e) {56 e.printStackTrace();57 }58 */​59 60 driver.quit(); /​/​close browser61 }62}

Language: Python

Framework: Selenium 4

copy
1from selenium import webdriver2from selenium.webdriver.common.keys import Keys3from selenium.webdriver.common.by import By4from selenium.webdriver.support.ui import WebDriverWait5from selenium.webdriver.support import expected_conditions as EC678# Assuming the website is already open and the page with the image buttons or links is the current page9driver = webdriver.Firefox()10driver.maximize_window()1112# Retrieving all the image buttons and links on the page13img_btns = driver.find_elements(By.XPATH, "/​/​img[@role='button']")14img_links = driver.find_elements(By.XPATH, "/​/​img[@role='link']")1516# Looping through all the image buttons and links to check if they have text alternative attributes17for img_btn in img_btns:18 alt_text = img_btn.get_attribute("alt")19 if alt_text == "":20 print("Image button does not have a text alternative attribute")2122for img_link in img_links:23 alt_text = img_link.get_attribute("alt")24 if alt_text == "":25 print("Image link does not have a text alternative attribute")2627# Adding support for remote driver with desired capabilities28# Assuming the remote driver is accessible and running with the desired capabilities set29from selenium.webdriver.common.desired_capabilities import DesiredCapabilities3031caps = DesiredCapabilities.FIREFOX.copy()32caps['acceptInsecureCerts'] = True3334remote_driver = webdriver.Remote(35 command_executor='http:/​/​127.0.0.1:4444/​wd/​hub',36 desired_capabilities=caps)3738# Assuming the website is already open and the page with the image buttons or links is the current page39remote_driver.get("https:/​/​www.example.com")4041# Retrieving all the image buttons and links on the page42img_btns = remote_driver.find_elements(By.XPATH, "/​/​img[@role='button']")43img_links = remote_driver.find_elements(By.XPATH, "/​/​img[@role='link']")4445# Looping through all the image buttons and links to check if they have text alternative attributes46for img_btn in img_btns:47 alt_text = img_btn.get_attribute("alt")48 if alt_text == "":49 print("Image button does not have a text alternative attribute")5051for img_link in img_links:52 alt_text = img_link.get_attribute("alt")53 if alt_text == "":54 print("Image link does not have a text alternative attribute")

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