com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of extentreports-cucumber3-adapter Show documentation
Show all versions of extentreports-cucumber3-adapter Show documentation
Cucumber-JVM 3 adapter for Extent Framework
package com.aventstack.extentreports.cucumber.adapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.GherkinKeyword;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.service.ExtentService;
import cucumber.api.HookTestStep;
import cucumber.api.PickleStepTestStep;
import cucumber.api.Result;
import cucumber.api.TestCase;
import cucumber.api.event.EmbedEvent;
import cucumber.api.event.EventHandler;
import cucumber.api.event.EventPublisher;
import cucumber.api.event.TestCaseStarted;
import cucumber.api.event.TestRunFinished;
import cucumber.api.event.TestSourceRead;
import cucumber.api.event.TestStepFinished;
import cucumber.api.event.TestStepStarted;
import cucumber.api.event.WriteEvent;
import cucumber.api.formatter.Formatter;
import gherkin.ast.Background;
import gherkin.ast.DataTable;
import gherkin.ast.DocString;
import gherkin.ast.Examples;
import gherkin.ast.Feature;
import gherkin.ast.Node;
import gherkin.ast.ScenarioDefinition;
import gherkin.ast.ScenarioOutline;
import gherkin.ast.Step;
import gherkin.ast.TableCell;
import gherkin.ast.TableRow;
import gherkin.ast.Tag;
import gherkin.pickles.Argument;
import gherkin.pickles.PickleCell;
import gherkin.pickles.PickleRow;
import gherkin.pickles.PickleString;
import gherkin.pickles.PickleTable;
/**
* A port of Cucumber-JVM (MIT licensed) HtmlFormatter for Extent Framework
* Original source: https://github.com/cucumber/cucumber-jvm/blob/master/core/src/main/java/cucumber/runtime/formatter/HTMLFormatter.java
*
*/
public class ExtentCucumberAdapter
implements Formatter {
private static ThreadLocal featureTestThreadLocal = new InheritableThreadLocal<>();
private static ThreadLocal scenarioOutlineThreadLocal = new InheritableThreadLocal<>();
private static ThreadLocal scenarioThreadLocal = new InheritableThreadLocal<>();
private static ThreadLocal stepTestThreadLocal = new InheritableThreadLocal<>();
private final TestSourcesModel testSources = new TestSourcesModel();
private String currentFeatureFile;
private Map currentTestCaseMap;
private ScenarioOutline currentScenarioOutline;
private Examples currentExamples;
private EventHandler testSourceReadHandler = new EventHandler() {
@Override
public void receive(TestSourceRead event) {
handleTestSourceRead(event);
}
};
private EventHandler caseStartedHandler= new EventHandler() {
@Override
public void receive(TestCaseStarted event) {
handleTestCaseStarted(event);
}
};
private EventHandler stepStartedHandler = new EventHandler() {
@Override
public void receive(TestStepStarted event) {
handleTestStepStarted(event);
}
};
private EventHandler stepFinishedHandler = new EventHandler() {
@Override
public void receive(TestStepFinished event) {
handleTestStepFinished(event);
}
};
private EventHandler embedEventhandler = new EventHandler() {
@Override
public void receive(EmbedEvent event) {
handleEmbed(event);
}
};
private EventHandler writeEventhandler = new EventHandler() {
@Override
public void receive(WriteEvent event) {
handleWrite(event);
}
};
private EventHandler runFinishedHandler = new EventHandler() {
@Override
public void receive(TestRunFinished event) {
finishReport();
}
};
public ExtentCucumberAdapter(String arg) { }
@Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestSourceRead.class, testSourceReadHandler);
publisher.registerHandlerFor(TestCaseStarted.class, caseStartedHandler);
publisher.registerHandlerFor(TestStepStarted.class, stepStartedHandler);
publisher.registerHandlerFor(TestStepFinished.class, stepFinishedHandler);
publisher.registerHandlerFor(EmbedEvent.class, embedEventhandler);
publisher.registerHandlerFor(WriteEvent.class, writeEventhandler);
publisher.registerHandlerFor(TestRunFinished.class, runFinishedHandler);
}
private void handleTestSourceRead(TestSourceRead event) {
testSources.addTestSourceReadEvent(event.uri, event);
}
private synchronized void handleTestCaseStarted(TestCaseStarted event) {
handleStartOfFeature(event.testCase);
handleScenarioOutline(event.testCase);
createTestCase(event.testCase);
if (testSources.hasBackground(currentFeatureFile, event.testCase.getLine())) {
createBackground(event.testCase);
}
}
private synchronized void handleTestStepStarted(TestStepStarted event) {
if (event.testStep instanceof PickleStepTestStep) {
PickleStepTestStep testStep = (PickleStepTestStep) event.testStep;
if (isFirstStepAfterBackground(testStep)) {
currentTestCaseMap = null;
}
createTestStep(testStep);
createMatchMap((PickleStepTestStep) event.testStep);
}
}
private synchronized void handleTestStepFinished(TestStepFinished event) {
if (event.testStep instanceof PickleStepTestStep) {
createResultMap(event.result);
} else if(event.testStep instanceof HookTestStep) {
//HookTestStep hookTestStep = (HookTestStep) event.testStep;
//HookType hookType = hookTestStep.getHookType();
createResultMap(event.result);
} else {
throw new IllegalStateException();
}
}
// todo
private synchronized void handleEmbed(EmbedEvent event) {
}
private void handleWrite(WriteEvent event) { }
private void finishReport() {
ExtentService.getInstance().flush();
}
private synchronized void handleStartOfFeature(TestCase testCase) {
if (currentFeatureFile == null || !currentFeatureFile.equals(testCase.getUri())) {
currentFeatureFile = testCase.getUri();
createFeature(testCase);
}
}
private synchronized Map createFeature(TestCase testCase) {
Map featureMap = new HashMap();
Feature feature = testSources.getFeature(testCase.getUri());
if (feature != null) {
ExtentTest t = ExtentService.getInstance()
.createTest(com.aventstack.extentreports.gherkin.model.Feature.class, feature.getName(), feature.getDescription());
featureTestThreadLocal.set(t);
List tagList = createTagsList(feature.getTags());
tagList.forEach(featureTestThreadLocal.get()::assignCategory);
}
return featureMap;
}
private List createTagsList(List tags) {
List tagList = new ArrayList<>();
for (Tag tag : tags) {
tagList.add(tag.getName());
}
return tagList;
}
private synchronized void handleScenarioOutline(TestCase testCase) {
TestSourcesModel.AstNode astNode = testSources.getAstNode(currentFeatureFile, testCase.getLine());
if (TestSourcesModel.isScenarioOutlineScenario(astNode)) {
ScenarioOutline scenarioOutline = (ScenarioOutline)TestSourcesModel.getScenarioDefinition(astNode);
if (currentScenarioOutline == null || !currentScenarioOutline.equals(scenarioOutline)) {
currentScenarioOutline = scenarioOutline;
createScenarioOutline(currentScenarioOutline);
addOutlineStepsToReport(scenarioOutline);
}
Examples examples = (Examples)astNode.parent.node;
if (currentExamples == null || !currentExamples.equals(examples)) {
currentExamples = examples;
createExamples(currentExamples);
}
} else {
scenarioOutlineThreadLocal.set(null);
currentScenarioOutline = null;
currentExamples = null;
}
}
private synchronized void createScenarioOutline(ScenarioOutline scenarioOutline) {
if (scenarioOutlineThreadLocal.get() == null) {
ExtentTest t = featureTestThreadLocal.get()
.createNode(com.aventstack.extentreports.gherkin.model.ScenarioOutline.class, scenarioOutline.getName(), scenarioOutline.getDescription());
scenarioOutlineThreadLocal.set(t);
List tags = createTagsList(scenarioOutline.getTags());
tags.forEach(scenarioOutlineThreadLocal.get()::assignCategory);
}
}
private synchronized void addOutlineStepsToReport(ScenarioOutline scenarioOutline) {
for (Step step : scenarioOutline.getSteps()) {
if (step.getArgument() != null) {
Node argument = step.getArgument();
if (argument instanceof DocString) {
createDocStringMap((DocString)argument);
} else if (argument instanceof DataTable) {
createDataTableList((DataTable)argument);
}
}
}
}
private Map createDocStringMap(DocString docString) {
Map docStringMap = new HashMap();
docStringMap.put("value", docString.getContent());
return docStringMap;
}
private List