Tuesday 7 January 2014

How to find broken images in a page using Selenium

package programs;

import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class findbrokenimages {
static int invalidimg;
static WebDriver driver ;
public static void main(String[] args) {
try {
driver = new FirefoxDriver();
driver.get("http://ruchi-myseleniumblog.blogspot.in");
invalidimg = 0;
List allImages  = driver.findElements(By.tagName("img"));
System.out.println("Total  images are " + allImages.size());
for (int i = 0; i < allImages.size(); i++) {
WebElement img = (WebElement) allImages.get(i);
if (img != null) {
verifyimgActive(img);
}
}

System.out.println("Total invalid images are " + invalidimg);
driver.quit();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}



public static void verifyimgActive(WebElement img) {
try {
HttpResponse response = new DefaultHttpClient().execute(new HttpGet(img.getAttribute("src")));
   if (response.getStatusLine().getStatusCode() != 200)
invalidimg++;
}
catch (Exception e) {
e.printStackTrace();
}
}
}


Back to Selenium Interview Questions and Answers

4 comments:

  1. Hello Ruchi,
    This verification : if (response.getStatusLine().getStatusCode() != 200) might be tricky.
    Think that you have two false positive :
    1) Get 200 with a invalid default broken image and the test passes.
    2) Get 200 with a invalid image.
    This two possibilities are not covered.
    Hint: Use image properties width ,height if possible or convert image from url.

    ReplyDelete
  2. Hi Copied the same code and replaced website to http://www.yahoo.com. I found that this code throws "org.openqa.selenium.StaleElementReferenceException:"

    Any fix for this would be wonderful

    Thanks
    Jois

    ReplyDelete
  3. "How to find broken images in a page using Selenium" This Post was Nice very Usefull.
    Please Provide output Result as a screenshot If it is possible only...

    ReplyDelete
  4. I have written this code but i got errors: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
    java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
    at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
    at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:141)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:339)
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:158)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:120)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:98)
    at brokenPage.findbrokenimages.main(findbrokenimages.java:16)

    ReplyDelete