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

API Testing With Selenium WebDriver

REST API Testing Framework We will be creating a simple Rest Testing Framework in Java and JUnit that could be used for any testing scenarios. Rest Testing Framework Overview The framework should be able to execute the basic REST operations (GET, POST, PUT, PATCH, DELETE) and perform the validations on the code, message, headers and body of the response. The completed code can be accessed from my  GitHub account   from where you can collect and make modifications based on your requirements. Design We will be having three classes for the framework (in package com.axatrikx.controller ) RestExecutor  : Performs the HTTP operations using Apache Http Client RestResponse  : A javabean class to hold our response values (code, message, headers, body) RestValidator  : Validates the response with the expected values The package  com.axatrikx.test  holds the test scripts. Note: I will be using ‘ json-server ‘ to f...

VBScript

Introduction to VBScript This tutorial focuses on: ·          What you should already know ·          What is VBScript? ·          VBScript and Internet Explorer ·          What can be done with VBScript? What you should already know Before studying VBScript, you should already have at least a basic understanding of HTML and/or XHTML. VBScript scripts are placed on webpages with HTML/XHTML tags and without knowing these tags, you will not know where and how to place VBScript scripts on a webpage. Check out our   HTML tutorials   and   XHTML tutorials   if you are not yet familiar with these languages. What is VBScript? VBScript is a scripting language used to provide dynamic and interactive content on webpages. VBScript is short for Visual Basic Scripting Edition. VBScript is a lighter ve...

Autonium

About Autonium Autonium is a test automation framework for web applications. This framework is built by using Java and Selenium. It uses Maven project structure. Easy to understand and use it. Having basic idea on Java, Selenium, Maven and TestNG is good enough to start with Autonium. Let’s start Automation with Autonium --------------------------------------------------- Advantages Provides support to all major browsers –  Firefox, Chrome, IE and Safari . Capability of executing scripts on  Remote Machines  by using Selenium Server. Users can make use of Selenium Web driver methods and Autonium methods. Autonium simplifies  Test data management . Test data can be read from properties files. Scripts can be developed without  hard coding  test data . Identifiers can also be read from properties files and  hard coding   of identifiers can be avoided. Test data and Identifier v...