TestNG provides the @Listeners annotation which heedfully auricularly discerns every event that occurs in a selenium code. Listeners are activated either afore the test or after the test case. It is an interface that modifies the TestNG comportment. For example, when you are running a test case either through selenium or appium and suddenly a test case fails. We require a screenshot of the test case that has been failed, to achieve such a scenario, TestNG provides a mechanism, i.e., Listeners. When the test case failure occurs, then it is redirected to the incipient block indited for the screenshot.
Listeners are implemented by the ITestListener interface. An ITestListener interface has the following methods:

- onTestStart(): An onTestStart() is invoked only when any test method gets commenced.
- onTestSuccess(): An onTestSuccess() method is executed on the prosperity of a test method.
- onTestFailure(): An onTestFailure() method is invoked when test method fails.
- onTestSkipped(): An onTestSkipped() run only when any test method has been skipped.
- onTestFailedButWithinSuccessPercentage(): This method is invoked each time when the test method fails but within prosperity percentage.
- onStart(): An onStart() method is executed on the commencement of any test method.
- onFinish(): An onFinish() is invoked when any test case culminates its execution.
How to create the TestNG Listeners:-
We can engender the TestNG Listeners in two ways. First, we can utilize the @Listeners annotation within the class and the second way to utilize them within the suite.
First case: First, we will engender the listeners within the class.
Step 1: Open the Eclipse.
Step 2: We engender a simple project.
import org.testng.Assert; import org.testng.annotations.Listeners; import org.testng.annotations.Test; @Listeners(com.javatpoint.Listener.class) public class Class1 { @Test public void sum() { int sum=0; int a=5; int b=7; sum=a+b; System.out.println(sum); } @Test public void testtofail() { System.out.println("Test case has been failed"); Assert.assertTrue(false); } }
In the above code, we create two test cases, i.e., sum() and testtofail().
package com.javatpoint; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult;public class Listener 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 } }
When sum() test case has been passed, then TestNG calls the onTestSuccess() listener. When testtofail() test case has been failed, then TestNG calls the onTestFailure() listener.
Step 3: Now, we create the testng.xml file.
- <?xml version=”1.0″ encoding=”UTF-8″?>
- <!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>
- <suite name=”Suite”>
- <test name=”Listeners”>
- <classes>
- <class name=”com.javatpoint.Class1″></class>
- </classes>
- </test>
- </suite> <!– Suite –>
Step 4: Run the testng.xml file. Right-click on the testng.xml file and move the cursor down to Run As and then click on the 1 TestNG Suite.



Thus we can say ITestlistners in TestNG provides a better way of organizing the test result and ability to provide modifiability in the current default behavior of TestNG. ITestlistners in TestNG is one of the widely used interface or listener available currently.
Refrences:-
https://testng.org/doc/documentation-main.html#testng-listeners