Accessibility testing : Alt attribute for img tags

All img tags must have alt attributes.

Language: Java

Framework: Selenium 4

copy
1Assumptions: 21. The web page to be tested is accessible through a URL. 32. Selenium 4 is installed on the local system. 45/​/​ Importing required packages6import org.openqa.selenium.By;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.chrome.ChromeDriver;10import org.openqa.selenium.remote.DesiredCapabilities;11import org.openqa.selenium.remote.RemoteWebDriver;12import java.net.URL;1314public class AltAttributeTest {1516 public static void main(String[] args) throws Exception {1718 /​/​ Setting up local driver19 WebDriver driver = new ChromeDriver();20 /​/​ Commented code for remote connection21 /​*22 DesiredCapabilities capabilities = DesiredCapabilities.chrome();23 URL url = new URL("http:/​/​Selenium hub IP:port/​wd/​hub");24 WebDriver driver = new RemoteWebDriver(url, capabilities);25 */​2627 /​/​ Navigating to the webpage28 driver.navigate().to("http:/​/​www.example.com");2930 /​/​ Getting all img tags31 java.util.List<WebElement> images = driver.findElements(By.tagName("img"));32 int count = 0;3334 /​/​ Looping through all img tags35 for (WebElement image : images) {36 String alt = image.getAttribute("alt");37 /​/​ Checking if alt attribute is empty or null38 if (alt == null || alt.isEmpty()) {39 System.out.println("Image without alt attribute found: " + image.getAttribute("src"));40 count++;41 }42 }4344 /​/​ Checking if any img tag is missing alt attribute45 if (count > 0) {46 System.out.println("Failed: " + count + " img tags are missing alt attributes");47 } else {48 System.out.println("Passed: All img tags have alt attributes");49 }5051 /​/​ Closing the driver52 driver.quit();53 }54}

Language: Python

Framework: Selenium 4

copy
1from selenium import webdriver2from selenium.webdriver.common.by import By34#Assuming the webpage is loaded5#Assuming all the img tags present on the page have alt attributes67#to initiate a local driver8driver = webdriver.Chrome(executable_path="path/​to/​chromedriver")910#to connect to remote client with desired capabilities11#uncomment the following lines and update the capabilities as needed12#from selenium.webdriver.common.desired_capabilities import DesiredCapabilities13#capabilities = DesiredCapabilities.CHROME.copy()14#capabilities['platform'] = "WINDOWS"15#capabilities['version'] = "10"16#driver = webdriver.Remote(17# command_executor='http:/​/​<remote-client-ip>:4444/​wd/​hub',18# desired_capabilities=capabilities)1920#to get all the img tags present on the current page21images = driver.find_elements(By.TAG_NAME, 'img')2223#to check if all the img tags have alt attributes24for image in images:25 alt_text = image.get_attribute('alt')26 assert alt_text is not None, "Alt Text not present for Image with Source: "+image.get_attribute('src')27 28driver.close()

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