1

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)

TheSociety
  • 1,936
  • 2
  • 8
  • 20
  • Cucumber version? Extent version? – foursyth Feb 03 '19 at 21:21
  • Selenium V 3.8.1 | Cucumber Version 1.2.4 | Extent Report V 3.1.5 (My concern is, extent report gets generated perfectly when implemented parallel execution via testNG or Selenium Grid. But if i use Cucable plugin then report contains only 1 test case report. – TheSociety Feb 04 '19 at 17:37
  • The recommended path should be to upgrade to cucumber-4 which does this natively. Extent has an adapter for cucumber-4 which does this out of box. – foursyth Feb 04 '19 at 18:10
  • I am sorry I did not get your inputs correctly. Did you mean, i should upgrade cucumber to version 4. As per my understanding, there is no version 4 for cucumber under info.cukes so what do you mean by saying The recommended path should be to upgrade to cucumber-4. Please suggest info.cukes cucumber-java ${cucumber.version} test – TheSociety Feb 05 '19 at 19:16
  • Check under io.cucumber – foursyth Feb 05 '19 at 19:21
  • Hey foursyth, I am migrating to v4 but cucumber is not able to identify @given/when/then. Can you please put some light on this i have added question on https://stackoverflow.com/questions/54569416/cucumber-v4-io-cucumber-is-not-identifying-given-when-then-and-giving-error – TheSociety Feb 07 '19 at 08:49
  • Thank you @foursyth for giving me direction. – TheSociety Feb 23 '19 at 03:42

1 Answers1

0

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

TheSociety
  • 1,936
  • 2
  • 8
  • 20