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

Commit

Permalink
Fixed the bug while running on parallel using TestNG (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
email2vimalraj authored Mar 10, 2017
1 parent c3ba300 commit 65de5b5
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*.iml
target
output
*.DS_Store
*.DS_Store
/test-output/report.html
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### v2.0.3
- Fixed the bug while running on parallel using TestNG

### v2.0.2
- Fixed the system info when execute from multiple runners

Expand Down
4 changes: 3 additions & 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.1</version>
<version>2.0.3</version>
</dependency>
```

Expand Down Expand Up @@ -64,6 +64,8 @@ public class RunCukesTest {

```

Please note that the plugin `com.cucumber.listener.ExtentCucumberFormatter` takes an argument of where the report file should be generated. If you omit the parameter of file path, the report will be generated in the `test-output` folder in your project directory by default with the file name as `report.html`.

The above example shows a JUnit runner. However, you can use the TestNG runner too.
Also make sure the `loadXMLConfig`, `setSystemInfo` and `setTestRunnerOutput` methods should be in your `@AfterClass` method.

Expand Down
21 changes: 20 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@

<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>2.0.2</version>
<version>2.0.3</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>Cucumber Extents Report</name>
Expand Down Expand Up @@ -57,6 +69,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
65 changes: 33 additions & 32 deletions src/main/java/com/cucumber/listener/ExtentCucumberFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import gherkin.formatter.Formatter;
import gherkin.formatter.Reporter;
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 gherkin.formatter.model.*;

import java.io.File;
import java.util.LinkedList;
Expand All @@ -27,43 +17,54 @@
* A cucumber based reporting listener which generates the Extent Report
*/
public class ExtentCucumberFormatter implements Reporter, Formatter {
private static ThreadLocal<ExtentReports> reportsThreadLocal = new ThreadLocal<ExtentReports>();
private static ThreadLocal<ExtentHtmlReporter> htmlReporterThreadLocal =
new ThreadLocal<ExtentHtmlReporter>();
private static ThreadLocal<ExtentTest> featureTestThreadLocal = new ThreadLocal<ExtentTest>();
private static ThreadLocal<ExtentTest> scenarioOutlineThreadLocal =
new ThreadLocal<ExtentTest>();
static ThreadLocal<ExtentTest> scenarioThreadLocal = new ThreadLocal<ExtentTest>();
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 ThreadLocal<LinkedList<Step>>();
static ThreadLocal<ExtentTest> stepTestThreadLocal = new ThreadLocal<ExtentTest>();
new InheritableThreadLocal<>();
static ThreadLocal<ExtentTest> stepTestThreadLocal = new InheritableThreadLocal<>();
private boolean scenarioOutlineFlag;

public ExtentCucumberFormatter() {
this(null);
}

public ExtentCucumberFormatter(File file) {
if (getExtentReport() == null) {
setExtentHtmlReport(new ExtentHtmlReporter(file));
ExtentReports extentReports = new ExtentReports();
extentReports.attachReporter(getExtentHtmlReport());
setExtentReport(extentReports);
}
setExtentHtmlReport(file);
setExtentReport();
stepListThreadLocal.set(new LinkedList<Step>());
scenarioOutlineFlag = false;
}

private static void setExtentHtmlReport(ExtentHtmlReporter htmlReport) {
htmlReporterThreadLocal.set(htmlReport);
private static void setExtentHtmlReport(File file) {
if (htmlReporter != null) {
return;
}
if (file == null) {
file = new File("test-output/report.html");
if (!file.exists()) {
file.getParentFile().mkdirs();
}
}
htmlReporter = new ExtentHtmlReporter(file);
}

static ExtentHtmlReporter getExtentHtmlReport() {
return htmlReporterThreadLocal.get();
return htmlReporter;
}

private static void setExtentReport(ExtentReports extentReports) {
reportsThreadLocal.set(extentReports);
private static void setExtentReport() {
if (extentReports != null) {
return;
}
extentReports = new ExtentReports();
extentReports.attachReporter(htmlReporter);
}

static ExtentReports getExtentReport() {
return reportsThreadLocal.get();
return extentReports;
}

public void syntaxError(String state, String event, List<String> legalEvents, String uri,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cucumber/listener/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* This class houses few utilities required for the report
*/
public class Reporter {
private static Map<String, Boolean> systemInfoKeyMap = new HashMap<String, Boolean>();
private static Map<String, Boolean> systemInfoKeyMap = new HashMap<>();

private Reporter() {
// Defeat instantiation
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/cucumber/runner/MyTestNGCukesRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.cucumber.runner;

import com.cucumber.listener.Reporter;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import org.testng.annotations.AfterClass;

import java.io.File;

@CucumberOptions(
features = {"src/test/resources/features/MySecondFeature.feature"},
glue = {"com.cucumber.stepdefinitions"},
plugin = {"com.cucumber.listener.ExtentCucumberFormatter"}
)
public class MyTestNGCukesRunner extends AbstractTestNGCucumberTests {
@AfterClass
public static void setup() {
Reporter.loadXMLConfig(new File("src/test/resources/extent-config.xml"));
Reporter.setSystemInfo("user", System.getProperty("user.name"));
Reporter.setSystemInfo("os", "Mac OSX");
Reporter.setTestRunnerOutput("Sample test runner output message");
}
}
23 changes: 23 additions & 0 deletions src/test/java/com/cucumber/runner/TestNGCukesRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.cucumber.runner;

import com.cucumber.listener.Reporter;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import org.testng.annotations.AfterClass;

import java.io.File;

@CucumberOptions(
features = {"src/test/resources/features/MyFeature.feature"},
glue = {"com.cucumber.stepdefinitions"},
plugin = {"com.cucumber.listener.ExtentCucumberFormatter"}
)
public class TestNGCukesRunner extends AbstractTestNGCucumberTests {
@AfterClass
public static void setup() {
Reporter.loadXMLConfig(new File("src/test/resources/extent-config.xml"));
Reporter.setSystemInfo("user", System.getProperty("user.name"));
Reporter.setSystemInfo("os", "Mac OSX");
Reporter.setTestRunnerOutput("Sample test runner output message");
}
}
10 changes: 10 additions & 0 deletions src/test/resources/suite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1" verbose="2" parallel="classes">
<test name="Test" >
<classes>
<class name="com.cucumber.runner.TestNGCukesRunner" />
<class name="com.cucumber.runner.MyTestNGCukesRunner" />
</classes>
</test>
</suite>

0 comments on commit 65de5b5

Please sign in to comment.