Dushyant Patil
Dushyant Patil

Reputation: 31

extentreports-testng-adapter : How to generate HTML report with screenshot if test failed (cucumber, testng)

I am using a framework which is built using cucumber, selenium, testng and maven.

Maven for dependencies only.

Testng for execution the cucumber feature files using AbstractTestNGCucumberTests.

I have also implemented the Extent Report using extentreports-testng-adapter. see this link that I referred - http://extentreports.com/docs/versions/4/java/testng.html

I have added both extent.properties file and html-config file and my report is properly generating.

Now, the only problem is I don't know how to attach the screenshot with the report if my cucumber scenario fails with the above setup.

Note: I have not added any code anywhere for extent report as the above library (extentreports-testng-adapter) covers it. Also, I have used the above setup because I have to run my test parallel hence cant use static built in methods.

code :

@CucumberOptions(features = { "src/test/resources/common_features/test.feature" }, glue = {
        "com.step.definitions" }, tags = {}, plugin = {}, monochrome = true, dryRun = false

)

@Listeners({ExtentITestListenerAdapter.class})
public class TestCase extends AbstractTestNGCucumberTests {

}

Thank you for the help.

Upvotes: 0

Views: 1470

Answers (1)

Sureshmani Kalirajan
Sureshmani Kalirajan

Reputation: 1938

@After hook should work,

`@After
 public void tearDown(Scenario scenario) {
 if (scenario.isFailed()) {
   // Take a screenshot...
     final byte[] screenshot = ((TakesScreenshot) 
  webDriver).getScreenshotAs(OutputType.BYTES);
      scenario.embed(screenshot, "image/png"); // ... and embed it in the report.
    }
 }`

This classpath should also be added as glue on your cucumberoptions.

Upvotes: 0

Related Questions