How to implement TestNG Listener using ITestListener in Selenium?

We have already discussed WebDriver Listeners. In that tutorial, we discussed all the fundamental concepts of the listeners. Just to remind you, Listeners listen to the events on starting of the tests, finishing the tests, when tests failed, passed or skipped, hence, if you implement listeners in your Selenium project then you do not need to write separate logs for all these basic operations. Listeners will automatically capture it as the log and send data to the console. Today we are going to discuss TestNG listener which is called as ITestListener.

TestNG is fully equipped with all the advanced features which any tester needs so the TestNG listener is one of them. Before we jump to the agenda of this tutorial, I would like to recommend you a tutorial on WebDriver Even Listener. You should read it as it will add another milestone to your knowledge bank.

What is TestNG Listener?

TestNG listener formally called as ITestListener, which is an interface in TestNG. A normal Java class implements ITestListener and overrides all the methods written inside it. Each method corresponds to an event of your Selenium Project.

ITestListener effectively helps in creating a new way of logging. We can implement log4j logging along with the implementation of ITestListener.

TestNG Listener ITestListener Banner

How to set up TestNG Listener through ITestListener?

At first, we need to implement ITestListener then we override the methods written inside it. Here are the methods which will be overridden.

We will discuss two techniques to call TestNG listener in the Selenium project. Here are them:

Technique# 1: Through @Listeners annotation

We add @Listeners annotation in the TestNG class in which we want logging through listeners. Hence, we add the annotation before the class definition.

Technique# 2: Through testng.xml file

If we want TestNG listener to be implemented to the entire Selenium project, then we need to define it in the testng.xml file. Here is the statement:

Steps to implement TestNG Listener using ITestListener

We saw an overview of the implementation of ITestListener. Let’s see the step by step implementation in the project.

Step# 1: Implement ITestListener in Java Class

Here is the implementation of ITestListener. We override the methods which are written inside it.


Step# 2: Implementation of Listener by using @Listeners annotation

Here we will keep testng.xml file normally implemented whereas we implement the listener in a specific class file by using @Listeners annotation.

testng.xml file

Alternative Step# 2: Implementation of Listener at project level through the testng.xml file

Here we remove @Listeners annotation and we define one general implementation of the listener in the testng.xml file. So here is the implementation.

testng.xml file

Console Output

Below screenshot is the console output. The same output will come in both the techniques.

TestNG Listener ITestListener

TestNG Listener ITestListener status

This is all about TestNG Listener using ITestListener. You can write your queries below and don’t forget to join our Facebook group for more updates on Selenium.

Join Inviul fb group


Leave a Reply