Accessibility testing : Avoid images of text

Avoid images of text, except in cases such as logos.

Language: Java

Framework: Selenium 4

copy
1/​/​ Assumptions: 2/​/​ A website page is loaded in the WebDriver with the name 'driver'. 3/​/​ The images with text will have an attribute 'alt' which will be used to determine 4/​/​ if the image contains text.56import org.openqa.selenium.By;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.remote.DesiredCapabilities;9import org.openqa.selenium.remote.RemoteWebDriver;10import java.net.URL;1112public class AccessibilityTesting {1314 public static void main(String[] args) {1516 /​/​ Using local WebDriver17 WebDriver driver = new ChromeDriver();1819 /​/​ Uncomment the below code to connect to remote client with desired capabilities20 /​*21 DesiredCapabilities capabilities = DesiredCapabilities.chrome();22 capabilities.setCapability("version", "83.0");23 capabilities.setCapability("platform", "Windows 10");24 RemoteWebDriver driver = null;25 try {26 driver = new RemoteWebDriver(new URL("http:/​/​192.168.0.1:4444/​wd/​hub"), capabilities);27 } catch (MalformedURLException e) {28 e.printStackTrace();29 }30 */​3132 driver.get("https:/​/​www.example.com");3334 /​/​ Finding all image elements in the web page35 List<WebElement> images = driver.findElements(By.tagName("img"));3637 /​/​ Iterating through each image and checking if it contains text38 for (WebElement img : images) {39 String altText = img.getAttribute("alt");40 if (altText != null && !altText.isEmpty() && !altText.toLowerCase().equals("logo")) {41 System.out.println("Image with text is present on the page");42 break;43 }44 }4546 driver.quit();47 }48}

Language: Python

Framework: Selenium 4

copy
1Python Code:23from selenium.webdriver import Chrome, ChromeOptions4from selenium.webdriver.common.keys import Keys5from selenium.webdriver.common.desired_capabilities import DesiredCapabilities67#Assuming the website is developed using HTML code89#Creating ChromeOptions object and setting the path to the local driver10chrome_options = ChromeOptions()11chrome_options.add_argument("--no-sandbox")12chrome_options.add_argument("--disable-dev-shm-usage")13driver = Chrome(options=chrome_options, executable_path=r'C:\webdrivers\chromedriver.exe')1415#Connecting to remote client by setting the desired capabilities for the driver16remote_url = 'http:/​/​localhost:4444/​wd/​hub'17caps = DesiredCapabilities().CHROME.copy()18caps['acceptInsecureCerts'] = True19driver = webdriver.Remote(remote_url, DesiredCapabilities.CHROME)2021#Opening the website URL22driver.get("https:/​/​www.example.com")2324#Checking for images of text except in cases of logo25images_of_text = driver.find_elements_by_tag_name('img[alt*=text]')26for image in images_of_text:27 if 'logo' not in image.get_attribute('alt').lower():28 print('FAIL: Image of text found that is not a logo')29 30#Closing the browser31driver.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