anshooarora / extentreports-java

Community version of Extent API for Java has moved to https://github.com/extent-framework/
http://extentreports.com
Other
528 stars 318 forks source link

Not able to generate a single extent report for different class files with 3.1.5 version #1104

Open ghost opened 6 years ago

ghost commented 6 years ago

Summary

Not able to generate a single extent report for different class files with 3.1.5 version

Expected Behavior

Test results from different classes should get updated in Extent report

Current Behavior

Test results from different classes are not getting updated in Extent report. It is showing test results for TCs in one class file not in other files.

Sample

public class ExtentManager {
    private static ExtentReports extent;
    private static Platform platform;
    private static String reportFileName = "Test-Automaton-Report.html";

    private static String macPath = System.getProperty("user.dir")+ "/test-report";
    private static String windowsPath = System.getProperty("user.dir")+ "\\test-report";
    private static String macReportFileLoc = macPath + "/" + reportFileName;
    private static String winReportFileLoc = windowsPath + "\\" + reportFileName;

    public static synchronized ExtentReports getInstance() {
        if (extent == null)
            createInstance();
        return extent;
    }

    //Create an extent report instance
    public static ExtentReports createInstance() {
        platform = getCurrentPlatform();
        String fileName = getReportFileLocation(platform);
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);

        htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
        htmlReporter.config().setChartVisibilityOnOpen(true);
        htmlReporter.config().setTheme(Theme.DARK);
        htmlReporter.config().setDocumentTitle(fileName);
        htmlReporter.config().setEncoding("utf-8");
        htmlReporter.config().setReportName(fileName);

        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);

        return extent;
    }

public class TestListener implements ITestListener {

    String filePath = "target\\screenshot";

    //Extent Report Declarations
    private static ExtentReports extent = ExtentManager.createInstance();
    private static ThreadLocal<ExtentTest> test= new ThreadLocal<ExtentTest>();

    CommonUtility commomUtil;

    @Override
    public synchronized void onStart(ITestContext context) {
        System.out.println("Extent Reports Test execution started!");
    }

    @Override
    public synchronized void onFinish(ITestContext context) {
        System.out.println(("Extent Reports Test execution is ending!"));
        extent.flush();
    }

    @Override
    public void onTestStart(ITestResult result) {
        System.out.println((result.getMethod().getMethodName() + " started!"));
        ExtentTest extentTest = extent.createTest(result.getMethod().getMethodName(),result.getMethod().getDescription());
        test.set(extentTest);
    }

    @Override
    public void onTestSuccess(ITestResult result) {
       System.out.println((result.getMethod().getMethodName() + " passed!"));
       test.get().pass("Test passed");
    }

    @Override
    public void onTestFailure(ITestResult result) {
      System.out.println("***** Error " + result.getMethod().getMethodName() + " test has failed *****");
      String methodName = result.getName().toString().trim();
      this.takeScreenShot(EnvSetup.WEBDRIVER, methodName);
      test.get().fail(result.getThrowable());
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        System.out.println((result.getMethod().getMethodName() + " skipped!"));
        test.get().skip(result.getThrowable());
    }

    @Override
    public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        System.out.println(("onTestFailedButWithinSuccessPercentage for " + result.getMethod().getMethodName()));

    }
}

Environment Details

Screenshots

ghost commented 6 years ago

@anshooarora - could you please help out here.

foursyth commented 6 years ago

This should work:

public class ExtentTestNGReportBuilder {

    private static ExtentReports extent = ExtentManager.createInstance("test-output/extent.html");
    private static ThreadLocal<ExtentTest> parentTest = new ThreadLocal();
    private static ThreadLocal<ExtentTest> test = new ThreadLocal();

    @BeforeClass
    public synchronized void beforeClass() {
        ExtentTest parent = extent.createTest(getClass().getName());
        parentTest.set(parent);
    }

    @BeforeMethod
    public synchronized void beforeMethod(Method method) {
        ExtentTest child = parentTest.get().createNode(method.getName());
        test.set(child);
    }

    @AfterMethod
    public synchronized void afterMethod(ITestResult result) {
        if (result.getStatus() == ITestResult.FAILURE)
            test.get().fail(result.getThrowable());
        else if (result.getStatus() == ITestResult.SKIP)
            test.get().skip(result.getThrowable());
        else
            test.get().pass("Test passed");

        extent.flush();
    }
}

public class ExtentManager {

    private static ExtentReports extent;

    public static ExtentReports getInstance() {
        if (extent == null)
            createInstance("test-output/extent.html");

        return extent;
    }

    public static ExtentReports createInstance(String fileName) {
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
        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);

        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);

        return extent;
    }
}