General webpage functionality : Test broken images check

Check all pages for broken images.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming we have the Selenium 4 libraries imported23/​/​Create a new WebDriver object using the local driver4WebDriver driver = new ChromeDriver();5/​/​Uncomment the following code to connect to a remote client with desired capabilities6/​*7DesiredCapabilities caps = DesiredCapabilities.chrome();8caps.setCapability("platform", "Windows 10");9caps.setCapability("browserName", "chrome");10driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), caps);11*/​1213/​/​Navigate to the webpage14driver.navigate().to("http:/​/​www.website.com");1516/​/​Find all the images on the page using the tagname "img"17List<WebElement> images = driver.findElements(By.tagName("img"));1819/​/​Loop through all the images and check if they are broken20for (WebElement image : images) {21 try {22 /​/​Retrieve the "src" attribute of the image23 HttpURLConnection connection = (HttpURLConnection) new URL(image.getAttribute("src")).openConnection();24 connection.setRequestMethod("HEAD");25 connection.connect();26 int responseCode = connection.getResponseCode();27 /​/​If the response code is not 200, the image is broken28 if (responseCode != 200) {29 System.out.println("Broken image found: " + image.getAttribute("src"));30 }31 } catch (Exception e) {32 /​/​If an exception occurs, the image is broken33 System.out.println("Broken image found: " + image.getAttribute("src"));34 }35}3637/​/​Close the browser session38driver.quit();

Language: Python

Framework: Selenium 4

copy
1#Assuming Python 3 is installed23#Assuming Selenium 4 is installed and the webdriver for the required browser is downloaded and extracted to a known location45#Assuming the web application uses HTTP protocol67#Assuming the test is to run on local machine89#Assuming the web application to be tested is already running on localhost:80801011from selenium import webdriver12from selenium.webdriver.common.desired_capabilities import DesiredCapabilities1314#Local Driver15driver = webdriver.Chrome(executable_path=r'path/​to/​chromedriver.exe') #Replace with actual path to chromedriver1617#Remote Driver18#desired_cap = DesiredCapabilities.CHROME.copy()19#desired_cap['version']=''20#driver = webdriver.Remote(command_executor='http:/​/​remoteclientip:port/​wd/​hub',desired_capabilities=desired_cap)2122#Assuming all pages are accessible by visiting the home page of the application23driver.get("http:/​/​localhost:8080/​")2425#Assuming broken images have 'src' attribute pointing to an invalid URL26broken_images = []27images = driver.find_elements_by_tag_name("img")28for image in images:29 if requests.get(image.get_attribute("src")).status_code == 404:30 broken_images.append(image)3132#Assert condition to check if there are no broken images33assert not broken_images, f"There are {len(broken_images)} broken images: {broken_images}"3435#Closing the driver object36driver.quit()

Language: Javascript

Framework: Cypress

copy
1describe('Test broken images check', () => {2 it('should check for broken images on all pages', () => {3 /​/​Assuming the website has a navbar, footer, and a logo that appears on all pages4 cy.visit('/​') /​/​visits the homepage5 cy.get('img').each(($el, index, $list) => {6 cy.request($el.attr('src'))7 .then((response) => {8 expect(response.status).to.eq(200) /​/​Check status code9 })10 .catch((err) => {11 throw new Error('Could not load image!')12 })13 })14 cy.get('a').first().click() /​/​Navigate to another page15 cy.get('img').each(($el, index, $list) => {16 cy.request($el.attr('src'))17 .then((response) => {18 expect(response.status).to.eq(200) /​/​Check status code19 })20 .catch((err) => {21 throw new Error('Could not load image!')22 })23 })24 /​/​Add additional code to navigate to other pages and check for broken images25 /​/​To use remote client, add this code commented -26 /​/​ const capabilities = {27 /​/​ browserName: 'chrome', /​/​assuming the test is running on chrome28 /​/​ 'goog:chromeOptions': {29 /​/​ args: ['--remote-debugging-port=9222'], /​/​running chrome in remote debugging mode30 /​/​ },31 /​/​ };32 /​/​ cy.visit('http:/​/​localhost:4444/​wd/​hub', { capabilities: capabilities }) /​/​Connecting to remote client33 })34})

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