Search code examples
reportextent

Extent Report showing only one test case result when executing cucumber scripts in parallel using Cucable plugin


I have to generate Extent Report from all executed test scripts. I am running scripts in parallel. When I use TestNG or Selenium Grid for parallel execution, in those implementation, Extent Reports are getting generated perfectly covering each executed test scripts. But when I run scripts in parallel using Cucable Plugin, Extent report gets generated but would have only 1 test case report if 2 test cases were there in execution.

I am using Cucumber (Selenium), Junit Suite Runner, Cucable Plugin

I verified Extent Report code is thread safe. So not sure, why only in case of Cucable Plugin, Extent report gets only 1 test case. Someone told me, In case of testNG, testNG itself provides additional thread safe mechanism which helps internally to have all executed test cases in report.

ExtentTestManager.java

package com.jacksparrow.automation.extent.listeners;

import java.io.IOException;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.Markup;
import com.aventstack.extentreports.markuputils.MarkupHelper;

public class ExtentTestManager {

    public static ThreadLocal<ExtentTest> testReport = new ThreadLocal<ExtentTest>();
    static ExtentReports extent = ExtentManager.getReporter();

    public static synchronized ExtentTest getTest() {
        return testReport.get();
    }

    public static synchronized void setTest(ExtentTest tst) 
    { 
        testReport.set(tst); 
    }

    public static synchronized void logInfo(String message) {

        testReport.get().info(message);
    }

    public static synchronized void logPass(String message) {

        testReport.get().pass(message);
    }

    public static synchronized void scenarioPass() {

        String passLogg = "SCENARIO PASSED";
        Markup m = MarkupHelper.createLabel(passLogg, ExtentColor.GREEN);
        testReport.get().log(Status.PASS, m);


    }

    public static synchronized void logFail(String message) {

        testReport.get().fail(message);
    }

    public static synchronized boolean addScreenShotsOnFailure() {

        ExtentManager.captureScreenshot();
        try {

            testReport.get().fail("<b>" + "<font color=" + "red>" + "Screenshot of failure" + "</font>" + "</b>",
                    MediaEntityBuilder.createScreenCaptureFromPath(ExtentManager.screenshotName).build());
        } catch (IOException e) {

        }

        String failureLogg = "SCENARIO FAILED";
        Markup m = MarkupHelper.createLabel(failureLogg, ExtentColor.RED);
        testReport.get().log(Status.FAIL, m);
        return true;
    }

    public static synchronized boolean addScreenShots() {

        ExtentManager.captureScreenshot();
        try {
            testReport.get().info(("<b>" + "<font color=" + "green>" + "Screenshot" + "</font>" + "</b>"),
                    MediaEntityBuilder.createScreenCaptureFromPath(ExtentManager.screenshotName).build());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

    public static synchronized ExtentTest startTest(String testName) {
        return startTest(testName, "");
    }

    public static synchronized ExtentTest startTest(String testName, String desc) {
        ExtentTest test = extent.createTest(testName, desc);
        testReport.set(test);
        return test;
    }
}

ExtentManager.java

package com.jacksparrow.automation.extent.listeners;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import com.jacksparrow.automation.utilities.DriverManager;
import com.aventstack.extentreports.AnalysisStrategy;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class ExtentManager {

    static ExtentReports extent;
    static Date d = new Date();
    static String fileName = "Extent_" + d.toString().replace(":", "_").replace(" ", "_") + ".html";

    public synchronized static ExtentReports getReporter() {
        if (extent == null) {

            ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir")+"/target/extent-report/"+fileName);

            htmlReporter.loadXMLConfig(".\\src\\test\\resources\\extent-config.xml");
            htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
            htmlReporter.config().setChartVisibilityOnOpen(true);
            htmlReporter.config().setTheme(Theme.STANDARD);
            htmlReporter.config().setDocumentTitle(fileName);
            htmlReporter.config().setEncoding("utf-8");
            htmlReporter.config().setReportName(fileName);
            //htmlReporter.setAppendExisting(true);

            extent = new ExtentReports();
            extent.setAnalysisStrategy(AnalysisStrategy.TEST);
            extent.attachReporter(htmlReporter);
            extent.setSystemInfo("Automation Analyst", "Robin Tyagi");
            extent.setSystemInfo("Organization", "Way2Automation");
            extent.setSystemInfo("Build no", "W2A-1234");
        }
        return extent;
    }

    public static String screenshotPath;
    public static String screenshotName;
    static int i=0;
    public static void captureScreenshot() {
        i = i + 1;
        File scrFile = ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(OutputType.FILE);

        Date d = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("E dd MMM HH:mm:ss z yyyy");  
        String strDate = formatter.format(d);
        screenshotName = strDate.replace(":", "_").replace(" ", "_") + "_"+i+".jpg";

        try {
            FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "/target/extent-report/" + screenshotName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void createExtentReportDirectory() {
        File file = new File(System.getProperty("user.dir") + "/target/extent-report/");
        if (!file.exists()) {
            if (file.mkdir()) {
            } else {
            }
        }
    }
}

Please help me to understand what could be the correct thought in order to generate Extent Report having summary of all executed test scripts when Cucable Plugin is used for achieving parallel execution in Cucumber (Selenium)


Solution

  • After migrating to cucumber 4.0, I am able to generate single consolidated extent report. Thank you.