---
description: In TestNG, there are several listeners that act as interfaces to modify the default TestNG&#039;s behaviors. As the name suggests Listeners &quot;listen&quot; to the event defined in the selenium script and behave accordingly. It allows customizing TestNG reports or log
title: TestNG Listeners in Selenium
image: https://www.guru99.com/images/testng-listeners-in-selenium.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Listeners in Selenium WebDriver are TestNG interfaces that intercept test events to customize logs, reports, and post-failure actions. This article explains ITestListener methods, walks through a runnable Java example, and clarifies how Selenium 4 replaced the deprecated WebDriverEventListener.

* ❓ **What Listeners Do:** They subscribe to TestNG events like start, pass, fail, and skip to drive logging or reporting hooks.
* 🧩 **Twelve Interfaces:** TestNG ships interfaces such as ITestListener, ISuiteListener, IReporter, IInvokedMethodListener, and IAnnotationTransformer for granular control.
* 🛠️ **Two Wiring Modes:** Attach a listener with the @Listeners annotation on a single class, or register it once inside testng.xml for every suite.
* 🚀 **Selenium 4 Update:** EventFiringWebDriver is deprecated; the modern WebDriverListener interface plus EventFiringDecorator now powers WebDriver-level event hooks.
* 🤖 **AI Angle:** AI-assisted listeners can auto-classify failures, attach smart screenshots, and feed flaky-test signals back to CI dashboards in real time.

[ Read More ](javascript:void%280%29;) 

