使用范围报告打印屏幕截图

Printing screenshot within ExtentReport

我不明白为什么我的屏幕截图没有出现在本地的 ExtentReport 中(我还没有在构建服务器上尝试过)。这是调用 takeScreenShot 方法的 TestNG @AfterMethod:-

@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:

这是因为指定的屏幕截图路径需要相对于报告路径。我自己也很难发现这一点。虽然屏幕截图确实被截取并存储在您认为的位置,但由于它与报告的位置完全相同,所以只需省略文件名的路径即可。

在我的输出中,我将报告文件放在项目文件夹外的 target\reports 文件夹中,但是我有一个 target\reports\screenshots 文件夹,所以当我将屏幕截图附加到测试时object,我只指定了"screenshots\filename.png" 试了完整路径就不行了