Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
April 9, 2017

Image Comparison in Selenium





Image Comparison in selenium will discuss about how to compare two images in selenium. Practically speaking image comparison is not possible with selenium web driver but when there is a specific need then we can depend on third party API to achieve this functionality. AShot is of the API’s we can use to compare two images.

To compare two images, we need to capture the screenshot of the particular element and we can store it somewhere in the project folder structure and we can capture same image dynamically while automating the application and we can compare this image/screenshot with the image/screenshot which we captured and placed in the folder structure for the reference(i.e. expected).

As we already saw how to capture the screenshot of a particular web element using AShot in one of our previous blogs. Will use the same approach to capture the screenshot and will use the same to compare the images/screenshots.  

Here will take two examples; one is positive scenario (will take the screenshot and will compare the same thing dynamically captured screenshot); in this case test script should PASS. And another one is negative scenario ( will add small pixel to the captured(i.e.expected) screenshot and compare the same thing dynamically captured screenshot); in this case test script should FAIL.




Below is the sample program to capture the web element screenshot (i.e. expected image/reference image):

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import javax.imageio.ImageIO;
import java.io.File;
 
public class TakeWebElementScreenshot
{
   public static void main(String args[]) throws Exception
   {
       System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\geckodriver.exe");
       WebDriver driver = new FirefoxDriver();
 
       driver.get("http://demo.automationtesting.in/Register.html");
       Thread.sleep(2000);
 
       WebElement webElement = driver.findElement(By.cssSelector("#imagetrgt"));
 
       Screenshot screenshot = new AShot().takeScreenshot(driver,webElement);
       ImageIO.write(screenshot.getImage(),"PNG",new File(System.getProperty("user.dir") +"\\Images\\ElementScreenshot.png"));
 
       Thread.sleep(2000);
       driver.quit();
   }
}





Below is the sample code for Positive scenario. Here we will compare two images which are same:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.comparison.ImageDiff;
import ru.yandex.qatools.ashot.comparison.ImageDiffer;

public class ImageComparisonPositive 
{
    WebDriver driver;
    
    @Test
    public void imageComaparision() throws IOException
    {
        System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.get("http://demo.automationtesting.in/Register.html");
        
        WebElement logoImage = driver.findElement(By.cssSelector("#imagetrgt"));
        
        BufferedImage expectedImage = ImageIO.read(new File(System.getProperty("user.dir") +"\\Images\\ElementScreenshot.png"));
        Screenshot logoImageScreenshot = new AShot().takeScreenshot(driver, logoImage);
        BufferedImage actualImage = logoImageScreenshot.getImage();
                
        ImageDiffer imgDiff = new ImageDiffer();
        ImageDiff diff = imgDiff.makeDiff(actualImage, expectedImage);
        Assert.assertFalse(diff.hasDiff(),"Images are Same");
                
        driver.quit();
    }
}




Below is the sample code for Negative scenario. Here we will compare two images which are NOT same:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.comparison.ImageDiff;
import ru.yandex.qatools.ashot.comparison.ImageDiffer;

public class ImageComparisonNegative {
    
    WebDriver driver;
    
    @Test
    public void imageComaparision() throws IOException
    {
        System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.get("http://demo.automationtesting.in/Register.html");
        
        WebElement logoImage = driver.findElement(By.cssSelector("#imagetrgt"));
        
        BufferedImage expectedImage = ImageIO.read(new File(System.getProperty("user.dir") +"\\Images\\ElementScreenshotWithPixle.png"));
        Screenshot logoImageScreenshot = new AShot().takeScreenshot(driver, logoImage);
        BufferedImage actualImage = logoImageScreenshot.getImage();
                
        ImageDiffer imgDiff = new ImageDiffer();
        ImageDiff diff = imgDiff.makeDiff(actualImage, expectedImage);
        Assert.assertFalse(diff.hasDiff(),"Images are Same");
                
        driver.quit();
    }
}

This way we can compare two images which are same or not using Ashot. Here the concept of showing negative scenario is to make sure that the scenarios working fine even there is a small change in the expected image.

By using this we can compare company logos and some important images which should not change.

Please watch the youtube video for better understanding.



Share this post: on Twitter on Facebook

Generate Excel Report in Selenium using TestNG Selenium Interview Questions – 1

Related Posts

TAKING WEB ELEMENT SCREENSHOT IN SELENIUM

AShot

Taking Web Element Screenshot in Selenium

TAKING FULL PAGE SCREENSHOT IN SELENIUM

AShot

Taking Full Page Screenshot in Selenium

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025