javacucumbercucumber-jvmcucumber-javatest-reporting

How to generate cucumber.json per test in respective test folders (I am using Cucumber 5 which uses TestNG for parallel execution)


I am using Cucumber 5.7.0 for parallel execution of scenarios. Code is as follows:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" data-provider-thread-count="2" preserve-order="true">
    <parameter name="platform" value="desktop"/>
    <test name="Chrome test">
        <parameter name="browser" value="chrome"></parameter>
        <classes>
            <class name="com.test.stepdefinitions.Runners.TestRunner"/>
        </classes>
    </test>
    <test name="FireFox Test">
        <parameter name="browser" value="firefox"></parameter>
        <classes>
            <class name="com.test.stepdefinitions.Runners.TestRunner"/>
        </classes>
    </test>
</suite>

TestRunner.java

public class TestRunner extends AbstractTestNGCucumberTests {
    private static Logger logger = LogManager.getLogger(TestRunner.class.getName());
    private static String reportPath;
    private static Main main = new Main();
    ThisRun thisRun = ThisRun.getInstance();
    private static ThreadLocal<WebDriver> driver = new ThreadLocal<>();
    DriverUtils driverUtils;
    private static String browser;
    private static String platform;


    @Override
    @DataProvider(parallel = true)
    public Object[][] scenarios() {
        return super.scenarios();
    }  

    @BeforeClass (alwaysRun = true)
    @Parameters({"browser", "platform"})
    public void beforeClassActivities(String browser, String platform) {
        this.browser = browser;
        this.platform = platform;

        reportPath = main.createReportFolder(browser);
        System.setProperty("cucumber.filter.tags", thisRun.getAsString(KEYS.TAG.name()));
        System.setProperty("cucumber.plugin", "json:" + reportPath + "/cucumber.json" + ",html:" + reportPath + "/cucumber.html");
     }

    @Before
    public void setup(Scenario scenario) throws IOException {
        logger.info("Running scenario : " + scenario.getName());
        loadFromPropertiesFile();
        addDriverProperties(platform, browser);
        printOptions();
    }

    private void printOptions() {
        logger.info("Platform: " + thisRun.getAsString(KEYS.PLATFORM.name()));
        logger.info("Sub Platform: " + thisRun.getAsString(KEYS.SUB_PLATFORM.name()));
        logger.info("Device id/Udid: " + thisRun.getAsString(KEYS.DEVICE_ID.name()));
        logger.info("Browser: " + browser);
        logger.info("URL: " + thisRun.getAsString(KEYS.APP_URL.name()));
        logger.info("OS: " + thisRun.getAsString(KEYS.OS_NAME.name()));
    }

    private void loadFromPropertiesFile() throws IOException {
        FileInputStream fileStream =
                new FileInputStream(thisRun.get(KEYS.TEST_RESOURCES.toString()) + "/CommonProperties.properties");
        Properties commonProperties = new Properties();
        commonProperties.load(fileStream);
        thisRun.add(KEYS.APP_URL, commonProperties.getProperty(KEYS.APP_URL.toString()));
    }

    private void addDriverProperties(String platform, String browser) throws MalformedURLException {
        driverUtils = new DriverUtils(platform, browser);
        driver.set(driverUtils.getDriver());
    }

    protected WebDriver getDriver() {
        return driver.get();
    }

    @After
    public void tearDown(Scenario scenario) {
        logger.info("Inside teardown(), now Browser will quit.....");
        ScreenShotUtils.embedScreenShotInReport(scenario, scenario.getName(), getDriver());
        driverUtils.quit(driver.get());
    }


    @AfterClass
    public void afterActivities() {

    }

}

In BeforeClass I am creating new folder and setting up cucumber.plugins in hope that I will be getting separate cucumber.json/html file/folder in respective folders. But cucumber.json and cucumber.html is getting generated in only in folder i.e. for "chrome" while "firefox" folder remains empty.

Is there a way I can generate cucumber.json each for one test in different folders?


Solution

  • I moved the code which was in BeforeClass to BeforeTest.