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 ExtentReports package under 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!

After test finished, you will see ExtentReportsResults.html file under 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:

GitHub Link of This Project: https://github.com/swtestacademy/ExtentReportsExample

Also, you can check How to do Test Reporting with Allure and TestNG.

Link: Allure Test Reporting Framework Integration with TestNG 

Also, you can check How to do Test Reporting with Extent Reports Version 3.

Link: Extent Reports Version 3 Reporting with TestNG Listener

Also, you can check How to do Historical Test Reporting with Klov  – ExtentReports.

LinkKlov – ExtentReports | Test Automation Reporting

Selenium Webdriver Tutorial Series

See you in next article!