We were checking for broken URLs using Selenium in the previous post. Now we are going to see how to find broken images in a webpage. Broken images are also called Red-X images. We have some tools in the market for finding red-x images on a website or webpage. For example, InSite which is a windows based application.
The same functionality can be possible with Selenium script. How do we do that?
How to extract the image size from the image property?
String s = selenium.getEval("(!" + currentImage + ".complete) ? false : !(typeof " + currentImage + ".naturalWidth != \"undefined\" && " + currentImage + ".naturalWidth == 0);");
if(s.equals("false"))
{
System.out.println(selenium.getEval(currentImage + ".src"));
}
Find the complete script below:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestBase;
public class FindBrokenImages extends SeleneseTestBase{
DefaultSelenium selenium;
public int invaildImg;
@BeforeMethod
public void SetUp()
{
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.yahoo.com/");
selenium.start();
}
@Test
public void testMethod() throws IOException {
invaildImg=0;
try
{
selenium.open("/");
}
catch (Exception e)
{
selenium.waitForPageToLoad("30000");
}
int imageCount = selenium.getXpathCount("//img").intValue();
FileOutputStream fout = new FileOutputStream ("broken_images.txt", true);
new PrintStream(fout).println("URL : " + selenium.getLocation());
new PrintStream(fout).println("--------------------------------------------");
for (int i = 1; i < imageCount; i++) {
String currentImage = "this.browserbot.getUserWindow().document.images[" + i + "]";
String s = selenium.getEval("(!" + currentImage + ".complete) ? false : !(typeof " + currentImage + ".naturalWidth != \"undefined\" && " + currentImage + ".naturalWidth == 0);");
if(s.equals("false")){
// System.out.println(selenium.getEval(currentImage + ".src"));
new PrintStream(fout).println(selenium.getEval(currentImage + ".src"));
invaildImg++; }
}
new PrintStream(fout).println("Total broken images = " + invaildImg);
new PrintStream(fout).println(" ");
fout.close();
}
@AfterMethod
public void tearDown()
{
selenium.close();
selenium.stop();
}
}
The same functionality can be possible with Selenium script. How do we do that?
- We need to find the number of images on the page
- We need to track the properties of each and every image.
- Finally we need to extract the image size amd check if its broken or not
How to find the number of images on the page?
We can find the number using selenium.getXpathCount("//img").intValue() method.
selenium=new DefaultSelenium("localhost", 4444, "*firefox", "http://www.yahoo.com");
selenium.start();
selenium.open("/");
int linkCount = selenium.getXpathCount("//img").intValue();
How to track the properties for each and every image?
We can use the for loop and track the properties of the image one by one.
for (int i = 1; i < imageCount; i++)
{
String currentImage = "this.browserbot.getUserWindow().document.images[" + i + "]";
}
How to extract the image size from the image property?
String s = selenium.getEval("(!" + currentImage + ".complete) ? false : !(typeof " + currentImage + ".naturalWidth != \"undefined\" && " + currentImage + ".naturalWidth == 0);");
if(s.equals("false"))
{
System.out.println(selenium.getEval(currentImage + ".src"));
}
Find the complete script below:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestBase;
public class FindBrokenImages extends SeleneseTestBase{
DefaultSelenium selenium;
public int invaildImg;
@BeforeMethod
public void SetUp()
{
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.yahoo.com/");
selenium.start();
}
@Test
public void testMethod() throws IOException {
invaildImg=0;
try
{
selenium.open("/");
}
catch (Exception e)
{
selenium.waitForPageToLoad("30000");
}
int imageCount = selenium.getXpathCount("//img").intValue();
FileOutputStream fout = new FileOutputStream ("broken_images.txt", true);
new PrintStream(fout).println("URL : " + selenium.getLocation());
new PrintStream(fout).println("--------------------------------------------");
for (int i = 1; i < imageCount; i++) {
String currentImage = "this.browserbot.getUserWindow().document.images[" + i + "]";
String s = selenium.getEval("(!" + currentImage + ".complete) ? false : !(typeof " + currentImage + ".naturalWidth != \"undefined\" && " + currentImage + ".naturalWidth == 0);");
if(s.equals("false")){
// System.out.println(selenium.getEval(currentImage + ".src"));
new PrintStream(fout).println(selenium.getEval(currentImage + ".src"));
invaildImg++; }
}
new PrintStream(fout).println("Total broken images = " + invaildImg);
new PrintStream(fout).println(" ");
fout.close();
}
@AfterMethod
public void tearDown()
{
selenium.close();
selenium.stop();
}
}
The above script will identify all the broken images(if any) in yahoo.com and store the image URLs in a notepad file called broken_images.txt. If you want to check the broken images for N number of URLs, you can pass the parameters through Data Provider concept or Excel sheet using JXL package.
Try this for Broken images
ReplyDeletehttp://bugreaper.blogspot.in/2013/09/find-broken-images-for-application-or.html
Excellent Post. Also visit http://www.whiteboxqa.com/WBQA/google/selenium-training-BayArea.php
ReplyDeleteAppreciation for nice Update, I found something new and folks can get useful information about selenium Online Training
ReplyDeleteThank You !! Awesome way to present your thoughts and knowledge.
ReplyDeleteI am a beginner and I want to learn more .Keep sharing your knowledge with us.
Selenium Interview Questions
Selenium Tutorial
Selenium Training in Noida
Thanks for sharing this Information, Got to learn new things from your Blog on Megento.
ReplyDeleteRef link : http://thecreatingexperts.com/magento-training-in-chennai/
Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.
ReplyDeletehttp://thecreatingexperts.com/software-testing-training-in-chennai/selenium-training-in-chennai/
Valuable information thanks for sharing Selenium Online Training
ReplyDeleteSelenium is not just a single tool or a utility, rather a package of several testing tools and for the same reason it is referred to as a Suite. This blog gives great view for selenium beginners. Learn Selenium from the best Selenium Online Training in your locality at CatchExperts.com
ReplyDeleteI do trust all of the concepts you’ve presented on your post. They’re really convincing and will definitely work. Still, the posts are too brief for newbies. May you please extend them a little from subsequent time?Also, I’ve shared your website in my social networks.
ReplyDeleteSelenium Training in Chennai
I have one doubt is it possible for making an merge more than one images in one. so please explain about that.
ReplyDeleteRegards,
Sowmiya
Selenium Training in Chennai
check out
ReplyDeletehttp://seleniumtestingcom.blogspot.in/
for more info on selenium
wow very well information you had shared here. This is important thing because when we are running a business first we have to make our focusing area and what we are ready for serve among the people. And really i am much inspired with this 10 steps. Surely it will be useful for craeting the better mindset among our business. Thank you and i am looking forward your more information here
ReplyDeleteDigital Marketing Company in India
Thank you so much for sharing this informative blog. Your technical information is really useful for me. Keep update your blog.
ReplyDeleteRegards.. Selenium Training in Chennai
ReplyDeleteThank you for this great article which is about finding briken images in web page.keep more updates.
SEO Company in India
Simply nice article.We do offer Selenium automation course.Refer to the link for more
ReplyDeletehttps://www.mazenetsolution.com/training-program-selenium.aspx
Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information... Java Training in Chennai | Blue prism Training in Chennai
ReplyDeleteThis is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb.
ReplyDeleteThis trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here
Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Thank you for sharing such great information very useful to us.
ReplyDeleteSelenium Training in Delhi
Thanks For Sharing The InFormation The Information Shared Is Very Valuable Please Keeep updating Us Time Just Went On reading the article Python Online Course Data Science Online Course Data Science Online Course Hadoop Online Course Awsw Online Course
ReplyDeleteThis comment has been removed by the author.
ReplyDeletegreat very informative....
ReplyDeleteInterview Question for CTS Placement
Cognizant Interview Questions For Fresher
Cognizant Interview Questions
Resume Coustomer Service Executive
Resume For Bank Job
Resume Cyber security Engineer
Resume Data Base Developer
Resume DeputyManager
Resume Design Engineer
Resume Desktop Support Engineer
I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
ReplyDeleteAllahabad BCom Time Table 2020
Brij BCOM TimeTable 2020
DAVV BCOM TimeTable 2020
I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.
ReplyDeletemobile phone repair in Novi
iphone repair in Novi
cell phone repair in Novi
phone repair in Novi
tablet repair in Novi
ipad repair in Novi
mobile phone repair Novi
iphone repair Novi
cell phone repair Novi
phone repair Novi
mobile phone repair in Fredericksburg
ReplyDeleteiphone repair in Fredericksburg
cell phone repair in Fredericksburg
phone repair in Fredericksburg
tablet repair in Fredericksburg
mobile phone repair in Fredericksburg
mobile phone repair Fredericksburg
iphone repair Fredericksburg
cell phone repair Fredericksburg
phone repair Fredericksburg
This is most informative and also this post most user friendly and super navigation to all posts.
ReplyDeleteData Science
Python
Selenium
ETL Testing
AWS
Thanks for the informative and helpful post, obviously in your blog everything is good.. ExcelR Data Science Courses
ReplyDelete