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

标签 java extentreports selenium-extent-report

我不明白为什么我的屏幕截图没有出现在本地的 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:

enter image description here

最佳答案

这是因为指定的屏幕截图路径需要相对于报告路径。我自己也经历了一番艰难才发现这一点。虽然屏幕截图确实是在您认为的位置拍摄并存储的,但由于它与报告的位置完全相同,因此只需省略通向文件名的路径即可。

在我的输出中,我将报告文件放在项目文件夹外的 target\reports 文件夹中,但随后我有一个 target\reports\screenshots 文件夹,因此当我将屏幕截图附加到测试对象时,我只指定“screenshots\filename.png”,当我尝试完整路径时,它不会工作。

关于java - 使用范围报告打印屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52078525/

相关文章:

java - 如何永久解决 Jenkins 中显示范围报告的 HTML Publisher 插件问题?

selenium - 范围报告的 com.aventstack 和 com.relevantcodes 依赖项之间有什么区别

java - 使用atom导入包

java - JBoss、RestEasy、Spring 依赖注入(inject)失败

java - 使用 TextureRegion (LibGDX) 在 Java 中绘制部分 Sprite

java - 正则表达式..将字符串数组分割两次

java - 使用 ExtentReports 3.2 jar java 为一套测试服中的多个 testng 套装/类/多个套装生成报告

testing - extentreports-testng-适配器 : How to generate HTML report with screenshot if test failed (cucumber, testng)

java - 如何增加 ExtentReports 4.0.9 中屏幕截图缩略图的大小