Extent report logging while parallel testing

Hi As we discussed in the last post Parallel testing and Thread Safe driver for testNG, lets discuss about extent report log synchronization.

We can achieve this by invoking parallel threads for each of the test.  Lets consider we have one html extent report and we want to add several tests together.

We can do extent report as static and we can create tests in different threads,  and at the end we can flush each thread to report. Lets see below code for extReport and extTest classes which does the same.

public class ExtReport {
public static Calendar cal = Calendar.getInstance(TimeZone .getTimeZone("GMT")); 
public static long time = cal.getTimeInMillis();   
    public synchronized static ExtentReports getReport() 
    {          
      ExtentReports Report = ( new ExtentReports(System.getProperty("user.dir")    + "/test-output/ExtentReport_" + time + ".html", true));    
      Report.addSystemInfo("Host Name", "TestMachine") 
         .addSystemInfo("Environment", "Automation Testing")
         .addSystemInfo("User Name", "Tester");
      Report.loadConfig(new File(System.getProperty("user.dir")    + "\\extent-config.xml"));     
      return Report;   
    }                
 }

In the above class we are generating static report and have one method to get the report.

This method, will return the static report object. Lets look at creating test and adding it to the same report.

public class ExtTest 
{
public class ExtTest
{
   private static ExtentReports extent = ExtReport.getReport(); 
   public static ThreadLocal<ExtentTest>  extentTestThreadSafe = new ThreadLocal<ExtentTest>(); 
   public static synchronized ExtentTest getTest() 
{ 
  return extentTestThreadSafe.get(); 
}
public static void setTest(ExtentTest tst) 
{ 
   extentTestThreadSafe.set(tst); 
}

In the above class we are creating methods to set and get thread safe version of ExtentTest and use the same test in logging all the actions. Lets look at below code how we can make use of above two classes and do the reporting.

We initiate the logger (extent test) from test method and use setTest and getTest methods to set and access the started ExtentTest.

//this is to get the extent report created and declared in test class
public ExtentReports extent = ExtReport.getReport();
@Test 
public void login_Test_Case_01() 
{ 
try 
{ 
 ExtentTest test = extent.startTest(SheetName); 
 st = new start(); 
 ExtTest.setTest(test); 
//your testing code here you can pass the test to other page classes etc.
 } catch (Exception e) 
{ 
  ExtTest.getTest().log(LogStatus.FAIL, "unexpected error " + e.getStackTrace().toString()); e.printStackTrace(); 
} finally
{ 
extent.endTest(test);
 extent.flush(); 
} 
}

This way tests that are running parallel can write in parallel to the Extent-report.

Here also, there is one challenge that these tests can flush to report when ever they are done/complete/fail. so the final report might not contain tests in any particular order., but all the steps in each test will not jumble up and you see report steps for each test will be listed in the same test. Below is the example report running tests in parallel.

Please find the whole code in below git repo.

https://github.com/SaiAuditya/Selenium_POM.git

11 thoughts on “Extent report logging while parallel testing

  1. Thanks a lot Sai Auditya for your post. I am having issues with the report when trying with multiple classes parallely. Could you share the git repo link if possible.

    Thanks in advance.

    Like

  2. I have handled all the threads in a thread safe synchronise manner. However when parallel thread are run, they create a single report containing all the @test methods (have used extent.createtest() inside all @test methods), however the test steps are missing and not all the steps are logged in many tests. Please help me out.

    Like

  3. Thanks a lot, Auditya for the solution, But while executing in serial the testcase start time and end time is appearing for an only test case in report, and while running in parallel start time and end time is not appearing for any of the tescases, is there any workaround, bcoz I found the end time is not displaying in the report screenshot you’ve shared too.

    Like

Leave a comment