From 280b5a4a30fcc316b2340beb6cc4574539fdb2d3 Mon Sep 17 00:00:00 2001 From: VimalRaj Selvam Date: Tue, 7 Mar 2017 06:49:12 +0530 Subject: [PATCH] Step data table will be logged and fixed the thread safe issue when run in parallel (#29) - Added a feature to display the data table in the step - Fixed the thread safe issue when run in parallel --- Changelog.md | 4 ++ Readme.md | 2 +- pom.xml | 2 +- .../listener/ExtentCucumberFormatter.java | 67 +++++++++++++------ .../cucumber/stepdefinitions/MyStepdefs.java | 10 ++- src/test/resources/features/MyFeature.feature | 3 + .../features/MySecondFeature.feature | 4 ++ 7 files changed, 65 insertions(+), 27 deletions(-) diff --git a/Changelog.md b/Changelog.md index 39221cc..b7da139 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,9 @@ # Changelog +### v2.0.1 +- Added a feature to display the data table in the step +- Fixed the thread safe issue when run in parallel + ### v2.0.0 - Upgraded to the latest version of ExtentReport and made it provided - Fixed scenario outline bugs diff --git a/Readme.md b/Readme.md index a25b999..55551dc 100644 --- a/Readme.md +++ b/Readme.md @@ -14,7 +14,7 @@ If you are using a maven based project, you can directly add this library as a d com.vimalselvam cucumber-extentsreport - 2.0.0 + 2.0.1 ``` diff --git a/pom.xml b/pom.xml index e3b5d50..8f0a158 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.vimalselvam cucumber-extentsreport - 2.0.0 + 2.0.1 jar Cucumber Extents Report diff --git a/src/main/java/com/cucumber/listener/ExtentCucumberFormatter.java b/src/main/java/com/cucumber/listener/ExtentCucumberFormatter.java index 1870885..a777392 100644 --- a/src/main/java/com/cucumber/listener/ExtentCucumberFormatter.java +++ b/src/main/java/com/cucumber/listener/ExtentCucumberFormatter.java @@ -2,12 +2,22 @@ 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 gherkin.formatter.Formatter; import gherkin.formatter.Reporter; -import gherkin.formatter.model.*; +import gherkin.formatter.model.Background; +import gherkin.formatter.model.DataTableRow; +import gherkin.formatter.model.Examples; +import gherkin.formatter.model.ExamplesTableRow; +import gherkin.formatter.model.Feature; +import gherkin.formatter.model.Match; +import gherkin.formatter.model.Result; +import gherkin.formatter.model.Scenario; +import gherkin.formatter.model.ScenarioOutline; +import gherkin.formatter.model.Step; +import gherkin.formatter.model.Tag; import java.io.File; import java.util.LinkedList; @@ -30,10 +40,12 @@ public class ExtentCucumberFormatter implements Reporter, Formatter { private boolean scenarioOutlineFlag; public ExtentCucumberFormatter(File file) { - setExtentHtmlReport(new ExtentHtmlReporter(file)); - ExtentReports extentReports = new ExtentReports(); - extentReports.attachReporter(getExtentHtmlReport()); - setExtentReport(extentReports); + if (getExtentReport() == null) { + setExtentHtmlReport(new ExtentHtmlReporter(file)); + ExtentReports extentReports = new ExtentReports(); + extentReports.attachReporter(getExtentHtmlReport()); + setExtentReport(extentReports); + } stepListThreadLocal.set(new LinkedList()); scenarioOutlineFlag = false; } @@ -104,20 +116,14 @@ public void startOfScenarioLifeCycle(Scenario scenario) { scenarioOutlineFlag = false; } -// if (scenario.getKeyword().trim().equalsIgnoreCase("Scenario")) { -// scenarioOutlineThreadLocal.set(null); -// } - 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()); + scenarioNode = + scenarioOutlineThreadLocal.get().createNode("Scenario: " + scenario.getName()); } else { - scenarioNode = featureTestThreadLocal.get() - .createNode(com.aventstack.extentreports.gherkin.model.Scenario.class, - scenario.getName()); + scenarioNode = + featureTestThreadLocal.get().createNode("Scenario: " + scenario.getName()); } for (Tag tag : scenario.getTags()) { @@ -183,14 +189,31 @@ 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 rows = step.getRows(); + int rowSize = rows.size(); + for (int i = 0; i < rowSize; i++) { + DataTableRow dataTableRow = rows.get(i); + List 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(); - GherkinKeyword keyword = null; - try { - keyword = new GherkinKeyword(step.getKeyword().trim()); - } catch (ClassNotFoundException e) { - e.printStackTrace(); + ExtentTest stepTest = scenarioTest.createNode(step.getKeyword() + step.getName()); + + if (data != null) { + Markup table = MarkupHelper.createTable(data); + stepTest.info(table); } - ExtentTest stepTest = scenarioTest.createNode(keyword, step.getName()); + stepTestThreadLocal.set(stepTest); } diff --git a/src/test/java/com/cucumber/stepdefinitions/MyStepdefs.java b/src/test/java/com/cucumber/stepdefinitions/MyStepdefs.java index f19c1c3..a45fad7 100644 --- a/src/test/java/com/cucumber/stepdefinitions/MyStepdefs.java +++ b/src/test/java/com/cucumber/stepdefinitions/MyStepdefs.java @@ -1,9 +1,10 @@ package com.cucumber.stepdefinitions; import com.cucumber.listener.Reporter; +import cucumber.api.DataTable; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; -import org.junit.Assert; +import cucumber.api.java.en.When; import java.io.IOException; @@ -13,8 +14,8 @@ public class MyStepdefs { throws IOException { Reporter.addStepLog("My test addStepLog message"); Reporter.addScenarioLog("This is scenario log"); -// Reporter.addScreenCaptureFromPath( -// "/Users/vimalrajselvam/Downloads/best-resume-template-2016-3.jpg", "My title"); + // Reporter.addScreenCaptureFromPath( + // "/Users/vimalrajselvam/Downloads/best-resume-template-2016-3.jpg", "My title"); } @Given("I have (\\d+) cukes in my bellies") public void I_have_cukes_in_my_bellies(int cukes) { @@ -24,4 +25,7 @@ public class MyStepdefs { @Then("^I print$") public void iPrint() throws Throwable { // Assert.assertTrue(false); } + + @When("^I login with credentials$") public void iLoginWithCredentials(DataTable table) { + } } diff --git a/src/test/resources/features/MyFeature.feature b/src/test/resources/features/MyFeature.feature index d80a218..903d190 100644 --- a/src/test/resources/features/MyFeature.feature +++ b/src/test/resources/features/MyFeature.feature @@ -14,4 +14,7 @@ Feature: My First Feature @scenarioTag Scenario: My Second Scenario Given I have 7 cukes in my bellies + When I login with credentials + | user1 | pass1 | + | user2 | pass2 | Then I print diff --git a/src/test/resources/features/MySecondFeature.feature b/src/test/resources/features/MySecondFeature.feature index 3eaeaeb..a7c2bb3 100644 --- a/src/test/resources/features/MySecondFeature.feature +++ b/src/test/resources/features/MySecondFeature.feature @@ -6,3 +6,7 @@ Feature: My Second Feature Scenario: My Second Scenario Given I have 7 cukes in my bellies + When I login with credentials + | user1 | pass1 | + | user2 | pass2 | + | user3 | pass3 |