Reporting with ExtentReports and TestNG Listeners


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.

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! 🙂

Step-1: Add ExtentReports Maven Dependency

You should add ExtentReports dependency to your pom.xml. (I used version 2.41.2! It is stable.)

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 AnnotationTransformer, Retry 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.

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 test results as shown below.

Detailed Test Results:

General Test Results:

GitHub Project: https://github.com/swtestacademy/ExtentReportsExample

Also, you can check Allure Test Reporting Framework Integration with TestNG in this article.

See you in next article!
-Onur

javafx

Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! http://www.swtestacademy.com/getting-started-with-javafx/ At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
selenium

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 5 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...
selenium

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...
javafx

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...
selenium

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across the wide range of browsers and platforms. In this tutorial, you will learn how to do web test automation with Selenium Webdriver and the related tools....
selenium

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...
selenium

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...

Getting Started with RobotFramework on Windows

By Onur Baskirt / Apr 25, 2016 / 12 Comments
What is RobotFramework? RobotFramework is a GENERIC test automation framework for acceptance testing and acceptance test-driven development (ATTD). What it means that you can do web, mobile, desktop and other test automation activities with related test libraries. These libraries can...
By | 2017-10-22T02:09:10+00:00 October 9th, 2017|Selenium, Test Automation, Test Frameworks, TestNG|13 Comments

About the Author:

Onur Baskirt
Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as research assistant, design verification engineer and software test leader. Also, he worked as software test leader and software operations manager at Turkey's biggest technology retailer, Teknosa. Now, he is working as Head of Software Testing and Manager of two Development Teams at Kariyer.net. His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.

13 Comments

  1. Sudheer Kumar Balivada October 10, 2017 at 10:49 am - Reply

    This is really a good article. I follow all your selenium and automation testing articles and of course helped me in creating a framework at work.

    Could you please create a similar article using Allure reporting. It really helps me.

    Thanks in advance.

  2. Rizwan Jafri October 10, 2017 at 12:55 pm - Reply

    Wonderful

  3. Alberto October 11, 2017 at 3:26 pm - Reply

    hi,
    First, i’m sorry for my english.
    Second, i tell you that it is the best report for testng that i know, and i congratulate you for it. congratulations.
    but, i have a problem. i can´t watch the feature name and the step name. how can i watch it?
    Thank you

  4. Prasad October 12, 2017 at 10:19 am - Reply

    How to invoke ExtentTestManager.getTest() method in test classes and page classes?

    I am getting “Null Pointer Exception” while calling getTest() method. Can you please help me out with this?

    Thanks In Advance!

    • Onur Baskirt
      Onur Baskirt October 13, 2017 at 12:44 am - Reply

      ExtentTestManager class has public synchronized static methods. Thus, when you import “import utils.ExtentReports.ExtentTestManager;” line, then you can reach ExtentTestManager.endTest(); or ExtentTestManager.getTest();
      Please, check TestListener class. I called those methods several times. If you get null pointer, first you should start test with ExtentTestManager.startTest(String testName, String desc), then you can get that test by ExtentTestManager.getTest()

Leave A Comment