---Advertisement---

TestNG Listeners in Selenium: Complete Guide with Examples Great 2025

By Manisha

Updated On:

---Advertisement---
TestNG Listeners in Selenium

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


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


TestNG Listeners in Selenium: TestNG provides various listeners to modify test behavior. Below are some important ones:

ListenerPurpose
IAnnotationTransformerModifies test annotation behavior at runtime
IConfigurableConfigures test execution behavior dynamically
IExecutionListenerListens to execution start and finish events
IHookableAllows modifying test case execution
IMethodInterceptorAlters the test method execution order
IReporterCustomizes TestNG reports
ISuiteListenerListens to test suite execution
ITestListenerLogs test status (Pass/Fail/Skip)

🔹 In this tutorial, we will focus on ITestListener as it is the most commonly used listener.


TestNG Listeners in Selenium: The ITestListener interface provides several methods to track test execution.

Methods in ITestListener

MethodDescription
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 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.


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


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).


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.


✔ 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.


🔹 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.

TestNG

Testing Selenium Download

Leave a Comment

Index