
👉Introduction
TestNG Listeners in Selenium: In Selenium automation testing, TestNG Listeners are used to modify the default behavior of TestNG and generate custom reports/logs. By implementing listeners, testers can track test execution, log success/failure statuses, and customize test reports.
This tutorial will cover:
✅ What are TestNG Listeners?
✅ Different types of listeners in TestNG
✅ Implementing ITestListener with a real-world example
✅ Generating logs & reports using TestNG Listeners
👉What Are Listeners in TestNG?
TestNG Listeners in Selenium: Listeners in TestNG are interfaces that help modify default TestNG behavior. They “listen” to specific events in test execution and trigger predefined actions.
Why use Listeners?
🔹 Generate detailed logs about test execution
🔹 Customize TestNG reports based on test status
🔹 Execute actions before/after tests (e.g., taking screenshots on failure)
🔹 Handle skipped/failing tests automatically
👉Types of TestNG Listeners
TestNG Listeners in Selenium: TestNG provides various listeners to modify test behavior. Below are some important ones:
Listener | Purpose |
IAnnotationTransformer | Modifies test annotation behavior at runtime |
IConfigurable | Configures test execution behavior dynamically |
IExecutionListener | Listens to execution start and finish events |
IHookable | Allows modifying test case execution |
IMethodInterceptor | Alters the test method execution order |
IReporter | Customizes TestNG reports |
ISuiteListener | Listens to test suite execution |
ITestListener | Logs test status (Pass/Fail/Skip) |
🔹 In this tutorial, we will focus on ITestListener as it is the most commonly used listener.
👉ITestListener Interface in TestNG
TestNG Listeners in Selenium: The ITestListener interface provides several methods to track test execution.
Methods in ITestListener
Method | Description |
onStart() | Executes when a test starts |
onTestSuccess() | Executes when a test case passes |
onTestFailure() | Executes when a test case fails |
onTestSkipped() | Executes when a test is skipped |
onTestFailedButWithinSuccessPercentage() | Executes when a test fails but within an acceptable limit |
onFinish() | Executes when all tests are completed |
👉Step-by-Step Implementation of ITestListener in Selenium
Step 1: Create a TestNG Listener Class (ListenerTest.java)
We will create a listener class that implements ITestListener.
ListenerTest.java (Tracking Test Execution Logs)
java
package com.listeners;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class ListenerTest implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
System.out.println(“Test Started: ” + result.getName());
}
@Override
public void onTestSuccess(ITestResult result) {
System.out.println(“Test Passed: ” + result.getName());
}
@Override
public void onTestFailure(ITestResult result) {
System.out.println(“Test Failed: ” + result.getName());
}
@Override
public void onTestSkipped(ITestResult result) {
System.out.println(“Test Skipped: ” + result.getName());
}
@Override
public void onFinish(ITestResult result) {
System.out.println(“All Tests Finished.”);
}
}
This listener logs when a test starts, passes, fails, or gets skipped.
👉Step 2: Create a Test Class (TestCases.java)
TestNG Listeners in Selenium: We will create a Selenium test class that logs into a sample website.
TestCases.java (Selenium Test)
java
package com.testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(com.listeners.ListenerTest.class) // Attaching Listener
public class TestCases {
WebDriver driver;
@Test
public void loginTest() {
driver = new ChromeDriver();
driver.get(“https://example.com/login”);
System.out.println(“Login Test Executed”);
driver.quit();
}
@Test
public void invalidLoginTest() {
driver = new ChromeDriver();
driver.get(“https://example.com/login”);
System.out.println(“Invalid Login Test Executed”);
driver.quit();
}
}
Key Features:
✔ Uses @Listeners annotation to attach ListenerTest.java
✔ Opens a login page and executes test cases
✔ Generates logs on test execution
👉Step 3: Running the Test Cases
TestNG Listeners in Selenium: Execute TestCases.java in Eclipse → Right-click → Run As → TestNG Test
Expected Console Output:
yaml
Test Started: loginTest
Login Test Executed
Test Passed: loginTest
Test Started: invalidLoginTest
Invalid Login Test Executed
Test Passed: invalidLoginTest
All Tests Finished.
✅ Listener logs execution details (Start, Pass, Finish).
👉Using Listeners for Multiple Test Classes
If a project has multiple test classes, adding @Listeners to each one is tedious.
Solution: Use testng.xml to Apply Listeners Globally
testng.xml (Global Listener Implementation)
xml
<suite name=”Test Suite”>
<listeners>
<listener class-name=”com.listeners.ListenerTest”/>
</listeners>
<test name=”Login Test”>
<classes>
<class name=”com.testcases.TestCases”/>
</classes>
</test>
</suite>
✅ Now, all test classes in testng.xml will use ListenerTest without adding @Listeners manually.
👉Key Benefits of Using TestNG Listeners in Selenium
✔ Automatic Test Logging – Listeners track all test activities in real-time.
✔ Custom Reporting – Modify reports based on test success/failure.
✔ Failure Handling – Take screenshots or log errors automatically.
✔ Reduces Manual Effort – No need to write extra logging code in every test.
👉Conclusion
🔹 TestNG Listeners modify test execution behavior and help generate logs/reports.
🔹 ITestListener is widely used to track test execution (Pass, Fail, Skip).
🔹 Listeners can be applied at the class level (@Listeners) or globally (testng.xml).
🔹 They enhance test logging, debugging, and reporting in Selenium automation.
Start using TestNG Listeners today to make your test execution more efficient and trackable!
Description: Learn about TestNG Listeners in Selenium, including ITestListener & ITestResult. See how to generate logs, customize TestNG reports, and implement listeners with real-world examples.