Monday, January 9, 2012

Finding broken images in a webpage using Selenium

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?

  • 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.

26 comments:

  1. Try this for Broken images
    http://bugreaper.blogspot.in/2013/09/find-broken-images-for-application-or.html

    ReplyDelete
  2. Excellent Post. Also visit http://www.whiteboxqa.com/WBQA/google/selenium-training-BayArea.php

    ReplyDelete
  3. Appreciation for nice Update, I found something new and folks can get useful information about selenium Online Training

    ReplyDelete
  4. Thank You !! Awesome way to present your thoughts and knowledge.
    I am a beginner and I want to learn more .Keep sharing your knowledge with us.
    Selenium Interview Questions
    Selenium Tutorial
    Selenium Training in Noida

    ReplyDelete
  5. Thanks for sharing this Information, Got to learn new things from your Blog on Megento.
    Ref link : http://thecreatingexperts.com/magento-training-in-chennai/

    ReplyDelete
  6. 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.
    http://thecreatingexperts.com/software-testing-training-in-chennai/selenium-training-in-chennai/

    ReplyDelete
  7. Selenium 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

    ReplyDelete
  8. I 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.

    Selenium Training in Chennai

    ReplyDelete
  9. I have one doubt is it possible for making an merge more than one images in one. so please explain about that.
    Regards,
    Sowmiya

    Selenium Training in Chennai

    ReplyDelete
  10. check out
    http://seleniumtestingcom.blogspot.in/

    for more info on selenium

    ReplyDelete
  11. 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

    Digital Marketing Company in India

    ReplyDelete
  12. Thank you so much for sharing this informative blog. Your technical information is really useful for me. Keep update your blog.
    Regards.. Selenium Training in Chennai

    ReplyDelete

  13. Thank you for this great article which is about finding briken images in web page.keep more updates.
    SEO Company in India

    ReplyDelete
  14. Simply nice article.We do offer Selenium automation course.Refer to the link for more
    https://www.mazenetsolution.com/training-program-selenium.aspx

    ReplyDelete
  15. 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

    ReplyDelete
  16. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb.
    This 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

    ReplyDelete
  17. Thank you for sharing such great information very useful to us.
    Selenium Training in Delhi

    ReplyDelete
  18. 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



    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
    Allahabad BCom Time Table 2020
    Brij BCOM TimeTable 2020
    DAVV BCOM TimeTable 2020

    ReplyDelete
  21. This is most informative and also this post most user friendly and super navigation to all posts.

    Data Science

    Python

    Selenium

    ETL Testing

    AWS

    ReplyDelete
  22. Thanks for the informative and helpful post, obviously in your blog everything is good.. ExcelR Data Science Courses

    ReplyDelete