我目前已经使用Java和JUnit在Appium测试中实现了iOS截图功能。在测试结束之前,代码会运行以获取屏幕上最后可见的内容,然后关闭连接。
@After
public void tearDown() throws Exception {
if (platform.equals(iOS)) {
captureScreenshot(testName.getMethodName());
}
driver.quit();
}
@SuppressWarnings("Augmenter")
public void captureScreenshot(String testName) {
String imagesLocation = "target/surefire-reports/screenshot/" + platform + "/";
new File(imagesLocation).mkdirs(); // Insure directory is there
String filename = imagesLocation + testName + ".jpg";
try {
Thread.sleep(500);
WebDriver augmentedDriver = new Augmenter().augment(driver);
File scrFile = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(filename), true);
} catch (Exception e) {
System.out.println("Error capturing screen shot of " + testName + " test failure.");
// remove old pic to prevent wrong assumptions
File f = new File(filename);
f.delete(); // don't really care if this doesn't succeed, but would like it to.
}
}
这在安卓上有点用,但最近它完全破坏了正在运行的测试,但这可能与它试图获取快照的设备有关。以前它会保存文件,但图像会出现空白或损坏。
有人知道如何使用appium在安卓上实现图像/屏幕捕获吗?也许可以使用UIAutomator?