Listener in TestNG basically signifies the process how it will listen to the event defined in the Selenium Test and behave accordingly.
There are multiple Listeners available. Such as
- IAnnotationTransformer
- IAnnotationTransformer2
- IConfigurable
- IConfigurationListener
- IExecutionListener
- IHookable
- IInvokedMethodListener
- IInvokedMethodListener2
- IMethodInterceptor
- IReporter
- ISuiteListener
- ITestListener
TestNG provides @Listener annotation which can listen to the event defined in the script. Listener can listen the event after of before the event so that it can modifies the behaviour.
Can you guess Listeners are Class or Interface?
Yes. This is an Interface. If we want to perform specific action to be performed when test will be failed, then we need to implement Listener interface. When Test failed then this will redirect to the new block written for Listener and perform the action.

In this tutorial we will concentrate on ITestListener.
ITestListener:
ITestListener is the most popular listener in TestNG which provides an easy implementation through java class. Where classes override every method of ITestListener. This will change the default behavior of your Test and customize based on requirement.
With ITestListener, you can implement new way of logging and reporting.
Below are the methods of ITestlistener Interface
- onTestStart- This method will be called when any test method gets started.
- onTestSuccess- This method will be called when any method gets success.
- onTestFailure- This method will be called when any method fails
- onTestSkipped- This method will call when any method skipped.
- onTestFailedButWithinSuccessPercentage – This method will be called when any method fails but within success percentage.
- onFinish-This method will be called when any method finishes the execution
Example:
Create Listener class.
package testinglpointListener; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; public class TestinglPointListeners implements ITestListener { public void onTestStart(ITestResult result) { System.out.println("New Test Started" + result.getName()); } public void onTestSuccess(ITestResult result) { System.out.println("Test Successfully Finished" + result.getName()); } public void onTestFailure(ITestResult result) { System.out.println("Test Failed" + result.getName()); } public void onTestSkipped(ITestResult result) { System.out.println("Test Skipped" + result.getName()); } public void onTestFailedButWithinSuccessPercentage(ITestResult result) { System.out.println("Failed Testcase with success value" + result.getName()); } public void onStart(ITestContext context) { System.out.println("This is onStart method" + context.getOutputDirectory()); } public void onFinish(ITestContext context) { System.out.println("This is onFinish method" + context.getPassedTests()); System.out.println("This is onFinish method" + context.getFailedTests()); } }
Create Test class.
package testinglpointListener; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.SkipException; import org.testng.annotations.Listeners; import org.testng.annotations.Test; import junit.framework.Assert; @Listeners(testinglpointListener.ListenersBlog.class) public class TestinglpointListenersTest { @Test System.setProperty("webdriver.chrome.driver", "C:\\Users\\Pratik\\Desktop\\Selenium\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.testinglpoint.com/"); driver.manage().window().maximize(); driver.findElement(By.xpath("//*[@id=\'name\']/div/ul[2]/li[3]")).click(); Thread.sleep(2000); driver.findElement(By.linkText("iPad")).click(); Thread.sleep(2000); driver.quit(); } @Test // Failing System.out.println("Forcely Failed Test Method"); Assert.assertTrue(false); } private int i = 0; @Test(successPercentage = 60, invocationCount = 5) //Test Failing Within Success Percentage public void sampleTest3() { i++; System.out.println("Failed Testcase with success value, count: " + i); if (i == 1 || i == 2) { System.out.println("sampleTest Failed"); Assert.assertEquals(i, 6); } }