Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

Commit

Permalink
Step data table will be logged and fixed the thread safe issue when r…
Browse files Browse the repository at this point in the history
…un in parallel (#29)

- Added a feature to display the data table in the step
- Fixed the thread safe issue when run in parallel
  • Loading branch information
email2vimalraj authored Mar 7, 2017
1 parent 86da44b commit 280b5a4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 27 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ If you are using a maven based project, you can directly add this library as a d
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
<packaging>jar</packaging>

<name>Cucumber Extents Report</name>
Expand Down
67 changes: 45 additions & 22 deletions src/main/java/com/cucumber/listener/ExtentCucumberFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Step>());
scenarioOutlineFlag = false;
}
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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<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();
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);
}

Expand Down
10 changes: 7 additions & 3 deletions src/test/java/com/cucumber/stepdefinitions/MyStepdefs.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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) {
Expand All @@ -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) {
}
}
3 changes: 3 additions & 0 deletions src/test/resources/features/MyFeature.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/test/resources/features/MySecondFeature.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

0 comments on commit 280b5a4

Please sign in to comment.