我已经能够在 Firefox headless 模式下运行 selenium 测试用例,但是在截屏时,屏幕截图不是网页(在测试用例中测试的网页)的屏幕截图,而是截图背景(如显示的当前窗口(例如运行测试用例的 eclipse IDE))
截图功能
File screenShotFolder = new File("Screenshots");
WebDriver driver = getDriver();
try {
if (!screenShotFolder.exists() && !screenShotFolder.mkdir()) {
getLog().error(
"Cannot create a new file in the intended location. "
+ "" + screenShotFolder.getAbsolutePath());
}
File scrFile =
((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String filePath =
screenShotFolder.getAbsolutePath() + File.separator
+ imageName + ".png";
FileUtils.copyFile(scrFile, new File(filePath));
} catch (Exception e) {
e.printStackTrace();
}
是否还有其他需要设置的“选项”或“参数”?
最佳答案
使用 headless Firefox 截取屏幕截图应该像普通驱动程序一样工作。
过去我使用了以下方法:
public static String makeScreenshot() {
String fileName = System.currentTimeMillis() + "Test";
File screenshot = Driver.driver.get().getScreenshotAs(OutputType.FILE);
File outputFile = new File("LoggerScreenshots/" + fileName + ".png");
System.out.println(outputFile.getAbsolutePath());
try {
FileUtils.copyFile(screenshot, outputFile);
} catch (IOException e) {
e.printStackTrace();
}
return outputFile.getName();
}
并在测试执行失败时调用它:
关于java - 如何在 firefox headless(java 中的 selenium)中截取屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49606051/