If a short text alternative is not sufficient to describe an image (such as in a chart, graph, or diagram), provide short text via the image's alt attribute, and include a long description in nearby text.
Language: Java
Framework: Selenium 4
1//Assuming the website is already opened in a browser and the image to be tested is already displayed23import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;78public class AccessibilityTesting {9 10 public static void main(String[] args) {11 12 //Provide the path of the webdriver executable13 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");14 WebDriver driver = new ChromeDriver();15 16 //Navigate to the website17 driver.get("http://www.example.com");18 19 //Assuming the image has an id 'testImage' and a long description is attached in the 'title' attribute20 WebElement image = driver.findElement(By.id("testImage"));21 22 //Check if alt attribute is present for the image and its value is not empty23 if(image.getAttribute("alt") != null && !image.getAttribute("alt").isEmpty()) {24 25 //Pass the long description of the image as a comment in the code26 String longDescription = "This is a complex image with chart and graphs which provides an analysis of sales trend for last quarter";27 28 //Print the long description29 System.out.println("Long description: " + longDescription);30 31 //Assuming the long description is included in nearby text, search for the text in the page32 WebElement nearbyText = driver.findElement(By.xpath("//div[contains(text(),'sales trend for last quarter')]"));33 34 //Click on the nearby text to view the long description35 nearbyText.click();36 37 //Verify if the long description is displayed38 if(driver.getPageSource().contains(longDescription)) {39 System.out.println("Long description is displayed");40 } else {41 System.out.println("Long description is not displayed");42 }43 44 } else {45 System.out.println("Alt attribute is not provided for the image");46 }47 48 //Close the browser49 driver.quit();50 }51 52 //Code to connect to remote client with desired capabilities (commented)53 /*DesiredCapabilities capabilities = new DesiredCapabilities();54 capabilities.setCapability("browserName", "firefox");55 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);*/56}
Language: Python
Framework: Selenium 4
1# Assumptions:2# - The website under test contains images that require a long description3# - The images have an "alt" attribute that provides a short text description4# - The long description is provided in nearby text5# - The Selenium 4 and Python have been installed6# - A local driver for the specified browser has been downloaded and is available in the specified path.7# - The Chrome browser version is current with the latest released version.89from selenium.webdriver import Chrome, ChromeOptions10from selenium.webdriver.common.keys import Keys1112# Local driver for the Chrome browser 13driver_path = '/path/to/chromedriver'1415# Desired capabilities for remote client using Selenium WebDriver16capabilities = ChromeOptions()17capabilities.add_argument("--disable-extensions")18capabilities.add_argument("--disable-gpu")19capabilities.add_argument("--no-sandbox")20capabilities.add_argument("--headless")2122def test_long_description_for_complex_images():23 # Launch Chrome browser for the website under test using local driver24 driver = Chrome(executable_path=driver_path)25 # Uncomment to launch browser on remote client26 # driver = Chrome(options=capabilities, command_executor='http://[remote_client]:[port]/wd/hub')27 28 # Navigate to the URL of the website under test29 driver.get("https://example.com")30 31 # Find all the images without long descriptions32 images = driver.find_elements_by_xpath("//img[@alt and not(@aria-describedby)]") 33 34 # Loop through each image and verify the presence of long description in nearby text35 for img in images:36 # Get the alt attribute 37 alt_text = img.get_attribute('alt')38 # Find the nearby text by traversing the DOM39 nearby_text = img.find_element_by_xpath('..').text40 41 # Check if the nearby text contains the long description42 if alt_text in nearby_text:43 print(f"Image '{alt_text}' has a long description in nearby text")44 else:45 print(f"No long description found for image '{alt_text}'")46 47 # Close the browser session48 driver.quit()4950# Execute the testcase51test_long_description_for_complex_images()
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.
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.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing