TestNG reporting tool is of the most common and frequently used tool with Selenium Webdriver. So, every tester must have a detailed knowledge about this tool.

TestNG report can be customized using the different types of Listener. It helps pass the proper message in execution report, so that anyone can easily understand it. TestNG provides many different types of Listener. With the help of Listeners, we can generate own events when test cases Pass/Fail or Skipped. The main purpose of Listeners is to extend the default report behavior of TestNG, which helps in generating own custom reports by listening to the different events.

For example, if you have implemented ‘ITestListener’ class or extended ‘TestListenerAdapter’ and written your code in a custom listener to override onTestFailure or onTestSuccess methods, you will be able to do anything you want when a test fails or passes, including writing methods to connect with the database, taking screenshots, sending emails, etc.

Here are a few examples of different methods in Listener:

onTestStart: This method will always be invoked each time before a test method will be invoked. This method allows you to perform any type of action required before each test method.
Example: Checking the server state before starting the execution, Login into an application, Close other browser instances, and more.

onTestSuccess: This method will always be invoked each time a test succeeds. This method allows you to perform any type of action required on each test method success.
Example: Sending mail on each test success, etc.

onTestFailure: This method will always be invoked each time a test fails. This method allows you to perform any type of action required on each test method failure.
Example: This method can be implemented while capturing screenshot on every failure using the test name.

onTestSkipped: This method will always be invoked each time a test is skipped. This method helps in performing any type of action which is required on each test method skip.
Example: This method can be implemented to pass messaged in the Log.

onTestFinish: This method will always be invoked after all the tests have run and all their configuration methods have been called. This method allows you to perform any type of action which is required on test method finish.
Example: This method can be used when sending mail of execution details, like how many test cases Passed/Failed/Skipped.

There are a number of Listeners in TestNG .Here are a few common Listeners in TestNG:

  • IExecutionListener
  • IAnnotationTransformer
  • ISuiteListener
  • ITestListener

Example of IExecutionListener:

It monitors the beginning and end of TestNG run and includes two methods, first one is onExecutionStart() and the second one is onExecutionFinish(). Method onExecutionStart() is run before the TestNG starts running the suites and onExecutionFinish() is run after TestNG has completed running all the test suites.

In the example given below, two IExecutionListener listeners class are created: ExecutionListenerClass1 and ExecutionListenerClass2. In class ExecutionListenerClass1, the start time is recorded within the method onExecutionStart() and the method onExecutionFinish() is used to print the time TestNG takes to run all the suites.

ExecutionListenerClass1:

ExecutionListenerClass1

ExecutionListenerClass2:

ExecutionListenerClass2

The class TestExampleClass has two annotations @BeforeSuite and @AfterSuite method.

TestExampleClass:

TestExampleClass

To configure the Listener in testing.xml file, we use <listeners> tag. We need to specify the listener implementation’s fully qualified name in class-name attribute.

TestngExecutionListener.xml:

TestngExecutionListener.xml

Execution output

So, this is how we use Listener in project and generate customized reports. TestNG provides many different listeners; if you want, you can use in this way and customize your default generated report.

Read more: Easy 5 steps to get browser detail in TestNG Reports with ReportNG?