extent-framework / extentreports-testng-adapter

TestNG Adapter for Extent Framework
http://extentreports.com/docs/versions/4/java/testng.html
Apache License 2.0
50 stars 14 forks source link

Start and End time for tests are not updating #1

Closed qamadhu closed 5 years ago

qamadhu commented 5 years ago

Hi Anshoo Arora, If you notice, the duration for individual test cases is updated correctly whereas the overall duration of whole test suite is not updating I tried with extentreports-testng-adapter- ExtentIReporterSuiteListenerAdapter listener as well as below code and it's giving me same result. Please guide me if anything mistake I did. timeupdate issue

public class ExtentIReporterSuiteListenerAdapter implements IReporter { private static final Calendar CALENDAR = Calendar.getInstance(); private ExtentReports extent; public void generateReport(List xmlSuites, List suites, String outputDirectory) {

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

            ITestContext context = r.getTestContext();
            ExtentTest suiteTest = extent.createTest(r.getTestContext().getCurrentXmlTest().getName());

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

        }
    }
    //To print the TestRunner Logs
    for (String s : Reporter.getOutput()) {
        extent.setTestRunnerOutput(s);

    }

    extent.flush();

}

private void buildTestNodes(ExtentTest suiteTest,IResultMap tests, Status status) {
    ExtentTest test;

    if (tests.size() > 0) {

        for (ITestResult result : tests.getAllResults()) {  

            test = suiteTest.createNode(result.getMethod().getMethodName(), result.getMethod().getDescription());       
            test.getModel().setDescription("Test Description: " + result.getMethod().getDescription()); 
            for (String group : result.getMethod().getGroups())
                test.assignCategory(group);

            if (result.getThrowable() != null){
                if (result.getThrowable() instanceof java.lang.AssertionError) {
                    test.log(status,  result.getThrowable().getMessage());
                    //Attach screenshot
                    try {
                        test.log(status,
                                "Exception/Assertion screenshot:"+test.addScreenCaptureFromPath(".\\screenshots\\"+result.getMethod().getXmlTest().getName()+result.getMethod().getMethodName()+".png","ExceptionScreenshot"));
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    if(result.getMethod().getTestClass().getAfterTestMethods().length>0) 
                        test.log(status,result.getMethod().getTestClass().getAfterTestMethods()[0].getDescription());

                }else{
                    //node.log(status, "Test " + status.toString().toLowerCase() + "ed");
                    test.log(status, "Test " + status.toString().toLowerCase() + "ed"+": Due to Webdriver exception. Please re-run again\n"
                            .concat("\n<a href='.\\index.html' target='_blank'><span class='label info'>DetailedException</span></a>"));
                }

            }         

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

        }
    }
}
private Date getTime(long millis) {

        CALENDAR.setTimeInMillis(millis);
        return CALENDAR.getTime();
    }

private void init(String dir) {

    ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(dir +  File.separator + "ExtentReport.html");

    htmlReporter.config().setDocumentTitle("Automation Report");
    htmlReporter.config().setReportName("Mobile-Automation -Summary Report");
    htmlReporter.config().enableTimeline(true);
    htmlReporter.config().setTheme(Theme.STANDARD);
    htmlReporter.setAnalysisStrategy(AnalysisStrategy.TEST);

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

}
anshooarora commented 5 years ago

@qamadhu This was resolved in extentreports-4.0.2. Let me bump the version here, but you can test by adding the latest extentreports version in your pom.xml and check once.

<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports-testng-adapter</artifactId>
    <version>1.0.2</version>
</dependency>
<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>4.0.5</version>
</dependency>
anshooarora commented 5 years ago

@qamadhu are you adding the code you've shared with me in your project? With this adapter, you only need to add the relevant listener to your base class; no extra settings required. See here.

Once you've done this, simply add a extent.properties file under src/test/resources with the required configuration paths and the service will automatically pick it so you do not need to add any extra code in your project besides the @Listener.

I have created a few examples here

https://github.com/extent-framework/examples/blob/master/extentreports-testng-adapter-example/src/test/java/com/aventstack/extentreports/adapter/testng/tests/SimpleAssertTests.java

Configuration for above: https://github.com/extent-framework/examples/tree/master/extentreports-testng-adapter-example/src/test/resources

Refer readme: https://github.com/extent-framework/extentreports-testng-adapter/blob/master/Readme.md

qamadhu commented 5 years ago

Hi Anshooarora, Thank you for the update. I used ExtentIReporterSuiteListenerAdapter at suite level and it was working fine but I couldn't able to solve below points in that

  1. Attach screenshot for failed test
  2. Add INFO into to test results ( E.g: description of @BeforeClass/@BeforeMethod etc) So I used above code. Please let me know is there any way to add those details using ExtentIReporterSuiteListenerAdapter

Thank you for helping

foursyth commented 5 years ago

Well, the IReporter runs after suite completion so having access to realtime events is not possible. You need ITestListener. See here for options:

http://extentreports.com/docs/versions/4/java/testng.html#listeners

qamadhu commented 5 years ago

Thank you, Anshooarora, still issue exists for time updating I have update the latest latest version in pom.xml PFA time update issue

foursyth commented 5 years ago

Is this a regression?

anshooarora commented 5 years ago

@foursyth yeah, but also a bug in the IReporter listeners.

anshooarora commented 5 years ago

@qamadhu Please check with extentreports-testng-adapter:1.0.3-SNAPSHOT.

<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports-testng-adapter</artifactId>
    <version>1.0.3-SNAPSHOT</version>
</dependency>
qamadhu commented 5 years ago

@anshooarora I'm not able to add the maven dependency with above version. It's giving me missing me error "Missing artifact com.aventstack:extentreports-testng-adapter:jar:1.0.3-SNAPSHOT" in pom.xml

I tried deleting the files in .m2 and updated the maven project, But it's not updating with new version Could you please let me know anything need to be updated

Thanks, extentdependency

anshooarora commented 5 years ago

You need to enable SNAPSHOTS, see here https://stackoverflow.com/questions/7713996/is-there-a-way-to-make-maven-download-snapshot-versions-automatically

qamadhu commented 5 years ago

Thank you