How to print logs by using ExtentReports listener in java?

Vaibhav_Sharma picture Vaibhav_Sharma · Jun 14, 2017 · Viewed 23.7k times · Source

Here I am using the listener for generating reports in HTML format but it is not printing the logs present in a test case.

Sample Test Cases

@Test
public void testRedirectAllControlScreen() throws Exception {

    reportLog("login using a valid IsoMetrix username and password.");
    HomePage homePage = loginPage.login("username", "password");

    reportLog("Go to All Control page");
    AllControlPage allControlPage = homePage.navigateToControlPage();

    reportLog("Verify All Control page");
    allControlPage.verifyAllControlPage();

}

Method Present in BaseClass

 public void reportLog(String message) {
    message = BREAK_LINE + message;
    logger.info("Message: " + message);
    Reporter.log(message);
}

ExtentReport Listener

 public class ExtentReporterNG implements IReporter {

private ExtentReports extent;

public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
    extent = new ExtentReports(outputDirectory + File.separator + "ExtentReport.html", true);

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

        for (ISuiteResult r : result.values()) {
            ITestContext context = r.getTestContext();
            buildTestNodes(context.getPassedTests(), LogStatus.PASS);
            buildTestNodes(context.getFailedTests(), LogStatus.FAIL);
            buildTestNodes(context.getSkippedTests(), LogStatus.SKIP);
        }
    }

    extent.flush();
    extent.close();
}

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

    if (tests.size() > 0) {
        for (ITestResult result : tests.getAllResults()) {
            test = extent.startTest(result.getMethod().getMethodName());
            test.assignAuthor("360Log");
            test.setStartedTime(getTime(result.getStartMillis()));
            test.setEndedTime(getTime(result.getEndMillis()));

            for (String group : result.getMethod().getGroups())
                test.assignCategory(group);
            int s = result.getStatus();
            if (result.getStatus() == 1) {
                test.log(status, "Test " + status.toString().toLowerCase() + "ed");
            } else {
                String screen = BaseTest.screen;
                test.log(status, "Test " + status.toString().toLowerCase() + "ed " + test.addScreenCapture(screen));

            }
            extent.endTest(test);
        }
    }
  }

}

PFA the screenshot. It doesn't print logs step wise.

Answer

Vaibhav_Sharma picture Vaibhav_Sharma · Jun 15, 2017

Without using listener I am able to achieve the same thing. I implemented the extent test and extent report in Baseclass.java as per people suggestion like this:

public static ExtentTest test;
public static ExtentReports extent;

@BeforeSuite
public void before() {
    extent = new ExtentReports("target\\surefire-reports\\ExtentReport.html", true);
}

@BeforeMethod
public void setUp(Method method) throws Exception {
            test = extent.startTest(method.getClass().getSimpleName(),method.getClass().getEnclosingMethod().getName());
    test.assignAuthor("Vaibhav");
//Rest code will be generic for browser initiliazion.

}

@AfterSuite
public void tearDownSuite() {
    // reporter.endReport();
    extent.flush();
    extent.close();
}

//Method for adding logs passed from test cases
 public void reportLog(String message) {    
    test.log(LogStatus.INFO, message);//For extentTest HTML report
    logger.info("Message: " + message);
    Reporter.log(message);

}

Sample Test Case

@Test
public void testRedirectAllControlScreen() throws Exception {

    reportLog("login using a valid IsoMetrix username and password.");
    HomePage homePage = loginPage.login("username", "password");

    reportLog("Go to All Control page");
    AllControlPage allControlPage = homePage.navigateToControlPage();

    reportLog("Verify All Control page");
    allControlPage.verifyAllControlPage();

}
  • ExtentReport view

ExtentReport view