What is ITestListner interface and its Use in Selenium

Test Automation

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used:

1- IAnnotationTransformer
2- IAnnotationTransformer2
3- IHookable
4- IInvokedMethodListener
5- IMethodInterceptor
6- IReporter
7- ISuiteListener
8 – ITestListener

In this blog, I will be covering ITestListner, we will learn how to use this particular listner.

When you implement one of these interfaces, you can let TestNG know about it with either of the following ways:
Using -listener on the command line.

a- Using with ant.
b- Using in your testng.xml file.
c- Using the @Listeners annotation on any of your test classes.
d- Using ServiceLoader.

In Below code, I have created a class Reporting_Listner which i have used ItestListner by extending it. Once after writing this syntax and class name, Java editor(Eclipse in this case) will ask to add method defined for this listner, on clicking yes it well add ITestListner method, method defined in ITestListner are-

1- onFinish
2- onStart
3- onTestFailureButWithSuccessPercentage
4- onTestFailure
5- onTestSkipped
6- onTestStart
7- onTestSuccess

In below code, I have customized onTestFailure and added System.out.println syntax, same will trigger whenever failure happen. Console will print after execution- On Test Skip-Inside Listner class as i have deliberately failed a test case, Refer second code snippet.

package TestNG_Classes;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class Reporting_Listners implements ITestListener{

public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub


public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
//System.out.println("On Test Pass-Inside Listner class");

public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("On Test Failure-Inside Listner class");


public void onTestSkipped(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("On Test Skip-Inside Listner class");

public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub


public void onStart(ITestContext context) {
// TODO Auto-generated method stub


public void onFinish(ITestContext context) {
// TODO Auto-generated method stub



Second code snippet

After above listner class customization, call it in your class where test cases are written, Method which have @Test annotation.

Deliberately, i have failed Test2, which will fail this test and trigger execution of listner syntax onTestFailure method.

package TestNG_Classes;
import junit.framework.Test;

import junit.framework.Assert;
//import org.testng.annotations.AfterTest;
//import org.testng.annotations.BeforeSuite;
//import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Listeners;

public class TestNGWalkthrough

public void Test1()
System.out.println("I am in Test1");

public void Test2()
System.out.println("I am in Test2");


@org.testng.annotations.Test(priority=3)//, dependsOnMethods={"Test2"})
public void Test3()
System.out.println("I am in Test1");


Output of above code- Test 2 is failed and listner method has beed triggered on its failure


Above code is an example, similarly you can call any piece of code in listner class method, on test pass, fail or Skip method.

It is recomended to call screenshot capture, or other log generating methods in listners methods.

Listners make test automation framework more effective as it helps in writing lesser code and can perform many activities by simply listening to test execution.

Listners can be called across multiple classes, In above example i have implemented for one class only

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Triggering SoapUI from selenium

The software industry has adopted Service Oriented Architecture-SOA, which consist of Microservices consist of SOAP and REST. Selenium is an API which helps in Web automation, Selenium does not help in API testing although SoapUI scripts can be invoked from selenium/Java classes. Using below 3 steps SOAPUI project can be …

Selenium Basic
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Test Automation
How to Capture Screenshot in Selenium Webdriver-3 different ways

Abstract– Capturing screenshot is an essential feature of a test automation framework, They help in defect analyses once after test execution is over. In this blog post we will see how to capture screenshot, there are different ways to capture screenshots. Which includes- 1- Capture full Page 2- Capturng full …