Whenever we write a piece of code or some tests for events on our webpage, we expect them to work according to the events triggered by the user. So we need some code or statement that observes and listens to all the events and executes them. But do we have such statements to handle the events for us? Yes, we have listeners who listen to all the events and execute them. Let’s discuss TestNG Listeners and how to use them in your tests in this article.

What is TestNG Listeners?
TestNG Listeners are the annotations provided by TestNG to listen to every event on the application and execute them if the event matches with the event we want to execute or want the listener to listen. It executes the code for events and modifies the default behavior of the TestNG events. The listeners are activated either before the test case or after it. Whenever a test case fails in Selenium, the listeners redirect us to the new behaviour written for the tests. The listeners are implemented using the ITestListener interface. Let’s learn about the ITestListener interface now.
ITestListener interface
ITestListener is the most used TestNG Listener with Selenium webDriver. We implement the ITestListener because it is an interface, and the class in which we implement this interface will override all the methods in it. The ITestListener has the following methods in it.
- onStart(): this method is invoked after any test class is instantiated and before executing any testNG method.
- onTestSuccess(): this method is invoked on the successful execution of any test.
- onTestFailure(): this method is invoked when a test fails to execute.
- onTestSkipped(): this method is invoked when we skip a test.
- onTestFailedButWithinSuccessPercentage(): this method is invoked whenever a method fails to execute but within the defined success percentage.
- onFinish(): this method is invoked on the execution of all the test cases of a class.
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(com.javatpoint.Listener.class)
public class TestNGClass {
@Test
public void addition() {
int sum = 0;
int num1 = 3;
int num2 = 3;
sum = num1 + num2;
System.out.println(sum);
}
@Test
public void fail() {
System.out.println("Test case has been failed");
Assert.assertTrue(false);
}
}
We implemented two methods; addition() and fail() in the above class “TestNGClass”. The method addition() has three variables sum, num1, and num2 initialized with 0, 3, and 3, respectively. Now let’s create a code for TestNG Listener.
import org.testng.ITestListener;
import org.testng.ITestResult;
public class TestNGListener implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
}
@Override
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("Success of test cases and its details are : "+result.getName());
}
@Override
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("Failure of test cases and its details are : "+result.getName());
}
@Override
public void onTestSkipped(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("Skip of test cases and its details are : "+result.getName());
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("Failure of test cases and its details are : "+result.getName());
}
@Override
public void onStart(ITestContext context) {
// TODO Auto-generated method stub
}
@Override
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
}
}
We created a TestNG Listener in the above code. The test cases in the code created above will be executed, and the methods of ITestListener will get executed accordingly when we run this TestNG Listener with our XML code.