com.report.CucumberExtent Maven / Gradle / Ivy
package com.report;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.gherkin.model.And;
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.gherkin.model.Then;
import com.aventstack.extentreports.gherkin.model.When;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Protocol;
import com.aventstack.extentreports.reporter.configuration.Theme;
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.EventListener;
import cucumber.api.event.EventPublisher;
import cucumber.api.event.TestCaseFinished;
import cucumber.api.event.TestCaseStarted;
import cucumber.api.event.TestRunFinished;
import cucumber.api.event.TestRunStarted;
import cucumber.api.event.TestSourceRead;
import cucumber.api.event.TestStepFinished;
import cucumber.api.event.TestStepStarted;
import cucumber.api.event.WriteEvent;
import cucumber.runtime.CucumberException;
import gherkin.ast.ScenarioDefinition;
import gherkin.ast.Step;
import gherkin.pickles.PickleTag;
public class CucumberExtent implements EventListener{
private final TestSourcesModel testSources = new TestSourcesModel();
private String currentFeatureFile;
private final String htmlReportDir;
@SuppressWarnings("serial")
private final Map MIME_TYPES_EXTENSIONS = new HashMap() {
{
put("image/bmp", "bmp");
put("image/gif", "gif");
put("image/jpeg", "jpg");
put("image/png", "png");
put("image/svg+xml", "svg");
put("video/ogg", "ogg");
}
};
private int embeddedIndex;
private String directory;
private String screenPath;
//Builds a new report using the html template
private ExtentHtmlReporter extentHtmlReporter = null;
private ExtentReports extent = null;
private ExtentTest extentBDDFeature = null;
private ExtentTest extentBDDScenario = null;
private ExtentTest extentBDDStep = null;
private ExtentTest extentTest = null;
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestSourceRead.class, testSourceReadHandler);
publisher.registerHandlerFor(TestRunStarted.class, runStartedHandler);
publisher.registerHandlerFor(TestRunFinished.class, runFinishedHandler);
publisher.registerHandlerFor(TestCaseStarted.class, caseStartedHandler);
publisher.registerHandlerFor(TestCaseFinished.class, caseFinishedHandler);
publisher.registerHandlerFor(TestStepStarted.class, stepStartedHandler);
publisher.registerHandlerFor(TestStepFinished.class, stepFinishedHandler);
publisher.registerHandlerFor(EmbedEvent.class, embedEventhandler);
publisher.registerHandlerFor(WriteEvent.class, writeEventhandler);
}
private EventHandler runStartedHandler = new EventHandler() {
@Override
public void receive(TestRunStarted event) {
handleTestRunStarted(event);
}
};
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) {
//Calling method that will be called when TestStepFinished
try {
handleTestStepFinished(event);
} catch (IOException e) {
e.printStackTrace();
}
}
};
private EventHandler caseFinishedHandler = new EventHandler() {
@Override
public void receive(TestCaseFinished event) {
handleTestCaseFinished(event);
}
};
private EventHandler runFinishedHandler = new EventHandler() {
@Override
public void receive(TestRunFinished event) {
handleTestRunFinished(event);
}
};
private EventHandler embedEventhandler = new EventHandler() {
@Override
public void receive(EmbedEvent event) {
try {
handleEmbed(event);
} catch (IOException e) {
e.printStackTrace();
}
}
};
private EventHandler writeEventhandler = new EventHandler() {
@Override
public void receive(WriteEvent event) {
handleWrite(event);
}
};
public CucumberExtent(String htmlReportDir) {
this.htmlReportDir = htmlReportDir;
this.directory = getLocation(htmlReportDir);
}
private void handleTestRunStarted(TestRunStarted event) {
attachExtentHtmlReporter();
configureExtentHtmlReporter();
}
private void handleTestSourceRead(TestSourceRead event) {
testSources.addTestSourceReadEvent(event.uri, event);
}
private void handleTestCaseStarted(TestCaseStarted event) {
startOfFeature(event.testCase);
if((CucumberExtentOptions.getInstance().getReportLevel()!=null)&&(CucumberExtentOptions.getInstance().getReportLevel().equals("Feature")))
createExtentBDDTestCase(event.testCase);
else
createExtentTest(event.testCase);
//Method to check the file in the given uri for any external location test data
//check if currentFeatureFile variable is null or not
}
private void handleTestStepStarted(TestStepStarted event) {
}
//Called at the end of the test step
private void handleTestStepFinished(TestStepFinished event) throws IOException {
if (event.testStep instanceof PickleStepTestStep) {
PickleStepTestStep testStep = (PickleStepTestStep) event.testStep;
if((CucumberExtentOptions.getInstance().getReportLevel()!=null)&&(CucumberExtentOptions.getInstance().getReportLevel().equals("Feature")))
createExtentBDDTestStep(testStep,event.result);
else
createExtentTestStep(testStep,event.result);
}
}
private void handleTestCaseFinished(TestCaseFinished event) {
if((CucumberExtentOptions.getInstance().getReportLevel()!=null)&&(CucumberExtentOptions.getInstance().getReportLevel().equals("Feature")))
endExtentBDDTestCase();
else
endExtentTestCase();
}
private void handleTestRunFinished(TestRunFinished event) {
generateExtentHtmlReporter();
}
private void handleWrite(WriteEvent event) {}
private void handleEmbed(EmbedEvent event) throws IOException {
String mimeType = event.mimeType;
String extension = MIME_TYPES_EXTENSIONS.get(mimeType);
if (extension != null) {
StringBuilder fileName = new StringBuilder("embedded").append(embeddedIndex++).append(Thread.currentThread().getId()).append(".").append(extension);
convertByteArrayToImage(event.data,fileName.toString(),extension);
this.extentBDDStep.addScreenCaptureFromPath(this.screenPath);
this.screenPath = null;
this.extentBDDStep = null;
}
}
//Method to convert byte[] to image
private void convertByteArrayToImage(byte[] byteImage, String fileName, String extension) throws IOException {
this.screenPath = this.directory+fileName;
ImageIO.write(ImageIO.read(new ByteArrayInputStream(byteImage)), extension, new File(this.screenPath));
}
//Method to get location of report
private String getLocation(String htmlReportDir) {
String[] htmlReportDirArray = htmlReportDir.split("/");
String dir = "";
for(int i = 0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy