To generate Extent Report need to add these codes
package main.java.listener; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.GherkinKeyword; import com.aventstack.extentreports.markuputils.Markup; import com.aventstack.extentreports.markuputils.MarkupHelper; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; import com.aventstack.extentreports.reporter.ExtentXReporter; import com.aventstack.extentreports.reporter.configuration.ChartLocation; import gherkin.formatter.Formatter; import gherkin.formatter.Reporter; import gherkin.formatter.model.*; import java.io.File; import java.util.LinkedList; import java.util.List; /** * A cucumber based reporting listener which generates the Extent Report */ public class ExtentCucumberFormatter implements Reporter, Formatter { private static ExtentReports extentReports; private static ExtentHtmlReporter htmlReporter; private static ThreadLocal<ExtentTest> featureTestThreadLocal = new InheritableThreadLocal<>(); private static ThreadLocal<ExtentTest> scenarioOutlineThreadLocal = new InheritableThreadLocal<>(); static ThreadLocal<ExtentTest> scenarioThreadLocal = new InheritableThreadLocal<>(); private static ThreadLocal<LinkedList<Step>> stepListThreadLocal = new InheritableThreadLocal<>(); static ThreadLocal<ExtentTest> stepTestThreadLocal = new InheritableThreadLocal<>(); private boolean scenarioOutlineFlag; public ExtentCucumberFormatter(File file) { setExtentHtmlReport(file); setExtentReport(); stepListThreadLocal.set(new LinkedList<Step>()); scenarioOutlineFlag = false; } private static void setExtentHtmlReport(File file) { if (htmlReporter != null) { return; } if (!file.exists()) { file.getParentFile().mkdirs(); } htmlReporter = new ExtentHtmlReporter(file); htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM); htmlReporter.config().setDocumentTitle("ExtentReports - Automation"); htmlReporter.config().setReportName("ExtentReports - Automation"); // htmlReporter.config().setAutoCreateRelativePathMedia(true); } static ExtentHtmlReporter getExtentHtmlReport() { return htmlReporter; } private static void setExtentReport() { if (extentReports != null) { return; } extentReports = new ExtentReports(); extentReports.attachReporter(htmlReporter); // Starting ExtentX Server. /*ExtentXReporter extentx = new ExtentXReporter("localhost", 27017); extentx.config().setProjectName("Yaskawa"); extentx.config().setReportName("Login"); extentReports.attachReporter(htmlReporter, extentx); extentx.config().setServerUrl("http://localhost:1337");*/ //extentx.flush(); } static ExtentReports getExtentReport() { return extentReports; } public void syntaxError(String state, String event, List<String> legalEvents, String uri, Integer line) { } public void uri(String uri) { } public void feature(Feature feature) { featureTestThreadLocal.set(getExtentReport() .createTest(com.aventstack.extentreports.gherkin.model.Feature.class, feature.getName())); ExtentTest test = featureTestThreadLocal.get(); for (Tag tag : feature.getTags()) { test.assignCategory(tag.getName()); } } public void scenarioOutline(ScenarioOutline scenarioOutline) { scenarioOutlineFlag = true; ExtentTest node = featureTestThreadLocal.get().createNode( com.aventstack.extentreports.gherkin.model.ScenarioOutline.class, scenarioOutline.getName()); scenarioOutlineThreadLocal.set(node); } public void examples(Examples examples) { ExtentTest test = scenarioOutlineThreadLocal.get(); String[][] data = null; List<ExamplesTableRow> rows = examples.getRows(); int rowSize = rows.size(); for (int i = 0; i < rowSize; i++) { ExamplesTableRow examplesTableRow = rows.get(i); List<String> cells = examplesTableRow.getCells(); int cellSize = cells.size(); if (data == null) { data = new String[rowSize][cellSize]; } for (int j = 0; j < cellSize; j++) { data[i][j] = cells.get(j); } } test.info(MarkupHelper.createTable(data)); } public void startOfScenarioLifeCycle(Scenario scenario) { if (scenarioOutlineFlag) { scenarioOutlineFlag = false; } ExtentTest scenarioNode; if (scenarioOutlineThreadLocal.get() != null && scenario.getKeyword().trim().equalsIgnoreCase("Scenario Outline")) { scenarioNode = scenarioOutlineThreadLocal.get() .createNode(com.aventstack.extentreports.gherkin.model.Scenario.class, scenario.getName()); } else { scenarioNode = featureTestThreadLocal.get() .createNode(com.aventstack.extentreports.gherkin.model.Scenario.class, scenario.getName()); } for (Tag tag : scenario.getTags()) { scenarioNode.assignCategory(tag.getName()); } scenarioThreadLocal.set(scenarioNode); } public void background(Background background) { } public void scenario(Scenario scenario) { } public void step(Step step) { if (scenarioOutlineFlag) { return; } stepListThreadLocal.get().add(step); } public void endOfScenarioLifeCycle(Scenario scenario) { } public void done() { getExtentReport().flush(); } public void close() { } public void eof() { } public void before(Match match, Result result) { } public void result(Result result) { if (scenarioOutlineFlag) { return; } if (Result.PASSED.equals(result.getStatus())) { stepTestThreadLocal.get().pass(Result.PASSED); } else if (Result.FAILED.equals(result.getStatus())) { stepTestThreadLocal.get().fail(result.getError()); } else if (Result.SKIPPED.equals(result)) { stepTestThreadLocal.get().skip(Result.SKIPPED.getStatus()); } else if (Result.UNDEFINED.equals(result)) { stepTestThreadLocal.get().skip(Result.UNDEFINED.getStatus()); } } public void after(Match match, Result result) { } public void match(Match match) { Step step = stepListThreadLocal.get().poll(); String data[][] = null; if (step.getRows() != null) { List<DataTableRow> rows = step.getRows(); int rowSize = rows.size(); for (int i = 0; i < rowSize; i++) { DataTableRow dataTableRow = rows.get(i); List<String> cells = dataTableRow.getCells(); int cellSize = cells.size(); if (data == null) { data = new String[rowSize][cellSize]; } for (int j = 0; j < cellSize; j++) { data[i][j] = cells.get(j); } } } ExtentTest scenarioTest = scenarioThreadLocal.get(); ExtentTest stepTest = null; try { stepTest = scenarioTest.createNode(new GherkinKeyword(step.getKeyword()), step.getKeyword() + step.getName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } if (data != null) { Markup table = MarkupHelper.createTable(data); stepTest.info(table); } stepTestThreadLocal.set(stepTest); } public void embedding(String mimeType, byte[] data) { } public void write(String text) { } }
package main.java.listener; import java.io.File; /** * An enum which holds the properties to be set for extent reporter */ public enum ExtentProperties { INSTANCE; private String reportPath; private String extentXServerUrl; private String projectName; ExtentProperties() { this.reportPath = "output" + File.separator + "Run_" + System.currentTimeMillis() + File.separator + "report1.html"; this.projectName = "default"; } /** * Gets the report path * @return The report path */ public String getReportPath() { return reportPath; } /** * Sets the report path * @param reportPath The report path value */ public void setReportPath(String reportPath) { this.reportPath = reportPath; } /** * Gets the ExtentX server URL * @return The ExtentX server URL */ public String getExtentXServerUrl() { return extentXServerUrl; } /** * Sets the ExtentX server URL * @param extentXServerUrl The ExtentX server URL */ public void setExtentXServerUrl(String extentXServerUrl) { this.extentXServerUrl = extentXServerUrl; } /** * Gets the project name * @return The project name */ public String getProjectName() { return projectName; } /** * Gets the project name * @param projectName The project name */ public void setProjectName(String projectName) { this.projectName = projectName; } }
package main.java.listener; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; /** * This class houses few utilities required for the report */ public class Reporter { private static Map<String, Boolean> systemInfoKeyMap = new HashMap<>(); private Reporter() { // Defeat instantiation } /** * Gets the {@link ExtentHtmlReporter} instance created through listener * * @return The {@link ExtentHtmlReporter} instance */ public static ExtentHtmlReporter getExtentHtmlReport() { return ExtentCucumberFormatter.getExtentHtmlReport(); } /** * Gets the {@link ExtentReports} instance created through the listener * * @return The {@link ExtentReports} instance */ public static ExtentReports getExtentReport() { return ExtentCucumberFormatter.getExtentReport(); } /** * Loads the XML config file * * @param xmlPath The xml path in string */ public static void loadXMLConfig(String xmlPath) { getExtentHtmlReport().loadXMLConfig(xmlPath); } /** * Loads the XML config file * * @param file The file path of the XML */ public static void loadXMLConfig(File file) { getExtentHtmlReport().loadXMLConfig(file); } /** * Adds an info message to the current step * * @param message The message to be logged to the current step */ public static void addStepLog(String message) { getCurrentStep().info(message); } /** * Adds an info message to the current scenario * * @param message The message to be logged to the current scenario */ public static void addScenarioLog(String message) { getCurrentScenario().info(message); } /** * Adds the screenshot from the given path to the current step * * @param imagePath The image path * @throws IOException Exception if imagePath is erroneous */ public static void addScreenCaptureFromPath(String imagePath) throws IOException { getCurrentStep().addScreenCaptureFromPath(imagePath); } /** * Adds the screenshot from the given path with the given title to the current step * * @param imagePath The image path * @param title The title for the image * @throws IOException Exception if imagePath is erroneous */ public static void addScreenCaptureFromPath(String imagePath, String title) throws IOException { getCurrentStep().addScreenCaptureFromPath(imagePath, title); } /** * Adds the screen cast from the given path to the current step * * @param screenCastPath The screen cast path * @throws IOException Exception if imagePath is erroneous */ public static void addScreenCast(String screenCastPath) throws IOException { getCurrentStep().addScreencastFromPath(screenCastPath); } /** * Sets the system information with the given key value pair * * @param key The name of the key * @param value The value of the given key */ public static void setSystemInfo(String key, String value) { if (systemInfoKeyMap.isEmpty() || !systemInfoKeyMap.containsKey(key)) { systemInfoKeyMap.put(key, false); } if (systemInfoKeyMap.get(key)) { return; } getExtentReport().setSystemInfo(key, value); systemInfoKeyMap.put(key, true); } /** * Sets the test runner output with the given list of strings * * @param log The list of string messages */ public static void setTestRunnerOutput(List<String> log) { getExtentReport().setTestRunnerOutput(log); } /** * Sets the test runner output with the given string * * @param outputMessage The message to be shown in the test runner output screen */ public static void setTestRunnerOutput(String outputMessage) { getExtentReport().setTestRunnerOutput(outputMessage); } /** * Sets the author name for the current scenario * @param authorName The author name of the current scenario */ public static void assignAuthor(String... authorName) { getCurrentScenario().assignAuthor(authorName); } private static ExtentTest getCurrentStep() { return ExtentCucumberFormatter.stepTestThreadLocal.get(); } private static ExtentTest getCurrentScenario() { return ExtentCucumberFormatter.scenarioThreadLocal.get(); } }

No comments:
Post a Comment