Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Screen screen = new Screen();
- //set a timeout for waiting for the image
- screen.setAutoWaitTimeout(30000); //default is 10 seconds
- //wait for an image to get displayed on the screen and then click on it
- screen.wait(new Pattern("img/image.PNG")).click();
- //wait for an image with exact match
- screen.wait(new Pattern("img/image.PNG").exact()).click();
- 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.navigate().to(baseUrl);
- driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
- }
- @AfterSuite
- public void tearDown() throws Exception {
- driver.quit();
- }
- @Test
- 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("GoogleInput.jpg");
- File fileOutPut = new File("GoogleOutput.jpg");
- BufferedImage bufferfileInput = ImageIO.read(fileInput);
- DataBuffer bufferfileInput = bufferfileInput.getData().getDataBuffer();
- int sizefileInput = bufferfileInput.getSize();
- BufferedImage bufferfileOutPut = ImageIO.read(fileOutPut);
- DataBuffer datafileOutPut = bufferfileOutPut.getData().getDataBuffer();
- int sizefileOutPut = datafileOutPut.getSize();
- Boolean matchFlag = true;
- if(sizefileInput == sizefileOutPut) {
- for(int i=0; i<sizefileInput; i++) {
- if(bufferfileInput.getElem(i) != datafileOutPut.getElem(i)) {
- matchFlag = false;
- break;
- }
- }
- }
- else {
- matchFlag = false;
- Assert.assertTrue(matchFlag, "Images are not same");
- }
- }
Add Comment
Please, Sign In to add comment