Home Core Java Overview Maven Tutorials

Wednesday, 24 July 2019

Listeners In Selenium and TestNG

Listeners in TestNG


Listeners are interfaces used in selenium webdriver script that modifies the default behaviour of the system.

The main purpose of listeners is to create Logs and reports.

We can fully customize the logs using Listeners.

Types of Listeners
1). WebDriver Listeners
2). TestNG Listeners

Types of Listeners in TestNG :-
There are several interfaces that allow you to modify TestNG behaviour.
Here are few listeners :-
1).IAnnotation Transformer
2).IAnnotation ransformer2
3). IHookable
4). IInvokeMethodListener
5). IMethodInterceptor
6). Ireporter
7). ISuiteListener
8). ITestListener

How to implement the Listener :-
a). Extends 'TestListenerAdapter' Class
b). Implement Interface 'ITestListener'
also we implement Listeners in Class Level as well as Suite Level

TestNG Listeners
TestNG provides the @Listeners annotation whic listens to every event that occurs in your selenium script.
As the name suggests Listeners "listen" to the event defined in the selenium script and behave accordingly. It is used in selenium by implementing Listeners Interface.
Listener is an interface that modifies the TestNG behaviour.
Types of Listeners in TestNG
There are many types of listeners which allows you to change the TestNG's behavior.
Below are the few TestNG listeners:

1.IAnnotationTransformer ,
2.IAnnotationTransformer2 ,
3.IConfigurable ,
4.IConfigurationListener ,
6.IHookable ,
7.IInvokedMethodListener ,
8.IInvokedMethodListener2 ,
9.IMethodInterceptor ,
12.ITestListener .
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 started.

onTestSuccess(): An onTestSuccess() method is executed on the success 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 success percentage.

onStart(): An onStart() method is executed on the start of any test method.

onFinish(): An onFinish() is invoked when any test case finishes its execution.


  1. Hi there,
    Nice Article I really enjoyed this post Thanks For Sharing,
    Obtain ISO 9001 Certification It enhances your product & service quality, Increases marketing opportunities, Reduces your costs and much more.
    ISO 9001 Certification

  2. Hi there,

    Nice Article I really enjoyed this post Thanks For Sharing, check out this
    Ascent ASSOCIATES is one of Sri Lanka’s leading ‘total solutions’ providers, offering a simple, cost-effective route to ISO Certification in Colombo, Kandy, Galle, Dambulla, Sri Jayawardenepura Kotte & all over Sri Lanka.

    Ascent Associates

  3. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles. Check this out

    ISO 22000 Certification in Mumbai