anshooarora / extentreports-java

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

Create new report for each execution and add test steps with screenshots #1039

Open seeramzan opened 6 years ago

seeramzan commented 6 years ago

capture

Summary

I am controlling TestNG through listener where i am invocking testng methods with invocation count and providing different data sets from excel.

I have implemented extent reports listener its showing the results are per my needs. but i am not able to do the following

1) Test case description on report 2) create new report on each exectuion 3) execution steps with screenshots.

Sample


// Sample code goes here
package CommonLib.Listeners;

import TestScenarios.TestScenarios;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.model.ScreenCapture;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import org.testng.*;
import org.testng.xml.XmlSuite;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static CommonLib.DataManger.getPropertyFile;
import static CommonLib.Executor.ProjectName;

public class listener implements IReporter {

    private static final String OUTPUT_FOLDER = "test-output/";
    private static final String FILE_NAME = "_Test Summary Report.html";
    static String dir = System.getProperty("user.dir");
    static String ReportPath = getPropertyFile("ReportPath");
    String ExtentReportXMLPath = getPropertyFile("ExtentReportXMLPath");
    private ExtentReports extent;
    ScreenCapture screenCapture;
    public static ExtentTest test;
    @Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
        init();

        for (ISuite suite : suites) {
            Map<String, ISuiteResult> result = suite.getResults();
            for (ISuiteResult r : result.values()) {
                ITestContext context = r.getTestContext();

                buildTestNodes(context.getFailedTests(), Status.FAIL);
                buildTestNodes(context.getSkippedTests(), Status.SKIP);
                buildTestNodes(context.getPassedTests(), Status.PASS);

            }
        }

        for (String s : Reporter.getOutput()) {
            extent.setTestRunnerOutput(s);
        }

        extent.flush();
    }

    private void init() {
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(dir + ReportPath + "/" + ProjectName + FILE_NAME);
        htmlReporter.config().setDocumentTitle("Test Summary Report: " + ProjectName);
        htmlReporter.config().setReportName("Test Summary Report: " + ProjectName);
        htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
        htmlReporter.config().setTheme(Theme.STANDARD);
       // htmlReporter.onScreenCaptureAdded();

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

    }

    private void buildTestNodes(IResultMap tests, Status status) {

        if (tests.size() > 0) {
            for (ITestResult result : tests.getAllResults()) {
                int count = result.getMethod().getTotalInvocationCount();

                if (count > 1) {

                    test = extent.createTest(result.getMethod().getMethodName() + "_Iteration_" + TestScenarios.itr++);

                } else {

                    test = extent.createTest(result.getMethod().getMethodName());
                }
                for (String group : result.getMethod().getGroups())
                    test.assignCategory(group);

                if (result.getThrowable() != null) {
                    test.log(status, result.getThrowable());
                } else {

                    test.log(status, "Test " + status.toString().toLowerCase() + "ed");

                }

                test.getModel().setStartTime(getTime(result.getStartMillis()));
                test.getModel().setEndTime(getTime(result.getEndMillis()));
            }
        }
    }

    private Date getTime(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return calendar.getTime();
    }
}

## Environment Details

<!-- If you're reporting a bug, include as many relevant details about the environment you experienced the bug in -->
* Extent report Version used: 3.1.3
* Operating System and version: Windows 10
* JDK Version: 1.8

## Screenshots

![image](https://user-images.githubusercontent.com/28932933/37079405-8cf5c78e-21f3-11e8-9e41-91d89b64a180.png)

<!-- If you have got some screenshots, feel free to attach with this issue. -->

<!-- And finally, kindly log ExtentX related issues on https://github.com/anshooarora/extentx/issues -->
dipesh17 commented 6 years ago

Hi seeramzan, When you create an instance of extent report in your init() method. You if add a timestamp it might resolve your issue.

ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(dir + ReportPath + "/" + ProjectName + 
getTime(result.getStartMillis()) + FILE_NAME);

This will create a new report every time when you call init() method.
I hope this might help.