Guest User

Untitled

a guest
May 4th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. Screen screen = new Screen();
  2. //set a timeout for waiting for the image
  3. screen.setAutoWaitTimeout(30000); //default is 10 seconds
  4. //wait for an image to get displayed on the screen and then click on it
  5. screen.wait(new Pattern("img/image.PNG")).click();
  6. //wait for an image with exact match
  7. screen.wait(new Pattern("img/image.PNG").exact()).click();
  8.  
  9. import java.awt.image.BufferedImage;
  10. import java.awt.image.DataBuffer;
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.util.concurrent.TimeUnit;
  14. import javax.imageio.ImageIO;
  15. import org.apache.commons.io.FileUtils;
  16. import org.openqa.selenium.OutputType;
  17. import org.openqa.selenium.TakesScreenshot;
  18. import org.openqa.selenium.WebDriver;
  19. import org.openqa.selenium.firefox.FirefoxDriver;
  20. import org.testng.Assert;
  21. import org.testng.annotations.AfterSuite;
  22. import org.testng.annotations.BeforeSuite;
  23. import org.testng.annotations.Test;
  24.  
  25. public class ImageComparison {
  26.  
  27. public WebDriver driver;
  28. private String baseUrl;
  29.  
  30. @BeforeSuite
  31. public void setUp() throws Exception {
  32. driver = new FirefoxDriver();
  33. baseUrl = "https://www.google.co.in/";
  34. driver.navigate().to(baseUrl);
  35. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  36. }
  37.  
  38. @AfterSuite
  39. public void tearDown() throws Exception {
  40. driver.quit();
  41. }
  42.  
  43. @Test
  44. public void testImageComparison() throws IOException, InterruptedException {
  45. File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  46. Thread.sleep(3000);
  47. FileUtils.copyFile(screenshot, new File("GoogleOutput.jpg"));
  48.  
  49. File fileInput = new File("GoogleInput.jpg");
  50. File fileOutPut = new File("GoogleOutput.jpg");
  51.  
  52. BufferedImage bufferfileInput = ImageIO.read(fileInput);
  53. DataBuffer bufferfileInput = bufferfileInput.getData().getDataBuffer();
  54. int sizefileInput = bufferfileInput.getSize();
  55. BufferedImage bufferfileOutPut = ImageIO.read(fileOutPut);
  56. DataBuffer datafileOutPut = bufferfileOutPut.getData().getDataBuffer();
  57. int sizefileOutPut = datafileOutPut.getSize();
  58. Boolean matchFlag = true;
  59. if(sizefileInput == sizefileOutPut) {
  60. for(int i=0; i<sizefileInput; i++) {
  61. if(bufferfileInput.getElem(i) != datafileOutPut.getElem(i)) {
  62. matchFlag = false;
  63. break;
  64. }
  65. }
  66. }
  67. else {
  68. matchFlag = false;
  69. Assert.assertTrue(matchFlag, "Images are not same");
  70. }
  71. }
Add Comment
Please, Sign In to add comment