Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find broken images in page & image replace by another image

Hi I am using selenium webdriver 2.25.0 & faceing the some serious issues,

  1. how to find broken images in a page using Selenium Webdriver
  2. How to find the image is replace by another image having same name (This is also bug) using webdriver.

Thanks in advance for your value-able suggestions.

like image 420
Chetan Avatar asked May 28 '13 05:05

Chetan


People also ask

What are broken images in a website?

What are Broken Image Links? A broken image is nothing more than a link that transfers users to a 404 error or an underloaded image icon. Underloaded images and 404 pages are not only unattractive. They provoke rejection and degrade conversion.

How do I find broken images on WordPress?

The easiest way to detect broken images on your WordPress website is using a plugin. One of such plugins is the Broken Link Checker. With this plugin, you can scan your site's internal and external links for any broken links and images.


2 Answers

The accepted answer requires that you use a proxy with an extra call to each image to determine if the images are broken or not.

Fortunately, there is another way you can do this using only javascript (I'm using Ruby, but you can use the same code in any executeScript method across the WebDriver bindings):

images = @driver.find_elements(:tag_name => "img")
broken_images = images.reject do |image|
  @driver.execute_script("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", image)
end
# broken_images now has an array of any images on the page with broken links
# and we want to ensure that it doesn't have any items
assert broken_images.empty?

To your other question, I would recommend just taking a screenshot of the page and having a human manually verify the resulting screenshot has the correct images. Computers can do the automation work, but humans do have to check and verify its results from time to time :)

like image 125
bbbco Avatar answered Oct 21 '22 08:10

bbbco


The next lines are not optimized, but they could find broken images:

List<WebElement> imagesList = _driver.findElements(By.tagName("img"));
for (WebElement image : imagesList)
{
    HttpResponse response = new DefaultHttpClient().execute(new HttpGet(image.getAttribute("src");));
    if (response.getStatusLine().getStatusCode() != 200)
        // Do whatever you want with broken images
}

Regarding your second issue, I think I didn't understand it correctly. Could you explain it with more detail?

like image 22
Johnbo Avatar answered Oct 21 '22 06:10

Johnbo