What are TestNg Listeners and Types of Listeners in TestNG?

Devstringx Technologies
4 min readMay 23, 2022

Listeners

Listeners additionally allow customizing the take a look at logs or reviews in step with undertaking requirements. In 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
  3. 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.
  4. 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 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 of kind kinds of Listener in TestNG-

  1. ITestListener
  2. IReporter
  3. ISuiteListener
  4. IInvokedMethod
  5. IHookable
  6. IConfigurationListener
  7. IConfigurableListener
  8. IAnnotationTransformer
  9. IExecution
  10. IMethodInterceptor
  11. 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 Test 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 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:

<!DOCTYP suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Listener Execution">
<listeners>
<listeners class name="TestNGListeners"></listener>
</listeners>
<classes>
<class name="TestNG"></class>
</classes>
</suite>

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 offer document to the beginning of a set execution and stop of 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 similar 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 be finished by TestNG.

Originally published at https://www.devstringx.com on May 23, 2022

--

--

Devstringx Technologies

Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services