我是Selenium WebDriver的新手。我使用以下方法在Selenium,TestNG中截取失败测试的屏幕截图。执行后,testNG报告完美地显示失败的测试,并且还成功创建了imageFile输出文件,但测试输出文件夹下没有显示屏幕截图。当我手动检查时,我发现截图是在test-output文件夹的项目文件夹中创建的。如何纠正此问题,以便屏幕截图显示在测试输出文件夹下?
@AfterMethod
public void closeBrowser(ITestResult result) throws IOException {
if (!result.isSuccess()){
File imageFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String failureImageFileName = result.getMethod().getMethodName()+new SimpleDateFormat("MM-dd-yyyy_HH-ss").format(new GregorianCalendar().getTime()) + ".png";
File failureImageFile = new File(failureImageFileName);
FileUtils.copyFile(imageFile, failureImageFile);
}
答案 0 :(得分:0)
public void closeBrowser(ITestResult result) throws IOException {
if (!result.isSuccess()){
File imageFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String failureImageFileName = result.getMethod().getMethodName()+new SimpleDateFormat("MM-dd-yyyy_HH-ss").format(new GregorianCalendar().getTime()) + ".png";
// It will store all the screenshots in test-output/screenshots folder
String destDir = System.getProperty("user.dir")) + "/" + "test-output/screenshots";
new File(destDir ).mkdirs();
String destFile = destDir + "/" + failureImageFileName;
FileUtils.copyFile(imageFile, new File(destFile ));
}