![TestNG Listeners in Selenium](https://www.guru99.com/images/testng-listeners-in-selenium.png)

Selenium WebDriver scripts often need to react to test events such as a passed assertion, a failed locator, or a skipped step. Listeners make that possible. Broadly, Selenium projects rely on two listener families:

1. WebDriver Listeners
2. TestNG Listeners

In this tutorial, we will focus on[ TestNG ](https://www.guru99.com/all-about-testng-and-selenium.html)Listeners, with a note on how WebDriver-level listeners changed in Selenium 4.x.

## What is a Listener in TestNG?

A Listener is an interface that modifies the default behavior of TestNG. As the name suggests, Listeners “listen” to events defined in a Selenium script and react accordingly. You use them by implementing the relevant Listener interface and registering it on your test class or suite. Listeners allow you to customize TestNG reports, attach screenshots, and emit structured logs.

## Types of Listeners in TestNG

TestNG ships a family of listener interfaces, and each one targets a different stage of the test lifecycle.

Below are the commonly used TestNG listeners:

1. IAnnotationTransformer
2. IAnnotationTransformer2
3. IConfigurable
4. IConfigurationListener
5. IExecutionListener
6. IHookable
7. IInvokedMethodListener
8. IInvokedMethodListener2
9. IMethodInterceptor
10. IReporter
11. ISuiteListener
12. ITestListener

These interfaces are used in Selenium to generate logs or customize TestNG reports. In this tutorial, we will implement **ITestListener**.

**ITestListener** exposes the following methods:

* **onStart –** called when any test starts.
* **onTestSuccess –** called when a test passes.
* **onTestFailure –** called when a test fails.
* **onTestSkipped –** called when a test is skipped.
* **onTestFailedButWithinSuccessPercentage –** called when a test fails but is within the success percentage.
* **onFinish –** called after all tests in the class are executed.

### RELATED ARTICLES

* [Action Class in Selenium ](https://www.guru99.com/keyboard-mouse-events-files-webdriver.html "Action Class in Selenium")
* [Selenium Tutorial – Guru99 ](https://www.guru99.com/selenium-tutorial.html "Selenium Tutorial – Guru99")
* [Selenium with Cucumber (BDD Framework Tutorial) ](https://www.guru99.com/using-cucumber-selenium.html "Selenium with Cucumber (BDD Framework Tutorial)")
* [TestNG Reports Generation in Selenium: How to Generate? ](https://www.guru99.com/testng-report.html "TestNG Reports Generation in Selenium: How to Generate?")

## Test Scenario

In this test scenario, we will automate the login process and implement _ITestListener_ against it.

1. Launch Firefox and open the site <https://demo.guru99.com/V4/>

[](https://www.guru99.com/images/c-sharp-net/052716%5F0822%5FListenersan2.png)

1. Log in to the application.

[](https://www.guru99.com/images/c-sharp-net/052716%5F0822%5FListenersan3.png)

## Steps to Create a TestNG Listener

For the test scenario above, we will implement the Listener step by step.

**Step 1)** Create a class called _ListenerTest_ that implements _ITestListener_. Hover over the red underline and Eclipse will suggest two quick fixes, as shown below:

[](https://www.guru99.com/images/c-sharp-net/052716%5F0822%5FListenersan4.png)

Click “Add unimplemented methods”. Multiple stub methods (without a body) are added to your code, similar to this:

package Listener\_Demo; 

import org.testng.ITestContext;  
import org.testng.ITestListener;  
import org.testng.ITestResult;

public class ListenerTest implements ITestListener {

 @Override  
public void onFinish(ITestContext arg0) {  
// TODO Auto-generated method stub  
}

 @Override  
public void onStart(ITestContext arg0) {  
// TODO Auto-generated method stub  
}

 @Override  
public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {  
// TODO Auto-generated method stub  
}

 @Override  
public void onTestFailure(ITestResult arg0) {  
// TODO Auto-generated method stub  
}

 @Override  
public void onTestSkipped(ITestResult arg0) {  
// TODO Auto-generated method stub  
}

 @Override  
public void onTestStart(ITestResult arg0) {  
// TODO Auto-generated method stub  
}

 @Override  
public void onTestSuccess(ITestResult arg0) {  
// TODO Auto-generated method stub  
}  
} 

Now let us modify the _ListenerTest_ class. In particular, we will fill in the following methods: _onTestFailure_, _onTestSkipped_, _onTestStart_, and _onTestSuccess_.

The change is simple: each method prints the name of the test so the console clearly shows pass, fail, and skip status.

After modification, the code looks like this:

package Listener\_Demo; 

import org.testng.ITestContext;  
import org.testng.ITestListener;  
import org.testng.ITestResult;

public class ListenerTest implements ITestListener {

 @Override  
public void onFinish(ITestContext Result) {  
}

 @Override  
public void onStart(ITestContext Result) {  
}

 @Override  
public void onTestFailedButWithinSuccessPercentage(ITestResult Result) {  
}

// When a test case fails, this method is called.  
@Override  
public void onTestFailure(ITestResult Result) {  
System.out.println(“The name of the testcase failed is : “ \+ Result.getName());  
}

// When a test case is skipped, this method is called.  
@Override  
public void onTestSkipped(ITestResult Result) {  
System.out.println(“The name of the testcase Skipped is : “ \+ Result.getName());  
}

// When a test case starts, this method is called.  
@Override  
public void onTestStart(ITestResult Result) {  
System.out.println(Result.getName() + ” test case started”);  
}

// When a test case passes, this method is called.  
@Override  
public void onTestSuccess(ITestResult Result) {  
System.out.println(“The name of the testcase passed is : “ \+ Result.getName());  
}  
} 

**Step 2)** Create another class called _TestCases_ for the login automation. Selenium will execute this class to log in to the demo site.

package Listener\_Demo; 

import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.firefox.FirefoxDriver;  
import org.testng.Assert;  
import org.testng.annotations.Listeners;  
import org.testng.annotations.Test;

public class TestCases {  
WebDriver driver = new FirefoxDriver();

// Test designed to pass, to verify the success listener.  
@Test  
public void Login() {  
driver.get(“https://demo.guru99.com/V4/”);  
driver.findElement(By.name(“uid”)).sendKeys(“mngr34926”);  
driver.findElement(By.name(“password”)).sendKeys(“amUpenu”);  
driver.findElement(By.name(“btnLogin”)).click();  
}

// Forcefully failed test, to verify the failure listener.  
@Test  
public void TestToFail() {  
System.out.println(“This method to test fail”);  
Assert.assertTrue(false);  
}  
} 

**Step 3)** Next, attach this listener to our test class _TestCases_. There are two ways to connect a class to a listener interface.

The first way is to use the _@Listeners_ annotation, as shown below:

@Listeners(Listener\_Demo.ListenerTest.class) 

We add this annotation above the _TestCases_ class. The class then looks like this:

package Listener\_Demo; 

import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.firefox.FirefoxDriver;  
import org.testng.Assert;  
import org.testng.annotations.Listeners;  
import org.testng.annotations.Test;

@Listeners(Listener\_Demo.ListenerTest.class)  
public class TestCases {  
WebDriver driver = new FirefoxDriver();

// Test to pass, to verify the success listener.  
@Test  
public void Login() {  
driver.get(“https://demo.guru99.com/V4/”);  
driver.findElement(By.name(“uid”)).sendKeys(“mngr34926”);  
driver.findElement(By.name(“password”)).sendKeys(“amUpenu”);  
driver.findElement(By.name(“btnLogin”)).click();  
}

// Forcefully failed test, to verify the failure listener.  
@Test  
public void TestToFail() {  
System.out.println(“This method to test fail”);  
Assert.assertTrue(false);  
}  
} 

The project structure looks like:

[](https://www.guru99.com/images/c-sharp-net/052716%5F0822%5FListenersan5.png)

**Step 4)** Execute the _TestCases_ class. Methods inside _ListenerTest_ are invoked automatically based on the behavior of methods annotated with _@Test_.

**Step 5)** Verify the output shown in the console.

The output of _TestCases_ looks like:

[](https://www.guru99.com/images/c-sharp-net/052716%5F0822%5FListenersan6.png)

\[TestNG\] Running:  
C:\\Users\\gauravn\\AppData\\Local\\Temp\\testng-eclipse–1058076918\\testng-customsuite.xml 

Login test case started  
The name of the testcase passed is : Login  
TestToFail test case started  
This method to test fail  
The name of the testcase failed is : TestToFail  
PASSED: Login  
FAILED: TestToFail  
java.lang.AssertionError: expected \[true\] but found \[false\] 

## Using a Listener for Multiple Classes

If a project has many test classes, adding the _@Listeners_ annotation to each one becomes cumbersome and error prone.

In that case, create a _testng.xml_ file and register the listener there once.

[](https://www.guru99.com/images/c-sharp-net/052716%5F0822%5FListenersan7.png)

This listener is then applied across the entire test suite regardless of the number of classes. When you run the XML file, the listener fires for every class declared in the suite, and you can chain any number of listener classes inside the same tag.

## WebDriverListener in Selenium 4 vs the Deprecated EventFiringWebDriver

While TestNG listeners react to test lifecycle events, WebDriver listeners react to browser-driver actions such as a click, a navigation, or a findElement call. In Selenium 3 the typical approach was the _WebDriverEventListener_ interface wired through _EventFiringWebDriver_. Both are deprecated in Selenium 4.x.

The modern replacement is the _WebDriverListener_ interface combined with _EventFiringDecorator_:

import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  
import org.openqa.selenium.support.events.EventFiringDecorator;  
import org.openqa.selenium.support.events.WebDriverListener; 

public class LoggingListener implements WebDriverListener {  
@Override  
public void beforeGet(WebDriver driver, String url) {  
System.out.println(“Navigating to “ \+ url);  
}  
}

WebDriver raw = new ChromeDriver();  
WebDriver driver = new EventFiringDecorator<>(new LoggingListener()).decorate(raw); 

The decorator can wrap any WebDriver, WebElement, or Alert, which is more flexible than the old event-firing wrapper. Use _WebDriverListener_ for browser-side observability and TestNG _ITestListener_ for suite-level reporting.

## AI-Powered Listeners: Smarter Logs and Failure Triage

Modern Selenium teams increasingly plug AI services into their listeners to make CI signals more actionable. Inside _onTestFailure_, an AI-assisted listener can capture a DOM snapshot plus a screenshot, send them to a model that returns a likely root-cause cluster, and write a tag back into the TestNG report or a tool like ReportPortal.

Common AI-driven patterns include:

* **Flaky-test detection:** Listeners forward pass/fail timelines to a model that classifies a failure as flaky, environmental, or a real regression.
* **Smart screenshots:** Computer-vision models crop, annotate, and diff UI screenshots so reviewers see the changed region instead of a full page dump.
* **Self-healing locators:** A _WebDriverListener_ hooks _beforeFindElement_ and asks an AI helper to suggest an alternate locator when the primary one throws _NoSuchElementException_.
* **Natural-language summaries:** An _IReporter_ implementation feeds suite results into an LLM that produces a one-paragraph stand-up summary.

The listener layer is the cleanest place to inject these hooks because it stays out of the test logic and applies uniformly across the suite.

## Summary

Listeners are required to generate logs or customize TestNG reports in Selenium WebDriver.

* TestNG offers many listener interfaces; pick the one that matches the event you care about.
* Listeners are interfaces used in Selenium WebDriver scripts to react to test lifecycle events.
* The tutorial demonstrated _ITestListener_ with a passing and a failing test.
* You can attach a listener with _@Listeners_ or register it once in _testng.xml_ for the whole suite.
* Selenium 4.x replaces _EventFiringWebDriver_ with _WebDriverListener_ \+ _EventFiringDecorator_ for WebDriver-level events.

## FAQs

🎧 What is a Listener in Selenium WebDriver?

A Listener is an interface that subscribes to events generated during a Selenium test run. TestNG listeners react to test lifecycle events such as start, pass, fail, and skip. WebDriver listeners react to browser-driver events such as clicks, navigations, and findElement calls.

🧩 What is the difference between ITestListener and ISuiteListener?

ITestListener fires for individual @Test methods and exposes onTestStart, onTestSuccess, onTestFailure, onTestSkipped, and onFinish. ISuiteListener fires only twice per suite, with onStart and onFinish, making it ideal for suite-level setup such as opening a report file.

🛠️ How do I register a TestNG listener?

You can register a TestNG listener in two ways: add the @Listeners(MyListener.class) annotation above the test class, or declare a <listeners> tag inside testng.xml. The XML approach applies the listener across every class in the suite without modifying source code.

🚀 Is EventFiringWebDriver still supported in Selenium 4?

EventFiringWebDriver and WebDriverEventListener are deprecated in Selenium 4.x. The recommended replacement is the WebDriverListener interface combined with EventFiringDecorator, which can wrap WebDriver, WebElement, or Alert instances and offers cleaner hook points such as beforeGet and afterClick.

📋 When should I use IReporter or IInvokedMethodListener?

Use IReporter when you want to produce a custom report after the suite finishes, such as an HTML or JSON summary. Use IInvokedMethodListener when you need a hook before and after every test method, including configuration methods like @BeforeMethod and @AfterMethod.

🤖 How can AI improve Selenium event logging?

AI models can enrich listener output by tagging each log entry with severity, generating natural-language step descriptions, and clustering related failures. Inside onTestFailure, an AI service can analyze the screenshot and stack trace, then attach a likely root cause to the TestNG report.

🧠 Can AI-driven listeners triage flaky tests automatically?

Yes. A listener can stream pass/fail history to an AI service that classifies a failure as flaky, environmental, or a real regression. The verdict is written back as a TestNG attribute, so dashboards can quarantine flaky tests without a human reviewing every red build.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/testng-listeners-in-selenium.png","url":"https://www.guru99.com/images/testng-listeners-in-selenium.png","width":"700","height":"250","caption":"TestNG Listeners in Selenium","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/listeners-selenium-webdriver.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/selenium","name":"Selenium"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/listeners-selenium-webdriver.html","name":"TestNG Listeners in Selenium"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/listeners-selenium-webdriver.html#webpage","url":"https://www.guru99.com/listeners-selenium-webdriver.html","name":"TestNG Listeners in Selenium","dateModified":"2026-06-17T13:08:06+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/testng-listeners-in-selenium.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/listeners-selenium-webdriver.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta","url":"https://www.guru99.com/author/admin","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","url":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","caption":"Krishna Rungta","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Selenium","headline":"TestNG Listeners in Selenium","description":"In TestNG, there are several listeners that act as interfaces to modify the default TestNG&#039;s behaviors. As the name suggests Listeners &quot;listen&quot; to the event defined in the selenium script and behave accordingly. It allows customizing TestNG reports or log","keywords":"selenium","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta"},"dateModified":"2026-06-17T13:08:06+05:30","image":{"@id":"https://www.guru99.com/images/testng-listeners-in-selenium.png"},"copyrightYear":"2026","name":"TestNG Listeners in Selenium","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is a Listener in Selenium WebDriver?","acceptedAnswer":{"@type":"Answer","text":"A Listener is an interface that subscribes to events generated during a Selenium test run. TestNG listeners react to test lifecycle events such as start, pass, fail, and skip. WebDriver listeners react to browser-driver events such as clicks, navigations, and findElement calls."}},{"@type":"Question","name":"What is the difference between ITestListener and ISuiteListener?","acceptedAnswer":{"@type":"Answer","text":"ITestListener fires for individual @Test methods and exposes onTestStart, onTestSuccess, onTestFailure, onTestSkipped, and onFinish. ISuiteListener fires only twice per suite, with onStart and onFinish, making it ideal for suite-level setup such as opening a report file."}},{"@type":"Question","name":"How do I register a TestNG listener?","acceptedAnswer":{"@type":"Answer","text":"You can register a TestNG listener in two ways: add the @Listeners(MyListener.class) annotation above the test class, or declare a  tag inside testng.xml. The XML approach applies the listener across every class in the suite without modifying source code."}},{"@type":"Question","name":"Is EventFiringWebDriver still supported in Selenium 4?","acceptedAnswer":{"@type":"Answer","text":"EventFiringWebDriver and WebDriverEventListener are deprecated in Selenium 4.x. The recommended replacement is the WebDriverListener interface combined with EventFiringDecorator, which can wrap WebDriver, WebElement, or Alert instances and offers cleaner hook points such as beforeGet and afterClick."}},{"@type":"Question","name":"When should I use IReporter or IInvokedMethodListener?","acceptedAnswer":{"@type":"Answer","text":"Use IReporter when you want to produce a custom report after the suite finishes, such as an HTML or JSON summary. Use IInvokedMethodListener when you need a hook before and after every test method, including configuration methods like @BeforeMethod and @AfterMethod."}},{"@type":"Question","name":"How can AI improve Selenium event logging?","acceptedAnswer":{"@type":"Answer","text":"AI models can enrich listener output by tagging each log entry with severity, generating natural-language step descriptions, and clustering related failures. Inside onTestFailure, an AI service can analyze the screenshot and stack trace, then attach a likely root cause to the TestNG report."}},{"@type":"Question","name":"Can AI-driven listeners triage flaky tests automatically?","acceptedAnswer":{"@type":"Answer","text":"Yes. A listener can stream pass/fail history to an AI service that classifies a failure as flaky, environmental, or a real regression. The verdict is written back as a TestNG attribute, so dashboards can quarantine flaky tests without a human reviewing every red build."}}]}],"@id":"https://www.guru99.com/listeners-selenium-webdriver.html#schema-1115702","isPartOf":{"@id":"https://www.guru99.com/listeners-selenium-webdriver.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/listeners-selenium-webdriver.html#webpage"}}]}
```
