Implementing Extent Reports with TestNG framework
Extent reports are detailed and interactive HTML reports that provide insights into the execution of our automated tests. We can include detailed information about test execution, such as test steps, status, logs, capture screenshots, and add different logging levels too. We can seamlessly integrate extent reports with popular testing frameworks like TestNG and JUnit.
Let's see how it can be integrated with TestNg.
First, create files ExtentFileReporter.java and Listeners.java in your Maven project. Add the ITestListener interface to the Listeners class.
In the ExtentFileReporter class, initialize a reference for ExtentReport and ExtentSparkReporter.
The ExtentReports report client for starting reporters and building reports. For most applications, you should have one ExtentReports instance for the entire JVM.
ExtentReports itself does not build any reports but allows reporters (in our case ExtentSparkReporter) to access information, which in turn builds the said reports.
An important point to note here is that it is mandatory to call the flush method to ensure information is written to the started reporters.
This snippet shows how an instance of ExtentReport is attached to the Extent Spark reporter to define the destination of the report and how we can add the layout, look, and info that is needed on our HTML report.
We have different config builder functions to choose from such as theme,documentTitle, reportName and timeStampFormat.
Next, we will need to import the ExtentTest class for defining a test. We can add logs, snapshots, and assign authors and categories to a test and its children with the help of this test instance.
ExtentTest test = extentReport.createTest(“testName”);
Extent Reports are designed to handle parallel test execution scenarios, providing a consolidated view of results even when tests are running concurrently. For this, we have used “synchronized” keyword for ExtentTest instances to avoid the system’s race condition.
Now for Listeners.java class add unimplemented methods from ITestListener interface.
Update the class methods to the following. The methods will be run according to the sequence and pass/fail conditions.
Run your test suite and see an HTML report will be created to the specified destination.
That was all it, easy peasy, lemon squeezy.