Generating Extent report during parallel testing on Suite level (Thread safe extent report)

Rajatshandilya
2 min readOct 5, 2020
Thread safe extent report during parallel testing

Reporting is one of the most important feature of any automation framework as it shows overall report of your test cases. Although, Selenium provides inbuilt HTML framework using framework like JUnit and TestNG but that report but they lack in customisation and in look and feel.

Extent reports are more customisable and can be integrated in your automation framework using selenium. Also, they’ve a much better UI and UX and can be shared with the stakeholders.

Check the article below on how to integrate extent report into framework.

https://www.automationinja.com/post/add-extent-report-4-in-selenium-webdriver-on-suite-level

Now let’s discuss on how to make these reports thread safe because if you are running multiple threads of same tests, chances are you first test object will be replaced or overridden by second test object and Extent Report only reports the active threads.

To prevent that form happening, we’ve to user ThreadLocal class in java We’ve discussed on ThreadLocal java class in parallel testing blog.

https://www.automationinja.com/post/thread-safe-parallel-testing-with-selenium

To implement this, we only have to make ExtentTest class object Thread safe and thats it.

Also, you can make ThreadLocal private and static so that it’s confined to only this class. So, Now when same tests are called in multiple threads at the same time, there objects won’t be overridden by each other.

On test start:

All tests object will be stored in the ThreadLocal class ref. variable and this will be implemented by passing the test object in “ .set() “ method.

On test finish:

We’ve to retrieve those test objects and to do that we’ll use “ .get() “ method.

Refer to below complete code snippet:

public class TestListner implements ITestListener
{
ExtentReports extent = ExtentReportGenerator.ExtentReport();
ExtentTest test;;
private static ThreadLocal<ExtentTest> extent_test = new ThreadLocal<ExtentTest>(); public void onTestStart(ITestResult result)
{
test = extent.createTest(result.getMethod().getMethodName());
extent_test.set(test);
}
public void onTestSkipped(ITestResult result)
{
extent_test.get().skip(result.getThrowable());
}
public void onTestSuccess(ITestResult result)
{
test.log(Status.PASS, "Successful");
}
public void onTestFailure(ITestResult result)
{
extent_test.get().fail(result.getThrowable());
}
public void onFinish(ITestContext context)
{
extent.flush();
}
}

Thanks

Cheers!! 🍻

--

--

Rajatshandilya

Automation Test Engineer / I love designing UI/UX & t-shirts / Blog at automationinja.com