tooltip.ImageComparison.java Source code

Java tutorial

Introduction

Here is the source code for tooltip.ImageComparison.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tooltip;

import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.Raster;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.sql.Driver;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
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 static WebDriver driver;
    public WebElement element;

    // @BeforeSuite
    public static void setUp() throws Exception {
        boolean ret = true;
        System.out.println("Inside Setup C:\\Comcast Project Docs\\Automation\\CAAP AUTOMATION\\Selenium");
        //System.setProperty("webdriver.chrome.driver","C:\\Users\\ajavva001c\\Downloads\\chromedriver.exe");

        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://activator-web-qaauto.g1.app.cloud.comcast.net/Activate/comFlow");
        File url = new File("C:/Users/ajavva001c/HSD/unpacked.png");
        FileInputStream fi = new FileInputStream(url);
        BufferedImage bufImgOne = ImageIO.read(fi);
        String s1 = driver.findElement(By.xpath("//*[@id='responsive']/div/div/div[2]/div/ul/li[2]/img"))
                .getAttribute("src");
        URL urls = new URL(s1);
        System.out.println(urls);
        BufferedImage bufImgOne1 = ImageIO.read(urls);
        Raster image = bufImgOne.getData();
        Raster image1 = bufImgOne1.getData();
        if (image.getNumBands() != image1.getNumBands() && image.getWidth() != image1.getWidth()
                && image.getHeight() != image1.getHeight()) {
            ret = false;
            System.out.println("fail");
        } else {
            search: for (int i = 0; i < image.getNumBands(); ++i) {
                for (int x = 0; x < image.getWidth(); ++x) {
                    for (int y = 0; y < image.getHeight(); ++y) {
                        if (image.getSample(x, y, i) != image1.getSample(x, y, i)) {
                            ret = false;
                            break search;
                        }

                    }
                }
            }
            System.out.println(ret);
        }
        driver.quit();
    }

    /*if(image.getNumBands() == image1.getNumBands() && image.getWidth() == image1.getWidth() && image.getHeight() == image1.getHeight()){
     ret= true;
     System.out.println("pass");
    }else {
     search: for (int i=0; i<image.getNumBands(); ++i){
        for (int x = 0; x<image.getWidth(); ++x){
           for (int y=0; y<image.getHeight(); ++y){
              if(image.getSample(x, y, i) == image1.getSample(x, y, i)){
                 ret = true;
                 System.out.println("pass");
                 break search;
              }
           }
        }
     }
    }   
    }*/
    public static void main(String[] args) throws Exception {
        setUp();
    }
}
/*driver.navigate().to(baseUrl);
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  try {
     // bank of america logo
     element = driver.findElement(By.xpath("//*[@id='hp-section-1']/div[2]/div[1]/img"));
  } catch (Exception e) {
     System.out.println("No element found");
  }
   }
    
   public static void main(String[] args) throws Exception {
  ImageComparison i = new ImageComparison();
  i.setUp();
  i.shootWebElement(i.element);
  i.testImageComparison();
   }
    
   public void tearDown() throws Exception {
  driver.quit();
   }
    
   public void shootWebElement(WebElement element) throws IOException {
    
  File screen = ((TakesScreenshot) this.driver).getScreenshotAs(OutputType.FILE);
    
  Point p = element.getLocation();
    
  int width = element.getSize().getWidth();
  int height = element.getSize().getHeight();
    
  BufferedImage img = ImageIO.read(screen);
    
  BufferedImage dest = img.getSubimage(p.getX(), p.getY(), width, height);
    
  ImageIO.write(dest, "jpg", screen);
    
  FileUtils.copyFile(screen,
        new File("C:\\Users\\ajavva001c\\HSD\\RuntimeImage.jpg"));
    
   }
    
   public void testImageComparison() throws IOException, InterruptedException {
    
  File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  Thread.sleep(3000);
  FileUtils.copyFile(screenshot, new File("GoogleOutput.jpg"));
    
  File fileInput = new File("C:\\Comcast Project Docs\\Automation\\CAAP AUTOMATION\\Selenium\\RuntimeImage.jpg");
  File fileOutPut = new File("C:\\Comcast Project Docs\\Automation\\CAAP AUTOMATION\\Selenium\\StoredImage.jpg");
  System.out.println("Inside testImageComparison");
  BufferedImage bufferfileInput = ImageIO.read(fileInput);
  DataBuffer datafileInput = bufferfileInput.getData().getDataBuffer();
  int sizefileInput = datafileInput.getSize();
  BufferedImage bufferfileOutPut = ImageIO.read(fileOutPut);
  DataBuffer datafileOutPut = bufferfileOutPut.getData().getDataBuffer();
  int sizefileOutPut = datafileOutPut.getSize();
  Boolean matchFlag = true;
  System.out.println("assigned matchflag to true");
    
  for (int i = 0; i < sizefileInput; i++) {
     System.out.println("cheking --" + i);
     if (datafileInput.getElem(i) != datafileOutPut.getElem(i)) {
        matchFlag = false;
        System.out.println("Images are not same here");
        break;
     }
    
  }
  if (matchFlag != false) {
     System.out.println("Images are  same outside for");
  }
   }
    
}
*/