Extent Report for Cucumber 7 (Extent reports for Cucumber-JVM version 7) | Cucumber, TestNG, Selenium Java , Maven

Sadakar Pochampalli
JasperSoft BI Suite Tutorials - Sadakar Pochampalli )

This post outlines about - how to integrate Extent Report for cucumber 7 that is integrated with TestNG, Selenium Java and Maven. 

You can clone the complete example from this GitHub repo or down load the zip archive file here

This code has two cucumber scenarios independent of each other but uses the same Background in the feature files. And, I've deliberately failing these scenarios to view the screen shot attachments in Extent Report(Spark Report). 
Update this piece of yellow highlighted string from this statement in HRMLoginPage.java so both the scenarios will get succeeded. 


You could also refer this no voice video walkthrough tutorial. 

The best practice is to keep the code under src/test/java or src/test/resources but this makes hard to have the executable jar crated, so code should reside under src/main/java or src/main/resources. 

Project outline:

Step 1: Add the following dependency to the pom.xml file

Step 2 :  Add the extent cucumber adapter as plugin in the @CucumberOptions
 Cucumber options are normally given in the Cucumber Test Runner class, for instance in RunCucumberTest. java

plugin = { "pretty", 

 Step 3 : Create extent.properties file in src/main/resources and add below properties

If we want to generate the extent report(say Spark report)  in an executable jar file, add the plugin in main method as shown in below BasePage.java. 
When run the cucumber scenarios from jar file , the jar generates "target" folder and inside it we can find the Extent Report that is Spark.html
package com.sadakar.common;

import org.openqa.selenium.WebDriver;

import io.cucumber.core.cli.Main;

public class BasePage {

public static WebDriver driver;
public static void main(String args[]) throws Throwable {
try {
.main(new String[] {




"-p", "pretty",
"-p", "json:target/cucumber-reports/cucumber.json",
"-p", "html:target/cucumber-reports/cucumberreport.html",

} catch (Exception e) {
.out.println("Main method exception : " + e);


Save the project and run it as TestNG from eclipse or you could run the scenarios from the executable jar file and analyze the reports generated in "target" folder. 

The Extent Report(Spark.html) with successful execution should look as shown in below image. 

Now, what if you want to attach screen shots for the failed scenarios ? 
The location is given the extent.properties file 

In order to get the screen shots , add below piece of code in Cucumber @After hook with order=2 since the order=1 is used to quit the driver after each scenario. This order plays a vital role in terms of attaching screen short after a failed scenario. 

if ((scenario.isFailed())) {
final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
scenario.attach(screenshot, "image/png", scenario.getName());

package com.sadakar.common;

import java.time.Duration;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.chrome.ChromeDriver;

import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;

public class Hooks extends BasePage {

@Before //Cucumber hook - runs for each scenario
public static void setupDriver() throws InterruptedException {

.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
= new ChromeDriver();


@After(order=1) //Cucumber hook - runs for each scenario
public static void quitDriver() throws Exception {
@After(order = 2) // Cucumber After Hook with order 1
public void takeScreenShotOnFailedScenario(Scenario scenario) {

.out.println("Taking screenshot from Cucumber After hook with order=2 if the scenario fails");
if ((scenario.isFailed())) {
final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
scenario.attach(screenshot, "image/png", scenario.getName());



The same code base above is given to have bad login so it will fail both the scenarios and so the Sprak.html look like below with the screen shot attached. 


That's all we have to do to integrate Extent Report with cucumber 7. 
I hope you found this demo is useful! Keep an eye on this space for interesting automation updates and do subscribe the YouTube channel.