屏幕显示的屏幕截图:范围报告中无

时间:2020-05-21 23:24:05

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

我正在使用此范围报告:

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

这是我的after方法:

@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;
        }

但是,屏幕截图未显示在报告中: enter image description here

如何解决此问题?预先感谢。

1 个答案:

答案 0 :(得分:0)

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

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

因此,请按照以下步骤更正您的代码-

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