TestNG Listeners – Selenium WebDriver | Selenium Tutorial

In this post, we see TestNG listeners. Listeners “listen” to the event defined in the selenium script and behave accordingly. The main purpose of using listeners is to create logs. There are many types of listeners such as WebDriver Listeners and TestNG Listeners.

Here in this post, we see TestNG Listeners. Using TestNG listeners we could generate logs and customize TestNG Reports.

Let’s see how to implement TestNG Listeners.

Step 1: Create a Class “ListenerTestNG” to implement ITestListener methods

Mouse hover on ITestListener and import ITestListener Interface and mouse hover on ListenerTestNG and click on “Add unimplemented methods” to add multiple unimplemented methods (without body)

Here, I am implementing onTestSuccess, onTestSkipped, onTestFailure methods.

Step 2: Create another Class “ListenerTestNGTestCase” and write a script (which ever script you prefer). Else copy paste the below mentioned script.

Step 3: Add the listeners annotation (@Listeners) in the Class “ListenerTestNGTestCase

The complete “ListenerTestNGTestCase” class after adding Listener annotation is mentioned below:

Step 4: Execute the “ListenerTestNGTestCase” class. Methods in class “ListenerTestNG” are called automatically according to the behavior of methods annotated as @Test.

Step 5: Verify the Output in the console. You could find the logs in the console.

If you want to use listeners in multiple classes.

Add the below lines of code in the TestNG.xml file

Must Read: TestNG Tutorial

Final testng.xml file will be like this:
Execute it by right clicking on testng.xml and run as TestNG Suite

If you are not regular reader of SoftwareTestingMaterial.com then I highly recommend you to signup for the free email newsletter using the below link.

Sharing is caring.

Share on facebook
Share on twitter
Share on linkedin

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar SM

2 thoughts on “TestNG Listeners – Selenium WebDriver | Selenium Tutorial”

Leave a Comment

Your email address will not be published. Required fields are marked *

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on whatsapp

Recent Posts:

Scroll to Top
API Testing eBook