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

Advertisement

Parallel testing and Thread Safe driver for testNG

In the recent times cicd is being the buzz word. Keeping this in mind the automation testing has been evolved to support this very need. Since after the build automation test suite need to be executed, the tests run time being the critical for whole build time. Most often because the tests run longer only prioritized tests will run in build generation cycle.

In this context its useful to know about parallel testing to reduce the turn around time. Let’s first look at parallel testing in testNg and selenium.

TestNg gives option to run parallel tests in threads. We can define how many threads we want to kick off simultaneously.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods" thread-count="2">
 <test name="Test" thread-count="3">
 <classes>
 <class name="com.tests.testcases"/>
 </classes>
 </test> <!-- Test -->
</suite> <!-- Suite -->

<suite name=”Suite” parallel=”methods” thread-count=”2″>

At Suite level the attribute parallel=”methods” added to run tests simultaneously.

The highlighted thread-count attribute determines how many threads testNG framework wants to kick off, these are parallel process running with different thread ids.

<test name=”Test” thread-count=”3″>

At test level also we have thread-count attribute and it is set to 3 in above example.

This means, three tests can run in parallel provided if we have enough threads.

Although you give thread-count is 2 at suite level and 3 at test level only two tests will run simultaneously, the third test will run only when first thread is freed up.

So batches of 2 tests will run until all tests completes.

The most common issue here being creating driver object which is thread-safe

And the other issue is being managing logging, as several threads trying to write to the same report. At the end we want single report for all tests.

Let us look at above mentioned two concepts. In java programming we can create driver which is thread safe with given command.

protected ThreadLocal<WebDriver> wbdriver = new ThreadLocal<WebDriver>();

ThreadLocal class in java is helpful in creating objects which are thread safe. It has get and set methods.  Following is its implementation

//chrome driver (options describe the desired capabilities for chrome driver.)
//you can just use string url instead of getting from properties file
String url = "https://google.com";
wbdriver.set(new ChromeDriver(options));
wbdriver.get().manage().window().maximize(); 
wbdriver.get().get(url);

in the above example, wbdriver holds the driver created, it is protected by ThreadLocal class for thread safety.

This way if you create driver, each test will run by its own thread. The only challenge being, the results will be hard to interpret when lot of concurrent driver processes are doing job simultaneously.

Lets discuss about synchronizing the logs with ExtentReports in next posts.

Extent report logging while parallel testing