SDET- QA Automation Techie

Software Testing Blog

  • Home
  • Training
    • Online
    • Self-Paced
  • Video Tutorials
  • Interview Skills
    • HR Interview Questions Videos
    • Domain Knowledge
  • Career Guidance
  • Home
  • Software Testing
    • Manual Testing Tutorials
    • Manual Testing Project
    • Manaul Testing FAQS
    • ISTQB
    • AGILE
  • Web Automation Testing
    • Java Programmng
    • Python Programmng
    • Selenium with Java
    • Selenium with Python
    • Robot Framework(Selenium with Python)
    • selenium with Cucumber
    • TestNG+IntelliJ
    • Mobile App Testing(Appium)
    • JMeter
  • API Automation Testing
    • Rest Assured API Testing (BDD)
    • Rest Assured API Testing (Java+ TestNG)
    • Robot Framework(Rest API Testing with Python)
    • Postman
    • SoapUI
    • API Testing(FAQ's)
  • SDET|DevOps
    • Continuos Integration
    • SDET Essentials
    • AWS For Testers
    • Docker
  • SQL
    • Oracle(SQL)
    • MySQL for Testers
    • NoSQL
  • Unix/Linux
    • UNIX TUTORIALS
    • Linux Shell Scripting
  • ETL Testing
    • ETL Data warehouse Tutorial
    • ETL Concepts Tools and Templates
    • ETL Testing FAQ's
    • ETL Testing Videos
  • Big Data Hadoop
  • Video Tutorials
  • ApachePOI Video Tutorials
  • Downloads
    • E-Books for Professionals
    • Resumes
  • Automation Essencials
    • Cloud Technologies
      • Docker For Testers
      • AWS For Testers
      • Sub Child Category 3
    • Java Collections
    • Selenium Locators
    • Frequently Asked Java Programs
    • Frequently Asked Python Programs
    • Protractor
    • Cypress Web Automation

How to compare Images in Selenium with Java using Ashot API

 How to compare Images in Selenium with Java using Ashot API   


In this article I'm going to explain how to capture the screen shot of specific elements (Especially images) and compare image with our expected image.

This is very important scenario when You are automating test cases. Unfortunately Selenium does not provide any Classes t achive this, But still we can do this using third party API AShot.


Below is code and explained in the video how to compare images using AShot API.



Pre-Requisites:

You have to download third party API jars. Links provided below.

ashot 1.5.3
https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot/1.5.4

Gson 2.8.5
https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5

Hamcreast-all 1.3
https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all/1.3

Capture logo image present on a web page.

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.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;

public class CaptureLogoImage
{
    public static void main(String args[]) throws IOException
    {
    System.setProperty("webdriver.chrome.driver","C://Drivers/chromedriver_win32/chromedriver.exe");
    WebDriver driver=new ChromeDriver();
       
        driver.get("https://opensource-demo.orangehrmlive.com/");
             WebElement logoImageElement = driver.findElement(By.xpath("//*[@id=\"divLogo\"]/img"));
             Screenshot logoImageScreenshot = new AShot().takeScreenshot(driver, logoImageElement);
       ImageIO.write(logoImageScreenshot.getImage(),"png",new File("C://SeleniumPractice/logos/OrangeHRMlogo.png"));
             File f = new File("C://SeleniumPractice/logos/OrangeHRMlogo.png");
if(f.exists())
{
   System.out.println("Image File Captured");
}
else
{
System.out.println("Image File NOT exist");
}
          }
}

Compare images

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.chrome.ChromeDriver;
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 ImageComparison
{
    public static void main(String args[]) throws IOException
    {
    System.setProperty("webdriver.chrome.driver","C://Drivers/chromedriver_win32/chromedriver.exe");
    WebDriver driver=new ChromeDriver();
       
        driver.get("https://opensource-demo.orangehrmlive.com/");
             
        BufferedImage expectedImage = ImageIO.read(new File("C://SeleniumPractice//logos/OrangeHRMlogo.png"));
             
        WebElement logoImageElement = driver.findElement(By.xpath("//*[@id=\"divLogo\"]/img"));
        Screenshot logoImageScreenshot = new AShot().takeScreenshot(driver, logoImageElement);
        BufferedImage actualImage = logoImageScreenshot.getImage();
               
        ImageDiffer imgDiff = new ImageDiffer();
        ImageDiff diff = imgDiff.makeDiff(actualImage, expectedImage);
        if(diff.hasDiff()==true)
        {
        System.out.println("Images are Not Same");
        }
        else {
        System.out.println("Images are Same");
        }
                     
      driver.quit();
    }
}


  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home
popup

Popular Posts

  • How To Explain Project In Interview Freshers and Experienced
    “ Describe an important project you’ve worked on ” is one of the most common questions you can expect in an interview. The purpose of a...
  • MANUAL TESTING REAL TIME INTERVIEW QUESTIONS & ANSWERS
    1. How will you receive the project requirements? A. The finalized SRS will be placed in a project repository; we will access it fr...
  • API/Webservices Testing using RestAssured (Part 1)
    Rest Assured : Is an API designed for automating REST services/Rest API's Pre-Requisites Java Free videos: https://www.you...

Facebook Page

Pages

  • Home
  • Resumes
  • Job Websites India/UK/US
  • ISTQB
  • Selenium with Java
  • E-Books for Professionals
  • Manual Testing Tutorials
  • Agile Methodology
  • Manual Testing Projects

Live Traffic

YouTube


Blog Visitors

Copyright © SDET- QA Automation Techie | Powered by Blogger
Design by SDET | Blogger Theme by | Distributed By Gooyaabi Templates