简体   繁体   中英

How to attach screenshot to extent report in cucumber java

I am using Cucumber java and Extent report. I am able to generate report, capture the screenshot for the failed test case. However, for some reason, it is not attaching the screenshot to the extent report.

I am using the below dependencies in pom file

<dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>7.2.3</version>
    </dependency>

<dependency>
      <groupId>tech.grasshopper</groupId>
      <artifactId>extentreports-cucumber7-adapter</artifactId>
      <version>1.2.0</version>
    </dependency>

In Hooks.java, I have written below code

@AfterStep
    public void AddScreenshot(Scenario scenario) throws IOException
    {
        WebDriver driver =testContextSetup.testBase.WebDriverManager();
        if(scenario.isFailed())
        {
        File sourcePath=    ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        byte[] fileContent = FileUtils.readFileToByteArray(sourcePath);
        scenario.attach(fileContent, "image/png", "image");
        }
    }

In the src/test/resources folder,

  1. I have created extent-config.xml which a standard template
  2. I have created extent.properties file as below:
extent.reporter.html.start=true
extent.reporter.html.config=src/test/resources/extent-config.xml
extent.reporter.html.out=test-output/extentreports/extent.html
screenshot.dir=test-output/ExtentReport/screenshots
screenshot.rel.path=./screenshots/

As you can see from the image, 在此处输入图像描述 it has successfully created the screenshot,however it does not attached it in the report.

在此处输入图像描述

Any help is appreciated!

I managed to solve this issue. Screenshot path was incorrect. Corrrected it.

In the extent.properties file screenshot.rel.path is not correct

screenshot.rel.path=./screenshots/

Please use this.

extent.reporter.html.start=true
extent.reporter.html.config=src/test/resources/extent-config.xml
extent.reporter.html.out=test-output/extentreports/extent.html
screenshot.dir=test-output/ExtentReport/screenshots
screenshot.rel.path=../screenshots/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM