What is broken images?
Broken image as the name signifies the web page code refers to an image location(i.e src of the image) is either incorrect or out of date. When you try to search that image at the specified location written in the code you wont be able to find that image.
Reason for broken images :-
There are three possible reasons why your images are not showing up on your pages as expected:
(1) The image file is not located in the same location that is specified in your IMG tag
(2) The image does not have the same file name as specified in your tag and
(3) The image file is either corrupted or damaged.
Code for finding broken images through Selenium :-
WebDriver driver = new FirefoxDriver();
EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver (driver);
// Storing all the image elemt in the variable allImages
List<WebElement> allImages =
eventFiringWebDriver.findElements(By.tagName("img"));
int countBrokenImages = 0;
// Declaring a dynamic string of array which will store src of all the broken images
List<String> BrokenimageUrl = new ArrayList<String>();
String script = "return (typeof arguments[0].naturalWidth!=\"undefined\" &&
arguments[0].naturalWidth>0)";
for (WebElement image : allImages)
{
Object imgStatus = eventFiringWebDriver.executeScript(script, image);
if(imgStatus.equals(false))
{
String currentImageUrl = image.getAttribute("src");
String imageUrl = currentImageUrl ;
BrokenimageUrl.add(imageUrl);
countBrokenImages++;
}
}
// Printing the src of the broken images if any
System.out.println("Number of broken images found in the page : " +countBrokenImages);
for (String z : BrokenimageUrl)
{
System.out.println(z);
}
Hope the above code resolved your problem.
Broken image as the name signifies the web page code refers to an image location(i.e src of the image) is either incorrect or out of date. When you try to search that image at the specified location written in the code you wont be able to find that image.
Reason for broken images :-
There are three possible reasons why your images are not showing up on your pages as expected:
(1) The image file is not located in the same location that is specified in your IMG tag
(2) The image does not have the same file name as specified in your tag and
(3) The image file is either corrupted or damaged.
Code for finding broken images through Selenium :-
WebDriver driver = new FirefoxDriver();
EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver (driver);
// Storing all the image elemt in the variable allImages
List<WebElement> allImages =
eventFiringWebDriver.findElements(By.tagName("img"));
int countBrokenImages = 0;
// Declaring a dynamic string of array which will store src of all the broken images
List<String> BrokenimageUrl = new ArrayList<String>();
String script = "return (typeof arguments[0].naturalWidth!=\"undefined\" &&
arguments[0].naturalWidth>0)";
for (WebElement image : allImages)
{
Object imgStatus = eventFiringWebDriver.executeScript(script, image);
if(imgStatus.equals(false))
{
String currentImageUrl = image.getAttribute("src");
String imageUrl = currentImageUrl ;
BrokenimageUrl.add(imageUrl);
countBrokenImages++;
}
}
// Printing the src of the broken images if any
System.out.println("Number of broken images found in the page : " +countBrokenImages);
for (String z : BrokenimageUrl)
{
System.out.println(z);
}
Hope the above code resolved your problem.