1

I need to test images on a webpage. How should I deal with the following situation:

  1. To check whether all images are loaded.
  2. To check if broken images are loaded.
  3. To that the description in Alt="" if the image is not loaded.

Kindly suggest how should I do this. I need a code in Java which works like the like given: http://tools.seochat.com/tools/broken-links-images-tool

4
  • 3
    Did you try any code? no one will give you ready made full code. You should try and share your code when you get problem. Jan 21, 2015 at 12:30
  • Hi kate, I tried using following code which i took from web, i tried manipulating code but it was not successful. Jan 22, 2015 at 12:34
  • Hello John, could you provide your code in the question?
    – amazpyel
    Jan 22, 2015 at 22:00
  • Hi,I need a code in java which works like the like given below,tools.seochat.com/tools/broken-links-images-tool Jan 23, 2015 at 10:11

1 Answer 1

4

With Selenium

I am not sure Selenium should be used as a link/image checker. In an ideal world you would define which images are on a page, if the images exist you could check their ALT tag. Then your tests verifies those predefined situations are still there.

This blog post shows how you can find images based on their ALT content:

driver.findElement(By.cssSelector("img[alt='Previous']"));

I get the feel you looking for some code that magically detects what images exist on a page and if they are correct. This is what some link checkers do, they check the source of an image or link exists. Then check if they have a ALT text, but do not check the correct value. The tool you linked to is a web-based tool and the magic happens on their back-end. You cannot re-use their work without the back-end code.

This blog post shows how you can loop tru all images on a page with Selenium in Java. If you combine that with the first blog post you can then check the ALT text:

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();
    }
  }
}

Also look into the .getAttribute() function of a WebElement: https://stackoverflow.com/questions/23394828/how-to-get-the-value-of-an-attribute-using-xpath

Open Source LinkCheckers:

I think you are looking for something like this:

1
  • thanks Niels, working on it will get back to you if any problem. Jan 29, 2015 at 17:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.