What are TestNG listeners in Selenium?

Key Takeaways:

  • TestNG listeners are essential components in Selenium for customizing test case behavior and extending the testing framework's functionality.

  • Common listener interfaces include ITestListener, ISuiteListener, IInvokedMethodListener, and IRetryAnalyzer, each providing methods to listen to specific events during test execution.

  • Listeners can be used for various purposes, such as logging test results, generating custom reports, handling test retries, and performing actions before or after test execution.

In Selenium, TestNG listeners are components that allow us to customize and extend the behavior of our test cases. TestNG is a testing framework for Java widely used in Selenium test automation. Listeners in TestNG are interfaces that you can implement to listen to different events during the test execution lifecycle. Some common events include test case start, test case failure, test case success, test case skip, etc.

Here, we’ll discuss some commonly used TestNG listeners in Selenium.

The ITestListener

This listener interface provides methods to listen to events related to test execution like onTestStart, onTestSuccess, onTestFailure, onTestSkipped, etc. We can use it to perform actions before or after each test method.

Code example

An example of how this listener is implemented in Selenium is given below.

import org.testng.ITestListener;
import org.testng.ITestResult;
public class CustomTestListener 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());
}
// Other methods can be overridden as well
}

The explanation of each method inside the public class CustomTestListener is given below:

  • The onTestStart method (Lines 6–9): This method is responsible for printing a message when a test starts.

  • The onTestSuccess method (Lines 11–14): This method prints a message when a test passes.

  • The onTestFailure method (Lines 16–19): This method prints a message when a test fails.

  • The onTestSkipped method (Lines 21–24): This method prints a message when a test is skipped.

The ISuiteListener

This listener interface provides methods to listen to events related to test suite execution like onStart, onFinish, etc. We can use it to perform actions before or after the entire suite execution.

Code example

An example of how the ISuiteListener listener is implemented in Selenium is given below.

import org.testng.ISuite;
import org.testng.ISuiteListener;
public class CustomSuiteListener implements ISuiteListener {
@Override
public void onStart(ISuite suite) {
System.out.println("Suite Started: " + suite.getName());
}
@Override
public void onFinish(ISuite suite) {
System.out.println("Suite Finished: " + suite.getName());
}
}

The explanation of each method inside the public class CustomSuiteListener is given below:

  • The onStart method (Lines 6-9): This method is responsible for printing a message when a suite starts.

  • The onFinish method (Lines 11-14): This method prints a message when a suit is finished.

The IInvokedMethodListener

This listener interface provides methods to listen to events related to individual test method invocation like beforeInvocation, afterInvocation, etc. We can use it to perform actions before or after each method invocation.

Code example

An example of how the IInvokedMethodListener listener is implemented in Selenium is given below.

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
public class CustomMethodListener implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
System.out.println("Before Invocation of method: " + method.getTestMethod().getMethodName());
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
System.out.println("After Invocation of method: " + method.getTestMethod().getMethodName());
}
}

The explanation of each method inside the public class CustomMethodListener is given below:

  • The beforeInvocation method (Lines 7–10): This method prints a message before a certain method is invoked

  • The afterInvocation method (Lines 12–15): This method is responsible for printing a message after a certain method is invoked

The IRetryAnalyzer

This listener interface provides a way to implement test case retry logic. We can implement this interface to control the retry behavior of failed test cases.

Code example

An example of how the IRetryAnalyzer listener is implemented in Selenium is given below.

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class CustomRetryAnalyzer implements IRetryAnalyzer {
private int retryCount = 0;
private static final int MAX_RETRY_COUNT = 3;
@Override
public boolean retry(ITestResult result) {
if (retryCount < MAX_RETRY_COUNT) {
retryCount++;
return true;
}
return false;
}
}

The line-by-line explanation of the code inside the CustomRetryAnalyzer is given below:

  • Lines 6–7: We declare variables for retry count (retryCount) and maximum retry count (i.e MAX_RETRY_COUNT)

  • Lines 9–16: We implement the retry method to control the retry behavior. This method returns true if the test should be retried, and false otherwise.

Conclusion

By implementing these listener interfaces, we can customize the behavior of our test cases, generate custom reports, handle retries, and perform various other actionsThese actions can include logging test start/end, capturing test results, generating custom reports, handling retries, and more. during test execution in Selenium with TestNG. TestNG listeners enhance the flexibility, maintainability, and scalability of Selenium test automation frameworks, allowing testers to adapt to changing requirements and ensure robust test execution.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What are the TestNG listeners?

TestNG listeners are components in the TestNG testing framework that allow you to customize and extend the behavior of your test cases.


What is the purpose of listeners in Selenium?

Listeners in Selenium provide a powerful way to customize and extend the behavior of your test cases, making your test automation framework more flexible, efficient, and reliable.


What is the difference between a TestNG listener and a WebDriver listener?

  • TestNG listeners provide a broader scope for customizing test behavior, focusing on the overall test execution life cycle.
  • WebDriver listeners provide a more granular level of customization, focusing on events related to the WebDriver object.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved