Hi all, in this article I will describe how to integrate ExtentReports reporting tool with TestNG Listeners. I described how to use TestNG Listeners in this article and Canberk Akduygu and I also described how to rerun failed tests with IRetryAnalyzer in this article. Here, I will use TestNG listeners and IRetryAnalyzer interface and add ExtentReports classes to generate very beautiful reports after each test run. At the end of this article, I will share this project GitHub link as well. 😉

If you do not know Page Object Model, before starting this article, I suggest you check my POM with JAVA article. In our Page Object Model example, we create two login tests for n11.com website. Now, I will add ExtentReports and Listener packages under test package as shown below.

Let’s start step by step! 🙂

Page Classes

BasePage Class:

HomePage Class:

LoginPage Class:

Step-1: Add ExtentReports Maven Dependency

You should add ExtentReports dependency to your pom.xml. (I used version 2.41.2! It is stable.) This is ExtentReports version 2. There is also version 3 and 4.

Step-2: Add ExtentReports Classes 

I created the ExtentReports package under the utils package and add below two classes in that package.

ExtentManager Class:

In this class, we created an ExtentReports object and it can be reachable via getReporter() method. Also, you need to set your ExtentReports report HTML file location.

ExtentTestManager Class:

  • An extentTestMap map is created. It holds the information of thread ids and ExtentTest instances.
  • ExtentReports instance is created by calling getReporter() method from ExtentManager.
  • At startTest() method, an instance of ExtentTest created and put into extentTestMap with current thread id.
  • At endTest() method, test ends and ExtentTest instance got from extentTestMap via current thread id.
  • At getTest() method, return ExtentTest instance in extentTestMap by using current thread id.

Step-3: Add Listener Classes

For retry failed tests, we should add AnnotationTransformerRetry classes In order to listen to test events such as passed, failed, skipped, etc. we should add TestListener class in our project.

TestListener Class:

I added ExtentReports codes in each method and add some informative comments.

AnnotationTransformer Class:

Retry Class:

Step-4: Add Description For Tests (Optional)

You can add test descriptions in your tests and you will see them in the report as shown below.

Step-5: Add Listeners to TestNG.xml

We should add listeners in TestNG.xml as shown below.

Step-6: BaseTest Class

Step-7: Test Class

And run the test! Please right-click to TestNG.xml file and run the tests here by clicking the green Run text.

After the test finished, you will see ExtentReportsResults.html file under the ExtentReports folder as shown below.

When you open the report, you will see the test results as shown below.

Detailed Test Results:

General Test Results: