Road to verify images using java webdriver

In this post I have explained that how to verify images in webdriver using java. As webdriver does not provide direct any function to image verification, but we can verify images by taking two screen shots of whole web page using “TakesScreenshot” webdriver function, one at script creation time and another at execution time,

In below example I have created a sample script in which first I captured a Google home page screen shot and saved (GoogleInput.jpg) into my project, Another screen shot “GoogleOutput.jpg” captured of same page at test executing time and saved into project. I compared both images if they are not same then test will script fail.
Here is sample code for same

 package com.test;

 import java.awt.image.BufferedImage;
 import java.awt.image.DataBuffer;
 import java.io.File;
 import java.io.IOException;
 import java.util.concurrent.TimeUnit;
 import javax.imageio.ImageIO;
 import org.apache.commons.io.FileUtils;
 import org.openqa.selenium.OutputType;
 import org.openqa.selenium.TakesScreenshot;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.firefox.FirefoxDriver;
 import org.testng.Assert;
 import org.testng.annotations.AfterSuite;
 import org.testng.annotations.BeforeSuite;
 import org.testng.annotations.Test;

 public class ImageComparison {

      public WebDriver driver;
      private String baseUrl;
     
      @BeforeSuite
      public void setUp() throws Exception {
         driver = new FirefoxDriver();
         baseUrl = "https://www.google.co.in/";
         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);      
      }
        
      @AfterSuite
      public void tearDown() throws Exception {
         driver.quit();    
      }
       
      @Test
      public void testImageComparison()
               throws IOException, InterruptedException
      {
         driver.navigate().to(baseUrl);
         File screenshot = ((TakesScreenshot)driver).
getScreenshotAs(OutputType.FILE);
         Thread.sleep(3000);
         FileUtils.copyFile(screenshot, new File("GoogleOutput.jpg"));

         File fileInput = new File("GoogleInput.jpg");
         File fileOutPut = new File("GoogleOutput.jpg");

         BufferedImage bufileInput = ImageIO.read(fileInput);
         DataBuffer dafileInput = bufileInput.getData().getDataBuffer();
         int sizefileInput = dafileInput.getSize();                     
         BufferedImage bufileOutPut = ImageIO.read(fileOutPut);
         DataBuffer dafileOutPut = bufileOutPut.getData().getDataBuffer();
         int sizefileOutPut = dafileOutPut.getSize();
         Boolean matchFlag = true;
         if(sizefileInput == sizefileOutPut) {                         
            for(int j=0; j<sizefileInput; j++) {
                  if(dafileInput.getElem(j) != dafileOutPut.getElem(j)) {
                        matchFlag = false;
                        break;
                  }
             }
         }
         else                            
            matchFlag = false;
         Assert.assertTrue(matchFlag, "Images are not same");    
      }
 }



10 comments:

  1. how to take the image manually

    ReplyDelete
    Replies
    1. After creating test, run test script it will automatically capture image and save into folder. Make this image as input this image is your expected image again when you run test it will capture image and compare from previous.

      Delete
  2. This verifies the images based on size. Rt? But how to verify the contents of the images displayed on a webpage?

    ReplyDelete
  3. I have followed your code and getting this error:
    Exception in thread "main" javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(Unknown Source)
    at ICLICKStagePackage.MainClass.Mediaeffectiveness(MainClass.java:562)
    at ICLICKStagePackage.MainClass.main(MainClass.java:709)
    Please help me out what does it require.

    ReplyDelete
  4. Even i am getting the same error.

    ReplyDelete
  5. Make a screenshot "GoogleInput.jpg" in the project folder, better change the name of the output file "GoogleOutput.jpg" to "GoogleInput.jpg" and run

    ReplyDelete
  6. Make a screenshot "GoogleInput.jpg" in the project folder, better change the name of the output file "GoogleOutput.jpg" to "GoogleInput.jpg" and run

    ReplyDelete
  7. You are trying to read the GoogleInput.jpg just after naming it. where is the content in it to read?

    ReplyDelete
  8. Hey Guys

    If you will simply copy and run the above code it will throw error "Can't read input file!"

    The concept here is comparing a image captured earlier with image captured now.

    So its better you change the @Test block as I shown below and capture the GoogleInput.jpg screen shot, verify in your project folder if the GoogleInput.jpg is captured or not and then again comeback and use the same code given in this website without changing @Test block to compare the Input image with Out put image.

    @Test

    public void testImageComparison()

    throws IOException, InterruptedException

    {

    driver.navigate().to(baseUrl);

    File screenshot = ((TakesScreenshot)driver).

    getScreenshotAs(OutputType.FILE);

    Thread.sleep(3000);

    FileUtils.copyFile(screenshot, new File("GoogleInput.jpg"));
    File fileInput = new File("GoogleInput.jpg");

    }

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution