For anyone who is looking to create Test report using extent report :
Here you go :)
TestNg runner:
package cucumber_Runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;
//Created a custom plugin
@CucumberOptions(plugin = {"cucumberReport.customReportListener"},
monochrome=true,
glue={"stepDefinitions","cucumberHooks"}, //Packagename
features = {"src\\featureFiles"} //FolderName
)
public class RunnerCucumber extends AbstractTestNGCucumberTests {
@DataProvider(parallel = true)
@Override
public Object[][] scenarios() {
return super.scenarios();
}
}
And plugin class:
package cucumberHooks;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.gherkin.model.Feature;
import com.aventstack.extentreports.gherkin.model.Given;
import com.aventstack.extentreports.gherkin.model.Scenario;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
import io.cucumber.plugin.EventListener;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.PickleStepTestStep;
import io.cucumber.plugin.event.TestCaseStarted;
import io.cucumber.plugin.event.TestRunFinished;
import io.cucumber.plugin.event.TestRunStarted;
import io.cucumber.plugin.event.TestSourceRead;
import io.cucumber.plugin.event.TestStepFinished;
import io.cucumber.plugin.event.TestStepStarted;
import io.cucumber.plugin.event.HookTestStep;
import java.util.HashMap;
import java.util.Map;
public class customReportListener implements EventListener {
private ExtentSparkReporter spark;
private ExtentReports extent;
Map<String, ExtentTest> feature = new HashMap<String, ExtentTest>();
ExtentTest scenario;
ExtentTest step;
public customReportListener() {
};
@Override
public void setEventPublisher(EventPublisher publisher) {
// TODO Auto-generated method stub
/*
* :: is method reference , so this::collecTag means collectTags method in
* 'this' instance. Here we says runStarted method accepts or listens to
* TestRunStarted event type
*/
publisher.registerHandlerFor(TestRunStarted.class, this::runStarted);
publisher.registerHandlerFor(TestRunFinished.class, this::runFinished);
publisher.registerHandlerFor(TestSourceRead.class, this::featureRead);
publisher.registerHandlerFor(TestCaseStarted.class, this::ScenarioStarted);
publisher.registerHandlerFor(TestStepStarted.class, this::stepStarted);
publisher.registerHandlerFor(TestStepFinished.class, this::stepFinished);
};
/*
* Here we set argument type as TestRunStarted if you set anything else then the
* corresponding register shows error as it doesn't have a listner method that
* accepts the type specified in TestRunStarted.class
*/
// Here we create the reporter
private void runStarted(TestRunStarted event) {
spark = new ExtentSparkReporter("./ExtentReportResults.html");
extent = new ExtentReports();
spark.config().setTheme(Theme.DARK);
// Create extent report instance with spark reporter
extent.attachReporter(spark);
};
// TestRunFinished event is triggered when all feature file executions are
// completed
private void runFinished(TestRunFinished event) {
extent.flush();
};
// This event is triggered when feature file is read
// here we create the feature node
private void featureRead(TestSourceRead event) {
String featureSource = event.getUri().toString();
String featureName = featureSource.split(".*/")[1];
if (feature.get(featureSource) == null) {
feature.putIfAbsent(featureSource, extent.createTest(featureName));
}
};
// This event is triggered when Test Case is started
// here we create the scenario node
private void ScenarioStarted(TestCaseStarted event) {
String featureName = event.getTestCase().getUri().toString();
scenario = feature.get(featureName).createNode(event.getTestCase().getName());
};
// step started event
// here we creates the test node
private void stepStarted(TestStepStarted event) {
String stepName = " ";
String keyword = "Triggered the hook :";
// We checks whether the event is from a hook or step
if (event.getTestStep() instanceof PickleStepTestStep) {
// TestStepStarted event implements PickleStepTestStep interface
// WHich have additional methods to interact with the event object
// So we have to cast TestCase object to get those methods
PickleStepTestStep steps = (PickleStepTestStep) event.getTestStep();
stepName = steps.getStep().getText();
keyword = steps.getStep().getKeyword();
} else {
// Same with HoojTestStep
HookTestStep hoo = (HookTestStep) event.getTestStep();
stepName = hoo.getHookType().name();
}
step = scenario.createNode(Given.class, keyword + " " + stepName);
};
// This is triggered when TestStep is finished
private void stepFinished(TestStepFinished event) {
if (event.getResult().getStatus().toString() == "PASSED") {
step.log(Status.PASS, "This passed");
} else if (event.getResult().getStatus().toString() == "SKIPPED")
{
step.log(Status.SKIP, "This step was skipped ");
} else {
step.log(Status.FAIL, "This failed");
}
};
}
Maven
<dependencies>
<! — https://mvnrepository.com/artifact/com.aventstack/extentreports →
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>4.1.5</version>
</dependency>
<! — https://mvnrepository.com/artifact/io.cucumber/cucumber-testng →
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>6.0.0-RC2</version>
</dependency>
<! — https://mvnrepository.com/artifact/io.cucumber/cucumber-java →
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.0.0-RC2</version>
</dependency>
<! — https://mvnrepository.com/artifact/com.beust/jcommander →
<! — https://mvnrepository.com/artifact/org.testng/testng →
<! — https://mvnrepository.com/artifact/com.beust/jcommander →
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>24.1-jre</version>
</dependency>
<! — https://mvnrepository.com/artifact/org.testng/testng →
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
</dependency>
<! — https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java →
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-4</version>
</dependency>
<dependency>
</dependencies>
Output: