简体   繁体   English

使用 Java 在 Selenium WebDriver 中进行图像比较

[英]Image comparison in Selenium WebDriver using Java

I am currently working in Selenium WebDriver to compare two images during automation.我目前在 Selenium WebDriver 中工作,在自动化过程中比较两个图像。 Currently I am using pixel comparison but the problem comes if the browser size is changed or the system is different on which I run the automation.目前我正在使用像素比较,但如果浏览器大小发生变化或运行自动化的系统不同,就会出现问题。

I have to compare two images, one is the golden one which is already saved in a location and the other one is the screenshot taken during the automation.我必须比较两张图片,一张是已经保存在某个位置的金色图片,另一张是自动化过程中截取的屏幕截图。 As soon as the screenshot is taken, it is compared with the golden image I have, and a pass or fail is asserted accordingly.截取屏幕截图后,将其与我拥有的黄金图像进行比较,并相应地断言通过或失败。 The problem comes if the browser size or the system resolution is different when the screenshot is taken, because this will affect the resolution of the image which might not be same as the resolution of the golden image I have for reference.如果截图时浏览器大小或系统分辨率不同,就会出现问题,因为这会影响图像的分辨率,可能与我参考的金色图像的分辨率不同。 Here both the images are same but the pixels might change with the browser size or system change.这里两个图像是相同的,但像素可能会随着浏览器大小或系统的变化而变化。

So is there any way to compare two images using java in selenium with out using pixel comparison?那么有没有办法在 selenium 中使用 java 比较两个图像而不使用像素比较?

The key thing here is the resolution, you must ensure that resolution is the same for base "golden" images and those you capture during test .这里的关键是分辨率,您必须确保基本“黄金”图像和您在测试期间捕获的图像的分辨率相同

  1. Set size of the browser window to fixed size, eg.将浏览器窗口的大小设置为固定大小,例如。 (1920x1080), (1920x1080),
  2. Make all screenshots in this resolution,以该分辨率制作所有屏幕截图,
  3. During the test: before each image-comparison check if the window size is (1920x1080) if not I change it temporarily,测试期间:在每次图像比较之前检查窗口大小是否为(1920x1080)如果不是我暂时更改它,
  4. Take screenshot,截图,
  5. Compare image with original "golden" img将图像与原始“金色” img 进行比较
  6. Do window maximize做窗口最大化

Other solution is to capture screenshots of single WebElement rather than whole page, because WebElements often are resolution independent.其他解决方案是捕获单个 WebElement 而不是整个页面的屏幕截图,因为 WebElements 通常与分辨率无关。

You can use Huxley by Facebook or Wraith by BBC, both are open source tools.你可以使用 Facebook 的Huxley或 BBC 的Wraith ,两者都是开源工具。 Wraith Selenium integrates Selenium and Wraith. Wraith Selenium集成了 Selenium 和 Wraith。 You can read this post for more information你可以阅读这篇文章了解更多信息

You want to use pixel comparison but instead of taking a screenshot, which may be a different size, compare the actual image to the golden image.您想使用像素比较,而不是截取可能大小不同的屏幕截图,而是将实际图像与黄金图像进行比较。 The flow would be to navigate to the page, grab the SRC value from the IMG tag and download that file.流程是导航到页面,从 IMG 标签中获取 SRC 值并下载该文件。 Compare the downloaded file to the golden image.将下载的文件与黄金映像进行比较。

Here's some sample code that downloads the google logo.这是一些下载 google 徽标的示例代码。

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.google.com");
String s = driver.findElement(By.id("hplogo")).getAttribute("src");
URL url = new URL(s);
System.out.println(url);
BufferedImage bufImgOne = ImageIO.read(url);
ImageIO.write(bufImgOne, "png", new File("test.png"));

By downloading the image, you can manually verify the image later if the validation fails.通过下载镜像,如果验证失败,您可以稍后手动验证镜像。 If you don't want to download it, you can probably skip the write() step and just do the comparison (depending on how your pixel comparison code is written).如果您不想下载它,您可以跳过write()步骤并只进行比较(取决于您的像素比较代码的编写方式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM