Skip to main content

How to Compare Two Images in Selenium

How to Compare Two Images in Selenium

Some time in testing we have to do image comparision as verification point. So, in this blog I expalin how to compare two images in selenium.

I have created one function for compare two images. you can use directly into your framework.

first of all you have to import below packages into your code.

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

Now, you can use below function for comparison of two images.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public boolean mCompareImages(BufferedImage img1, BufferedImage img2)
 {
  boolean bCompareImage = true;
  try
  {
   if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight())
   {
    for (int i = 0; i < img1.getWidth(); i++)
    {
     for (int j = 0; j < img1.getHeight(); j++)
     {
      if (img1.getRGB(i, j) != img2.getRGB(i, j))
      {
       bCompareImage = false;
      }
     }
    }
   }
   else
   {
    bCompareImage = false;
   }   
  }
  catch(Exception E)
  {
   bCompareImage = false;
  }
  
  return bCompareImage;
 }


Here, we are comparing tow images first in terms of heights and width. If Height and Width both are same then we are comparing RGB components of both images. If RGB components are same then images are same otherwise it is different.

Function is return Boolean value. If images are same then true and not same then false.

Comments

Popular posts from this blog

Reporting in Selenium

How do you Handle reporting in your project ? Method 1 : Maven - Testng - Surefire report The project will be created in Maven with  SureFire Plugin  and  SuiteXmlFile  tag in the POM.xml . The testng.xml will be called in the POM.            <plugin>                 <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-surefire-plugin</artifactId>            <version>2.18.1</version>                 <configuration>                     <suiteXmlFiles>                         <suiteXmlFile>                             ...

ExtentReports in Selenium Webdriver

ExtentReports in Selenium Webdriver What is ExtentReport? ExtentReports  is a HTML reporting library for Selenium WebDriver for Java which is extremely easy to use and creates beautiful execution reports. It shows test and step summary, test steps and status in a toggle view for quick analysis Download Download the jar below: Download ExtentReports 1.4 (1623)    Snapshot of Extent report After Executing the Script   Program Steps:  We are going to write three different testcases. Pass Warning Fail TestCase with Pass Result Navigate to http://www.guvi.in Click on Sign-in Enter the credientials Check the URL is correct or not after login   TestCase with Warning Result Verify with the Wrong URL (static String Afterloginfail="http://www.guvi.in/ ")    TestCase with fail Result Click on Menu Select Tech Challenges Verify With wrong URL. Source Code: import  java.io.File; import  j...

Handling Modal Dialog Window in Selenium Webdriver

Handling Modal Dialog Window in Selenium Webdriver In this tutorial we are going to see how to handle " Modal Dialog Window "  using Selenium Webdriver Disadvantage using Selenium Use Can't handle elements which is inside  Modal Dialog Window. Using Robot Class Using Robot Class we can handle  Modal Dialog Window. In this tutorial Open  http://vodkabears.github.io/remodal/# Click on  Show  button In the  Modal Dialog Window.  Click on Follow button  Source Code : import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import java.awt.datatransfer.StringSelection; import java.awt.event.KeyEvent; public class robot_demo { public static void main(String[] args) throws InterruptedException, AWTException { WebDrive...