[英]ExtentReports doesn't save the screenshot with the report
您好,我正在嘗試使用帶有 Maven 和 TestNG 的 selenium webdriver 使用以下代碼捕獲失敗測試用例的屏幕截圖,它確實在我的項目中生成了屏幕截圖,但沒有包含在報告中我需要在代碼中更改什么?
WebDriver driver;
ExtentReports extent;
ExtentTest logger;
@BeforeTest
public void setUp() {
extent = new ExtentReports("C:\\Users\\IdeaProjects\\p\\test-output\\report.html", true);
extent.loadConfig(new File("C:\\Users\\IdeaProjects\\p\\test-output\\extent-config.xml"));
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.google.com");
}
@Test
public void testExtentReports() {
logger = extent.startTest("testExtentReports");
Assert.assertTrue(false);
}
@AfterMethod
public void tearDown(ITestResult result) throws IOException {
if(result.getStatus() == ITestResult.FAILURE) {
logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
String screenshotPath = TestMine.getScreenshot(driver, result.getName());
logger.log(LogStatus.FAIL, logger.addScreenCapture(screenshotPath));
} else if(result.getStatus() == ITestResult.SKIP) {
logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
}
extent.endTest(logger);
}
public static String getScreenshot(WebDriver driver, String screenshotName) throws IOException {
String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);
// after execution, you could see a folder "FailedTestsScreenshots" under src folder
String destination = "ss/" + screenshotName+dateName+".png";
File finalDestination = new File(destination);
FileUtils.copyFile(source, finalDestination);
return destination;
}
@AfterTest
public void endReport() {
extent.flush();
extent.close();
driver.quit();
}
我在這里找到了一個解決方案,但我不太確定如何用我的代碼實現這個: Printing screenshot within ExtentReport
你的方法應該看起來像
@AfterMethod
public void tearDown(ITestResult result) throws IOException {
if(result.getStatus() == ITestResult.FAILURE) {
logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
String screenshotPath = TestMine.getScreenshot(driver, result.getName());
logger.log(LogStatus.FAIL, logger.addScreenCaptureFromPath((getScreenshot(result.getMethod().getMethodName())));
} else if(result.getStatus() == ITestResult.SKIP) {
logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
}
extent.endTest(logger);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.