我正在创建一个TestNG Maven框架。
我在测试失败的屏幕截图上将其上传到范围报告时遇到问题
以下是范围报告侦听器。
String targetLocation = null;
String testClassName = result.getInstanceName();
String errorDate = new SimpleDateFormat("(MM.dd.YYYY HH-mm-ss)").format(new Date());
String testMethodName = result.getName();
String screenShotName = testMethodName + errorDate + ".png";
String fileSeperator = System.getProperty("file.separator");
String reportsPath = System.getProperty("user.dir") + fileSeperator + "TestReport" + fileSeperator
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
targetLocation = reportsPath + fileSeperator + testClassName + fileSeperator + screenShotName;
try {
File targetFile = new File(targetLocation);
FileHandler.copy(src, targetFile);
} catch (FileNotFoundException e) {
Log.info("File not found exception occurred while taking screenshot " + e.getMessage());
} catch (Exception e) {
Log.info("An exception occurred while taking screenshot " + e.getCause());
}
// attach screenshots to report
try {
ExtentTestManager.getTest().fail("Screenshot", MediaEntityBuilder.createScreenCaptureFromPath(targetLocation).build());
} catch (IOException e) {
Log.info("An exception occured while taking screenshot " + e.getCause());
}
ExtentTestManager.getTest().log(Status.FAIL, "Test Failed");
最佳答案
只需将此代码用于屏幕截图。
ExtentTestManager.startTest(method.getName(), description);
ExtentTest extentTest = ExtentTestManager.getTest();
String base64Screenshot = "data:image/png;base64," + ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
extentTest.log(LogStatus.FAIL, logs, ExtentTestManager.getTest().addBase64ScreenShot(base64Screenshot));