In this post I have explained that how to verify images in
webdriver using java. As webdriver does not provide direct any function to image verification, but we can verify images by taking two screen shots of whole web page
using “TakesScreenshot” webdriver function, one at script creation time and another
at execution time,
Here is sample code for same
In below example I have created a sample script in which first I
captured a Google home page screen shot and saved (GoogleInput.jpg) into my
project, Another screen shot “GoogleOutput.jpg” captured of same page at test executing time and saved into project. I compared both images if they are not same then test will script fail.
package com.test;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
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 WebDriver driver;
private String baseUrl;
@BeforeSuite
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://www.google.co.in/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@AfterSuite
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void testImageComparison()
throws IOException,
InterruptedException
{
driver.navigate().to(baseUrl);
File screenshot = ((TakesScreenshot)driver).
getScreenshotAs(OutputType.FILE);
Thread.sleep(3000);
FileUtils.copyFile(screenshot,
new File("GoogleOutput.jpg"));
File fileInput = new
File("GoogleInput.jpg");
File fileOutPut = new
File("GoogleOutput.jpg");
BufferedImage
bufileInput = ImageIO.read(fileInput);
DataBuffer
dafileInput = bufileInput.getData().getDataBuffer();
int
sizefileInput = dafileInput.getSize();
BufferedImage
bufileOutPut = ImageIO.read(fileOutPut);
DataBuffer
dafileOutPut = bufileOutPut.getData().getDataBuffer();
int
sizefileOutPut = dafileOutPut.getSize();
Boolean
matchFlag = true;
if(sizefileInput
== sizefileOutPut) {
for(int
j=0; j<sizefileInput; j++) {
if(dafileInput.getElem(j)
!= dafileOutPut.getElem(j)) {
matchFlag
= false;
break;
}
}
}
else
matchFlag = false;
Assert.assertTrue(matchFlag,
"Images are not same");
}
}
how to take the image manually
ReplyDeleteAfter creating test, run test script it will automatically capture image and save into folder. Make this image as input this image is your expected image again when you run test it will capture image and compare from previous.
DeleteHow to verify image
ReplyDeleteThis verifies the images based on size. Rt? But how to verify the contents of the images displayed on a webpage?
ReplyDeleteI have followed your code and getting this error:
ReplyDeleteException in thread "main" javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at ICLICKStagePackage.MainClass.Mediaeffectiveness(MainClass.java:562)
at ICLICKStagePackage.MainClass.main(MainClass.java:709)
Please help me out what does it require.
Even i am getting the same error.
ReplyDeleteMake a screenshot "GoogleInput.jpg" in the project folder, better change the name of the output file "GoogleOutput.jpg" to "GoogleInput.jpg" and run
ReplyDeleteMake a screenshot "GoogleInput.jpg" in the project folder, better change the name of the output file "GoogleOutput.jpg" to "GoogleInput.jpg" and run
ReplyDeleteYou are trying to read the GoogleInput.jpg just after naming it. where is the content in it to read?
ReplyDeleteHey Guys
ReplyDeleteIf you will simply copy and run the above code it will throw error "Can't read input file!"
The concept here is comparing a image captured earlier with image captured now.
So its better you change the @Test block as I shown below and capture the GoogleInput.jpg screen shot, verify in your project folder if the GoogleInput.jpg is captured or not and then again comeback and use the same code given in this website without changing @Test block to compare the Input image with Out put image.
@Test
public void testImageComparison()
throws IOException, InterruptedException
{
driver.navigate().to(baseUrl);
File screenshot = ((TakesScreenshot)driver).
getScreenshotAs(OutputType.FILE);
Thread.sleep(3000);
FileUtils.copyFile(screenshot, new File("GoogleInput.jpg"));
File fileInput = new File("GoogleInput.jpg");
}
ICollection images = webdriver.FindElements(By.TagName("img"));
ReplyDeletestring imagesrc;
List imageurls = new List();
foreach (var src in images)
{
imagesrc = src.GetAttribute("src");
imageurls.Add(imagesrc);
}
foreach(string urls in imageurls)
{
webdriver.Navigate().GoToUrl(urls);
Thread.Sleep(1000);
ITakesScreenshot ssdriver = webdriver as ITakesScreenshot;
Screenshot screenshot = ssdriver.GetScreenshot();
Thread.Sleep(500);
screenshot.SaveAsFile(@"C:\Users\K780023\Documents\website_images\" + "image" + RandomDigits(3) + ".png", ScreenshotImageFormat.Png);
webdriver.Navigate().Back();
}
you can use this code for taking screen shot