在ExtentReport中打印屏幕截图

时间:2018-08-29 13:22:23

标签: java extentreports selenium-extent-report

我不明白为什么我的屏幕截图没有显示在我的ExtentReport中(我还没有从构建服务器上尝试过)。这是TestNG @AfterMethod,其中的takeScreenShot方法称为:-

@AfterMethod
public void afterMethod(ITestResult result) throws IOException {
    APPLICATION_LOGS.debug("Running @AfterMethod");
    switch (result.getStatus()) {
        case ITestResult.FAILURE:
            childTest.fail(result.getThrowable())
                    .addScreenCaptureFromPath((takeScreenShot(result.getMethod().getMethodName())));
            try {
                childTest.info("<a href='https://mywebsite.com/fabrix/logs/s/" + webSessionId + "'" + "target=_'blank'>Data control logs</a>");
            } catch (Exception e) {
                childTest.info("Unable to get Session ID");
            }//.....more code

这是被调用的方法:

private String takeScreenShot(String methodName) {
    String path = System.getProperty("user.dir") + "\\target\\reports\\screenshots\\" + methodName + ".png";
    try {
        File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotFile, new File(path));
    } catch (Exception e) {
        APPLICATION_LOGS.error("Could not write screenshot" + e);
    }
    return path;
}

报告:

[![在此处输入图片描述] [1]] [1]

我使用Chrome开发人员工具检查了图片,并且路径完美无缺,并且图片与报告保存在同一目录中。那为什么不显示呢?

从IntelliJ:

enter image description here

1 个答案:

答案 0 :(得分:0)

这是因为指定的屏幕快照路径需要与报告路径相对。我自己发现这很困难。尽管实际上是在截取屏幕快照并将其存储在您认为的位置,但由于它与报表完全相同,因此只需省略指向文件名的路径即可。

在输出中,我将报告文件放置在项目文件夹下的target \ reports文件夹中,但是随后有了target \ reports \ screenshots文件夹,因此,当我将屏幕截图附加到测试对象时,我仅指定“ screenshots \ filename.png“,当我尝试完整路径时,将无法使用。