Accessibility testing : Alt attribute for decorative images

Images that are decorative, used for formatting, or contain content already conveyed in text have a null alt attribute or are implemented as CSS background images.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming the webpage is already opened using local driver2/​/​Connecting to remote client with desired capabilities commented out below34/​/​importing necessary packages5import org.openqa.selenium.By;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.remote.DesiredCapabilities;9import org.openqa.selenium.remote.RemoteWebDriver;10import java.net.URL;1112/​/​Defining remote driver configuration13DesiredCapabilities capabilities = new DesiredCapabilities();14capabilities.setCapability("browserName", "chrome");15capabilities.setCapability("browserVersion", "latest");16capabilities.setCapability("platformName", "Linux");1718/​/​Initializing the remote driver with desired capabilities19WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);2021/​/​Locating all the images on the webpage22List<WebElement> allImages = driver.findElements(By.tagName("img"));2324/​/​Iterating through each image to check for null alt attributes25for(WebElement image : allImages) {26 String altAttribute = image.getAttribute("alt");27 if(altAttribute == null || altAttribute.isEmpty()) {28 System.out.println("This image has a null alt attribute: " + image.getAttribute("src"));29 }30}3132/​/​Assuming all images implemented as CSS background images have been identified and their alt attributes set accordingly.

Language: Python

Framework: Selenium 4

copy
1from selenium import webdriver2from selenium.webdriver.common.by import By3from selenium.webdriver.support.ui import WebDriverWait4from selenium.webdriver.support import expected_conditions as EC56#Assuming the web application is already loaded in the browser7#Assuming we are testing the home page of the website8#Assuming all decorative images have null alt attribute or implemented as CSS background images910#Local ChromeDriver path11chrome_driver_path = '/​path/​to/​chromedriver'1213# connecting to remote client with desired capabilities14# Uncomment below code to connect to remote client15'''16from selenium.webdriver.common.desired_capabilities import DesiredCapabilities17desired_cap = {'browserName': 'chrome'}18driver = webdriver.Remote(command_executor='http:/​/​localhost:4444/​wd/​hub', desired_capabilities=desired_cap)19'''2021# Set Chrome browser options22chrome_options = webdriver.ChromeOptions()23chrome_options.add_argument('--incognito')2425# Launch Chrome browser26driver = webdriver.Chrome(chrome_driver_path, options=chrome_options)2728# Wait for the page to load29wait = WebDriverWait(driver, 10)30page_body = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'body')))3132# Find all the images with alt attribute 'null'33decorative_images = driver.find_elements_by_xpath("/​/​img[@alt='null']")3435# Check if any decorative image has alt attribute 'null'36if len(decorative_images) > 0:37 print("Test case passed: All decorative images have null alt attribute")38else:39 print("Test case failed: Decorative image(s) found with alt attribute other than null")4041# Close the browser42driver.quit()

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