What Are TestNg Listeners and Types of Listeners in TestNg? | DS

Back to Blog
TestNG Listeners

What Are TestNg Listeners and Types of Listeners in TestNg? | DS

Listeners In Selenium

Listeners additionally allow customizing the take a look at logs or reviews in step with undertaking requirements. Selenium the maximum famous take a look at the automation framework used to check websites presents several functionalities to permit this interplay among net pages.

Forms of Selenium Listeners

  1. WebDriver Listeners
  2. TestNG Listeners
  1. WebDriver Listeners- Web Driver Listener is an interface protecting predefined methods. They are beneficial for viewing activities precipitated with the aid of using the Web Driver.  In The WebDriverListener interface, we will put into effect instructions and methods.
  2. TestNG Listeners- TestNG Listeners listen or take a look at execution and assist to carry out moves at more than one range of taking a look at execution.TestNG Listeners additionally allow customizing the take-a-look at logs or reviews in step with your requirements. In our code TestNG listeners are carried out as interfaces. These interfaces modify the character of the system.

TestNG Listeners in Selenium Web driver may be applied at the levels:

  1. Class level: These listeners put into effect in magnificence level regardless of what number of taking a look at instances it includes.
  2. Suite level: These listeners are put into effect at suite level for a selected suite which incorporates numerous instructions to take a look at instances.

Types of Listeners in TestNG

There is one kind of Listener in TestNG-

  1. ITestListener
  2. IReporter
  3. ISuiteListener
  4. IInvokedMethod
  5. IHookable
  6. IConfigurationListener
  7. IConfigurableListener
  8. IAnnotationTransformer
  9. IExecution
  10. IMethodInterceptor

1) ITestListener

ITestListener is an interface carried out withinside the magnificence.ITestListener is the maximum followed TestNG listener in Selenium WebDriver.ITestListener offers a few techniques.

  • onStart(): On begin technique invoked after check magnificence is instantiated and earlier than the execution of any testNG technique.
  • Syntax: void onStart(check begin);
  • onTestSuccess(): On the Test, the fulfillment technique invoked at the fulfillment of a check.
  • Syntax: void onTestSuccess(Test result);
  • onTestFailure(): This technique invokes each time a check case fails.
  • Syntax: void onTestFailure(Test result);
  • onTestSkipped(): invoked whilst a check is skipped.
  • Syntax: void onTestSkipped (Test result);
  • onTestFailedButWithinSuccessPercentage(): This technique is invoked every time a technique fails however in the described fulfillment result.
  • Syntax: void onTestFailedButSuccessPercentage (Testresult);
  • onFinish(): This technique invokes whilst all of the check techniques have run, and calling of all in their configuration techniques.
  • Syntax: void onFinish(check  finish);

Implementing ITestListener At Suite Level:



<suite name="Listener Execution">
             <listeners class name="TestNGListeners"></listener>
            <class name="TestNG"></class>
2) IReporter

This TestNg listener assists to generate custom reviews in TestNG primarily based totally on conditions. And It gives the generate Report approach which invokes after the execution of all the suites. This approach takes 3 parameters:

  • xmlSuite: It carries a listing of suites for execution withinside the XML file
  • suites:  it represents targeted statistics across the suite after the very last execution.
  • output directory: It carries the course wherein the file will be saved.
3) ISuiteListener

This TestNG listener in Selenium Web Driver is carried out at a set stage known as ISuiteListener. This listener offers a document at the beginning of a set execution and stops the suite execution. It consists of strategies:

  • onStart: earlier than check suite execution starts.
  •                 Syntax: void onStart(ISuite suite);
  • onFinish:  after check suite execution finishes.
  •              Syntax: void onFinish(ISuite suite);

Read Also:- Integrate Extent Report with in NUnit Selenium Framework Screenshots

4) IInvokedMethod

This TestNG listener in Selenium WebDriver works similarly to the ITestListerner and the ISuiteListerner.The IInvokedMethod interface permits you to carry out a few movements earlier than and after a way has been executed.IInvokedMethod listener consists of  strategies:

  • Before invocation(): this approach was invoked earlier than each approach.
  • After invocation(): this approach is invoked after each approach.

5) IHookable

IHookable listener interface runs its run() approach as opposed to the @Test approach on every occasion observed throughout the execution.

6) IConfigurationListener

IConfigurationListener in TestNG triggers best whilst the configuration strategies pass, fail, or skip. The IConfiguration listener has the subsequent strategies:

  • onConfigurationSuccess: The fulfillment of the configuration approach invokes this approach.
  •             Syntax: void onConfigurationSuccess(TestResult fulfillment);
  • onConfigurationFailure: The failure of the configuration approach invokes this approach.
  •             Syntax: void onConfigurationFailure(TestResult Fail);
  • onConfigurationSkip: This approach is invoked whilst the configuration approach is skip.
  •             Syntax: void onConfigurationSkip(TestResult Skip);
7) IConfigurableListener

IConfigurableListener skips the check strategies and presents a run approach invoked as an alternative to every configuration approach observed.

Syntax: void run(IConfigure testresult, ITestResult testresult);

8) IAnnotationTransformer

IAnnotationTransformer transforms the TestNG annotations at run time. The remodel listener has the subsequent parameters:

  • annotation: The annotation is studied from the check class.
  • testClass: The annotation discovered on a class.
  • testMethod:  the annotation discovered a technique.
  • testConstructor: The annotation discovered a constructor.

9) IExecution

IExecution listener is used to the music while the check or suite run commenced and is completed. And it’s far used to start/stop the server while beginning or finishing code execution. The IExecutionListener techniques:

  • onExecutionStart: This technique invokes earlier than the check start.
  • onExecutionFinish: This technique invokes after all of the tests/suites have completed the execution.

10) IMethodInterceptor

This listener enables modification of the techniques that TestNG is meant to run. The IMethodInterceptor interface consists of the most effective technique to implement intercept which returns the changed listing of check techniques. The IMethodInterceptor listener in TestNG most effective technique:

Intercept: This technique returns the listing of techniques as a way to finish by TestNG.


  • What is the purpose of a listener?

When a user clicks a mouse button or presses a key, for example, an event listener in Java “listens” for that event and then react appropriately. An event object that defines the event must attach to an event test listener in selenium.

  • Why is listener used?

In order to subscribe to these events, you can configure the listener in web.xml, for instance, HttpSessionListener. Servlet Listener uses to listen to events in a web container, such as when you create a session, insert an attribute in a session, or if you passivate and activate in another container.

  • How do listeners work?

When a user clicks a mouse button or presses a key, for example, an event listener in Java “listens” for that event and then react appropriately. An event object that defines the event must attach to an event listener.

  • What is the difference between a WebDriver listener and a TestNG listener?

TestNG works on various test-related events, whereas WebDriver works on various automation events. The important thing to remember is that “Logging” occurs before/after event occurrence with WebDriver listeners in selenium, such as click/SendKeys, etc.

  • What is the difference between an event and a listener?

While an Event Adapter class offers a default implementation of an EventListener interface, an EventListener interface defines the methods that must implement by an event handler for a certain kind of event.

If you are interested in even more software testing-related articles and information from us here at Devstringx, then we have a lot to choose from for you.

Share this post

Back to Blog