From 68d24246df10c18c4b0d65b45936df388b7f7916 Mon Sep 17 00:00:00 2001 From: VimalRaj Selvam Date: Mon, 12 Jun 2017 06:38:52 +0530 Subject: [PATCH] Added feature to configure the report location in 3 different ways --- .../listener/ExtentCucumberFormatter.java | 19 +++++++++++++++++-- .../cucumber/runner/MyTestNGCukesRunner.java | 2 +- .../com/cucumber/runner/RunCukesTest.java | 9 +++++++-- .../cucumber/runner/TestNGCukesRunner.java | 10 ++++++++-- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/cucumber/listener/ExtentCucumberFormatter.java b/src/main/java/com/cucumber/listener/ExtentCucumberFormatter.java index 83e33c0..5522807 100644 --- a/src/main/java/com/cucumber/listener/ExtentCucumberFormatter.java +++ b/src/main/java/com/cucumber/listener/ExtentCucumberFormatter.java @@ -19,6 +19,7 @@ */ public class ExtentCucumberFormatter implements Reporter, Formatter { + public static final String REPORT_PATH_PROPERTY = "cucumberReportPath"; private static ExtentReports extentReports; private static ExtentHtmlReporter htmlReporter; private static ThreadLocal featureTestThreadLocal = new InheritableThreadLocal<>(); @@ -40,8 +41,22 @@ private static void setExtentHtmlReport(File file) { if (htmlReporter != null) { return; } - if (!file.exists()) { - file.getParentFile().mkdirs(); + try { + if (!file.exists()) { + file.getParentFile().mkdirs(); + } + } catch (NullPointerException e) { + String filePath; + if (System.getProperty(REPORT_PATH_PROPERTY) != null && !System.getProperty(REPORT_PATH_PROPERTY).isEmpty()) { + filePath = System.getProperty(REPORT_PATH_PROPERTY); + } else { + filePath = "output" + File.separator + "Run_" + System.currentTimeMillis() + + File.separator + "report.html"; + } + file = new File(filePath); + if (!file.exists()) { + file.getParentFile().mkdirs(); + } } htmlReporter = new ExtentHtmlReporter(file); } diff --git a/src/test/java/com/cucumber/runner/MyTestNGCukesRunner.java b/src/test/java/com/cucumber/runner/MyTestNGCukesRunner.java index 9429daa..d495da3 100644 --- a/src/test/java/com/cucumber/runner/MyTestNGCukesRunner.java +++ b/src/test/java/com/cucumber/runner/MyTestNGCukesRunner.java @@ -14,7 +14,7 @@ ) public class MyTestNGCukesRunner extends AbstractTestNGCucumberTests { @AfterClass - public static void setup() { + public static void teardown() { Reporter.loadXMLConfig(new File("src/test/resources/extent-config.xml")); Reporter.setSystemInfo("user", System.getProperty("user.name")); Reporter.setSystemInfo("os", "Mac OSX"); diff --git a/src/test/java/com/cucumber/runner/RunCukesTest.java b/src/test/java/com/cucumber/runner/RunCukesTest.java index 3c412ad..f928269 100644 --- a/src/test/java/com/cucumber/runner/RunCukesTest.java +++ b/src/test/java/com/cucumber/runner/RunCukesTest.java @@ -4,6 +4,7 @@ import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.runner.RunWith; import java.io.File; @@ -15,12 +16,16 @@ @CucumberOptions( features = {"src/test/resources/features"}, glue = {"com.cucumber.stepdefinitions"}, - plugin = {"com.cucumber.listener.ExtentCucumberFormatter:output/report.html"} + plugin = {"com.cucumber.listener.ExtentCucumberFormatter:"} ) public class RunCukesTest { + @BeforeClass + public static void setup() { + System.setProperty("cucumberReportPath", "output/myreport.html"); + } @AfterClass - public static void setup() { + public static void teardown() { Reporter.loadXMLConfig(new File("src/test/resources/extent-config.xml")); Reporter.setSystemInfo("user", System.getProperty("user.name")); Reporter.setSystemInfo("os", "Mac OSX"); diff --git a/src/test/java/com/cucumber/runner/TestNGCukesRunner.java b/src/test/java/com/cucumber/runner/TestNGCukesRunner.java index c6bae6c..9b1311e 100644 --- a/src/test/java/com/cucumber/runner/TestNGCukesRunner.java +++ b/src/test/java/com/cucumber/runner/TestNGCukesRunner.java @@ -4,17 +4,23 @@ import cucumber.api.CucumberOptions; import cucumber.api.testng.AbstractTestNGCucumberTests; import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; import java.io.File; @CucumberOptions( features = {"src/test/resources/features/MyFeature.feature"}, glue = {"com.cucumber.stepdefinitions"}, - plugin = {"com.cucumber.listener.ExtentCucumberFormatter:output/report.html"} + plugin = {"com.cucumber.listener.ExtentCucumberFormatter:"} ) public class TestNGCukesRunner extends AbstractTestNGCucumberTests { + @BeforeClass + public static void setup() { + System.err.println("Hello"); + } + @AfterClass - public static void setup() { + public static void teardown() { Reporter.loadXMLConfig(new File("src/test/resources/extent-config.xml")); Reporter.setSystemInfo("user", System.getProperty("user.name")); Reporter.setSystemInfo("os", "Mac OSX");