【问题标题】:Screenshot with display:none in Extent Reports显示屏幕截图:范围报告中的无
【发布时间】:2020-05-21 23:24:05
【问题描述】:

我正在使用这个范围报告:

  <!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>4.1.5</version>
    </dependency>

这是我的方法:

@AfterMethod
    public void afterMethod(ITestResult result) {
        String methodName = result.getMethod().getMethodName();
        if (result.getStatus() == ITestResult.FAILURE) {
            String exceptionMessage = Arrays.toString((result.getThrowable().getStackTrace()));
            extentTest.fail("<details><summary><b><font color=red>Exception Occured, click to see details:"
                    + "</font></b></summary>" + exceptionMessage.replaceAll(",", "<br>") + "</details> \n");

            String path = takeScreenshot(result.getMethod().getMethodName());
            try {
                extentTest.fail(result.getThrowable().getMessage() +
                                "<br><font color=red>" + "Screenshot of failure" + "</font><br>",
                        MediaEntityBuilder.createScreenCaptureFromPath(path).build());
            } catch (IOException e) {
                extentTest.fail("Test Failed, cannot attach screenshot");
            }

这是截图:

 private String takeScreenshot(String methodName) {
            String fileName = getScreenshotName(methodName);
            String directory = System.getProperty("user.dir") + "/resources/screenshots/";
            new File(directory).mkdirs();
            String path = directory + fileName;
            try {
                File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(screenshot, new File(path));
                System.out.println("**********************************");
                System.out.println("Screenshot stored at: " + path);
                System.out.println("**********************************");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return path;
        }

但是,屏幕截图没有显示在报告中:

我该如何解决这个问题?提前致谢。

【问题讨论】:

    标签: java selenium-webdriver testng extentreports selenium-extent-report


    【解决方案1】:

    您无法将屏幕截图附加到范围报告 html 中,因为您忘记在 @AfterMethod 的末尾调用 flush() 方法。 flush() 将所有测试结果/屏幕截图附加到报告 HTML 文件中。要附加到报告中的任何内容都必须至少有一个结束测试。

    注意:如果在任何结束的测试之前调用了 flush(),则不会将任何信息附加到报告中。

    所以更正你的代码如下 -

        @AfterMethod
        public void afterMethod(ITestResult result) {
           //Your existing code
          try {
              //Your existing code
              } catch (IOException e) {
              //Your existing code
               }
                extent.flush();
          }
    

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多