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.

TestNG Listeners

Sumasri is a Sr. Software Test Engineer. Currently working in an MNC. She is a co-founder of Software Testing Material. She has an exclusive experience in the field of Software Testing. She writes here about Manual Testing and Automation Testing.

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

  1. how to explain page object model framework in interview for 3 years experience.

    please give me the format.

Leave a Comment

Share via
Copy link
Powered by Social Snap