如何使用 appium 将屏幕截图与参考图像进行比较 [英] How to compare screenshots to a reference image using appium
本文介绍了如何使用 appium 将屏幕截图与参考图像进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能够使用以下代码成功截取我的应用程序 JainLibrary 页面的屏幕截图之一.我正在使用 junit 和 appium.
I am able to successfully take the screenshot one of the page of my application JainLibrary using below code. I am using junit and appium.
public String Screenshotpath = "Mention the folder Location";
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(Screenshotpath+"Any name".jpg"));
现在我想将屏幕截图与参考图像进行比较,以便我可以继续测试用例.
Now I want to compare the screenshot with a reference image so that I can move forward with the test case.
推荐答案
一个简单的解决方案是将每个像素与参考屏幕截图进行比较:
A simple solution would be to compare each pixel with the reference screenshoot:
// save the baseline screenshot
driver.get("https://www.google.co.uk/intl/en/about/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\temp\\screenshot.png"));
// take another screenshot and compare it to the baseline
driver.get("https://www.google.co.uk/intl/en/about/");
byte[] pngBytes = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
if (IsPngEquals(new File("c:\\temp\\screenshot.png"), pngBytes)) {
System.out.println("equals");
} else {
System.out.println("not equals");
}
public static boolean IsPngEquals(File pngFile, byte[] pngBytes) throws IOException {
BufferedImage imageA = ImageIO.read(pngFile);
ByteArrayInputStream inStreamB = new ByteArrayInputStream(pngBytes);
BufferedImage imageB = ImageIO.read(inStreamB);
inStreamB.close();
DataBufferByte dataBufferA = (DataBufferByte)imageA.getRaster().getDataBuffer();
DataBufferByte dataBufferB = (DataBufferByte)imageB.getRaster().getDataBuffer();
if (dataBufferA.getNumBanks() != dataBufferB.getNumBanks()) {
return false;
}
for (int bank = 0; bank < dataBufferA.getNumBanks(); bank++) {
if (!Arrays.equals(dataBufferA.getData(bank), dataBufferB.getData(bank))) {
return false;
}
}
return true;
}
请注意,您需要将参考屏幕截图保存为 PNG
.JPEG
格式会改变像素.
Note that you need to save the reference screenshot as a PNG
. A JPEG
format will alter the pixels.
这篇关于如何使用 appium 将屏幕截图与参考图像进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文