我已经能够在Firefox无头模式下运行硒测试用例,但是在截屏时,截屏不是网页(在测试用例中测试过的网页)的截屏,而是截屏的背景(如图所示。(例如,运行测试用例的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();
}
是否需要设置其他“选项”或“参数”?
使用无头的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();
}
并在测试执行失败时调用它:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句