TestNG Listeners In Selenium WebDriver With Examples

Ramit Dhamija

Posted On: May 30, 2019

15 Min

There are different interfaces provided by Java that allow you to modify TestNG behavior. These interfaces are known as TestNG Listeners in Selenium WebDriver. They enable you to customize test logs or reports based on your project requirements.

Overview

What are TestNG Listeners?

TestNG Listeners are interfaces that allow you to listen to events during test execution. They provide hooks to customize test behavior, logging, reporting, or executing additional code before, after, or during tests.

What Are the Types of TestNG Listeners?

TestNG offers various listeners to monitor different aspects of test execution. Each listener serves a specific purpose, helping improve reporting, debugging, and test automation flexibility. Common types include:

  • ITestListener: Monitors the test lifecycle events like start, success, failure, and skip, enabling custom reporting or logging actions.
  • IAnnotationTransformer: Modifies test annotations dynamically at runtime to customize execution, priorities, or data-driven parameters programmatically.
  • IInvokedMethodListener: Listens before and after each method invocation, allowing additional actions around test method execution for logging or validations.
  • ISuiteListener: Tracks test suite start and finish events, enabling custom operations, reporting, or setup/teardown at the suite level.
  • IReporter: Generates customized reports after test execution, summarizing suite results, failed tests, and execution statistics programmatically.
  • IConfigurable: Allows configuration methods to access and modify test method details, providing fine-grained control during configuration execution.
  • IExecutionListener: Notifies before and after the entire test run, enabling global setup, teardown, or logging at the framework level.
  • IHookable: Allows overriding the test method execution dynamically, providing control to wrap test method calls for custom logic.
  • IMethodInterceptor: Intercepts test methods before execution to change order, filter, or modify methods based on runtime conditions or data.
  • IConfigurationListener: Monitors configuration method events like before/after suite, class, and method, enhancing logging and debugging capabilities.

Which TestNG Listeners In Selenium WebDriver Do You Use The Most?

In Selenium WebDriver projects, listeners like ITestListener, IReporter, and IInvokedMethodListener are commonly used. They help with reporting, logging, and handling test execution events efficiently. Commonly Used Listeners:

  • ITestListener: Tracks test success, failure, or skip events with custom logs or screenshots.
  • IReporter: Generates detailed, customized execution reports for all test suites.
  • IInvokedMethodListener: Performs actions before or after each method, useful for capturing screenshots or detailed logging.
  • IHookable: Controls the test method execution flow for advanced testing scenarios or additional validations.

What are TestNG Listeners?

TestNG listeners serve as vital components in the TestNG framework, offering a powerful mechanism to monitor and influence the progress of test executions. These listeners are specialized classes that facilitate enhanced control over test scenarios, enabling developers and testers to capture and respond to various events during test runs. With TestNG listeners, you can amplify test automation capabilities by customizing behavior, generating comprehensive reports, and implementing dynamic test management.

There are numerous TestNG listeners in Selenium WebDriver, some of them are used very frequently by the testing community & some are almost forgotten. In this TestNG tutorial, I will demonstrate the most popular TestNG listeners with examples but before that, let me enlist the various TestNG listeners in Selenium WebDriver.

  1. ITestListener
  2. IAnnotationTransformer
  3. IInvokedMethodListener
  4. ISuiteListener
  5. IReporter
  6. IConfigurable
  7. IExecutionListener
  8. IHookable
  9. IMethodInterceptor
  10. IConfigurationListener

With TestNG certification, you can challenge your skills in performing automated testing with TestNG and take your career to the next level.

Here’s a short glimpse of the TestNG certification from LambdaTest:

Frequently Used TestNG Listeners With Examples

Now, in this TestNG tutorial, let’s us first look into the most popular & widely used TestNG listeners with examples.

1. ITestListener

ITestListener is the most adopted TestNG listener in Selenium WebDriver. Providing you with an easy to implement interface through a normal Java class, where the class overrides every method declared inside the ITestListener. By using this TestNG listener in Selenium WebDriver, you can change the default behaviour of your test by adding different events to the methods. It also defines a new way of logging or reporting.

Following are some methods provided by this interface:

onStart: This method is invoked before any test method gets executed. This can be used to get the directory from where the tests are running.

onFinish: This method is invoked after all tests methods gets executed. This can be used to store information of all the tests that were run.

onTestStart: This method is invoked before any tests method is invoked. This can be used to indicate that the particular test method has been started.

onTestSkipped: This method is invoked when each test method is skipped. This can be used to indicate that the particular test method has been skipped.

onTestSuccess: This method is invoked when any test method gets succeeded. This can be used to indicate that the particular test method has successfully finished its execution.

onTestFailure: This method is invoked when any test method gets failed. This can be used to indicate that the particular test method has been failed. You can create an event of taking a screenshot which would show where the test has been failed.

onTestFailedButWithinSuccessPercentage: This method is invoked each time the test method fails but is within the success percentage mentioned. To implement this method, we use two attributes as a parameter of TestNG annotations i.e. successPercentage and invocationCount. The success percentage takes the value of success percentage and invocation count denotes the number of times that a particular test method would execute.

For example: @Test(successPercentage=60, invocationCount=5), in this annotation success percentage is 60% and invocation count is 5, that means out of 5 times if at least 3 times ((⅗)*100= 60) the test method gets passed, it would be considered as passed.

If you are not aware of TestNG and Selenium, we recommend you check our TestNG tutorial to run your first automation script.

For every ITestListener method we usually pass the following arguments:

    • “ITestResult” interface along with its instance “result” which describes the result of a test.

    Note: If you want to trace your exception through ITestResult then you need to avoid try/catch handling.

    • “ITestContext” interface along with its instance “context” which describes the test context containing all the information of the given test run.

    Now, in this TestNG tutorial for listeners, we will take a basic example code for running the test at the class level. Logs would get generated at a console and it would help you understanding which tests passed, failed and skipped.

    The first class(ListentersBlog.java) would contain all the methods implemented by ITestListener interface:

    Below is the code that includes the tests methods(TestNGListenersTest.java). Make sure you add a Listeners annotation just above your class name to implement the above added methods.
    Syntax: @Listeners(PackageName.ClassName.class)

    Console Output Screen:

    console output

    Now, suppose you have multiple classes in your project, then adding TestNG Listeners in Selenium WebDriver to each class might be a pain. In such cases, you can create a test suite and add Listeners tag to your suite(xml file) instead of adding Listeners to each class.

    Here is the example code(testng.xml) for running the test at the suite level :

Watch this video to learn how TestNG has become one of the most robust test automation frameworks and all you need to know to get started with TestNG in Selenium.

2. IAnnotationTransformer

IAnnotationTransformer is an interface that provides a method “transform” which would get invoked by TestNG to modify the behaviour of Test annotation method in our test class.
The transform method provides various parameters:

  1. annotation: The annotation that would get read from the test class.
  2. testClass: If the annotation found on a class, this parameter would represent that same class.
  3. testConstructor: If the annotation found on a constructor, this parameter would represent that same constructor.
  4. testMethod: If the annotation found on a method, this parameter would represent that same method.

Note: At least one of the parameters will be non-null.

Below is the sample code that would be executed at the suite level. In this code, we have used a parameter “alwaysRun = true” in our Test annotation that indicates that the test method would always run even if the parameters on which the method depends fails. However, we would transform this behaviour of our test method through IAnnotationTransformer Listener which won’t allow the particular test method to get executed.

Listeners Class File:

Test Class File:

Console Output Screen:

console output

3. IInvokedMethodListener

This interface allows you to perform some action before and after a method has been executed. This listener gets invoked for configuration and test methods. This TestNG listener in Selenium WebDriver works same as the ITestListerner and the ISuiteListerner. However, there is a difference that you should make a note of & that is, in IInvokedMethodListener, it makes the call before and after every method.

There are two methods to be implemented:

beforeInvocation(): This method is invoked prior every method.

afterInvocation(): This method is invoked post every method.

Here is sample code for this listener, implemented at class level.

File Name: InvokedMethodListenersTest.java (includes configuration and test methods)

Console Output Screen:

console output

Watch this video to learn how to set up and use TestNG with Selenium to automate your testing process. We will also introduce the TestNG Priority method, which allows you to write easy-to-read and maintainable tests.

4. ISuiteListener

This TestNG listener in Selenium WebDriver is implemented at a suite level called ISuiteListener. It has 2 methods:

onStart: This method is invoked prior the test suite execution.

onFinish: This method is invoked post the test suite execution.

This listener basically listen to the events to have occurred before and after the execution of the suite.If the parent suite further contains child suites then child suites are executed before running the parent suite.

Step 1: Implementing ISuiteListener with normal java class and adding the unimplemented methods.

Class: SuiteListeners

Step 2: Creating two test classes to be added in two different child suites.

Class 1: SuiteListenersTests1

Class 2: SuiteListenersTests2

Step 3: Adding the test classes to the child suites.

Suite 1: Test Suite One.xml

Suite 2: Test Suite Two.xml

Step 4: Creating a parent suite xml file that would combine other 2 defined suites along with the listeners class.

Console Output Screen:

console output

5. IReporter

This TestNG listener in Selenium WebDriver provides an interface which helps you to customize the test report generated by TestNG. It provides generateReport method which would get invoked after execution of all the suites. The method further contains three parameters:

  1. xmlSuite: it provides you with a list of multiple suites presented in the testng xml file that goes under execution.
  2. suites: This object represents a great deal of information about the classes, packages, test execution result, along with all the test methods. Basically, it represents detailed information around the suite after the final execution.
  3. outputDirectory: contains the output folder path where the report gets generated.

Below is the example of IReporterer listener at suite level.

File Name :ReporterListener.java

File Name: ReporterTest.java

Console Output Screen:

Console Output Screen

Not So Frequently Used TestNG Listeners In Selenium WebDriver

In this section, I will be highlighting those TestNG listeners which are not so renowned as the ones discussed in the previous section. I have avoided the practical demonstration of these TestNG listeners with their examples as they are rarely used. I will, however, help you understand their purpose.

6. IConfigurationListener

This TestNG listener in Selenium WebDriver is used to create an event only when the configuration method is passed, failed or skipped.
Below are the unimplemented methods provided by this listener:

  • onConfigurationSuccess: It gets invoked when the configuration method gets succeeded.
  • onConfigurationFailure: It gets invoked when the configuration method gets failed.
  • onConfigurationSkip: As the name suggests, when your configuration method is skipped, it calls for onConfigurationSkip method.

7. IExecutionListener

This listener is used to keep track when the test or suite run start and finish. It provides two methods:

onExecutionStart: It is invoked before the suite or test starts running.

onExecutionFinish: It is invoked after the suite or test gets executed.

Note: It is not possible for this listener to prevent the execution but only to create events in some way. Also, you can provide more than one “IExecution” listener as you configure TestNG.

8. IHookable

This interface skips the invocation of test methods and provides a run method which gets invoked instead of each @Test method found. The test method is then invoked once the callBack() method of the IHookCallBack parameter is called.

IHookable listener is utilized when you wish to perform testing on classes which require JAAS authentication. This can be used to set permissions i.e. for whom the test method should run and when the test method should get skipped.

9. IMethodInterceptor

→To return the list of IMethodInstance, post-execution of TestNG.

→ To sort the list of test methods.

TestNG would execute the tests methods in the same order defined in the returned value.
IMethodInterceptor interface includes only one method to implement “intercept” which returns the modified list of test methods.

Example: One of the test methods SampleTestOne is to test the logs, so we grouped it in “LogCheck”.

Now, suppose we only want to run the LogCheck grouped tests and not the other tests, so, we have to provide an IMethodInterceptor listener that can eliminate the other tests and return only LogCheck grouped tests.

10. IConfigurable

The ICongurable listener is somewhat similar to IHookable listener. This interface skips the invocation of test methods and provides a run method which gets invoked instead of each configuration method found. The configuration method is then invoked once the callBack() method of the IConfigureCallBack parameter is called.

Which TestNG Listeners In Selenium WebDriver Do You Use The Most?

I hope this TestNG tutorial, helped you to realize which TestNG listeners are most suitable for your project requirements. Regarding the rarely used TestNG listeners, if there are any specific TestNG listener in Selenium that you find highly useful then feel free to share them in the comment section below. For an all in one session on performing a complete TestNG tutorial with Selenium you can checkout this video.

Also, if you have any questions related to the article, let me know. I look forward to your replies. Happy testing! 🙂

Frequently Asked Questions (FAQs)

What is TestNG and how is it different from JUnit?

TestNG is a testing framework for Java that supports advanced features like parallel execution, test dependencies, and data-driven testing. Unlike JUnit, which focuses mainly on unit tests, TestNG provides more flexibility and control over complex test suites and reporting.

How do you control the order of test execution in TestNG?

You can control the test execution order using the priority attribute in the @Test annotation. Tests with lower priority values execute first. You can also use dependencies to ensure that specific tests run only after others complete successfully.

What is parallel testing in TestNG?

Parallel testing enables simultaneous execution of multiple tests, classes, or methods. This helps reduce total execution time in large projects and can be configured easily in the TestNG XML file.

What is a DataProvider in TestNG?

A DataProvider allows you to run a single test with multiple sets of data. It supports data-driven testing, enabling validation of functionality across different inputs without writing separate test cases.

How does TestNG handle expected exceptions?

TestNG allows defining expected exceptions in the @Test annotation. If the specified exception occurs, the test passes, making it ideal for validating negative test scenarios and exception handling.

What are test groups in TestNG?

Test groups help categorize test methods into logical sets like smoke, regression, or functional. You can run, include, or exclude specific groups using the TestNG XML configuration, making test management efficient.

How are reports generated in TestNG?

TestNG automatically generates HTML and XML reports after each execution, summarizing passed, failed, and skipped tests. These reports give clear visibility into test outcomes and can be shared or customized further.

How can you disable a test in TestNG?

You can temporarily disable a test by setting @Test(enabled = false). This prevents the test from running without removing its code, allowing you to skip unstable or unfinished tests easily.

What are setup and teardown annotations in TestNG?

TestNG provides lifecycle annotations such as @BeforeMethod, @AfterMethod, @BeforeClass, and @AfterClass. These annotations help prepare test data, initialize resources, and perform cleanup after execution.

What are test dependencies in TestNG?

Test dependencies let you specify that a test should execute only after certain other tests have passed. This ensures tests run in a logical order and prevents execution of dependent tests when prerequisites fail.

Author

Ramit Dhamija is a DevOps Engineer with 6+ years of experience in software testing, Java-based test automation, and cloud infrastructure. At LambdaTest, he contributed to automation frameworks that enhanced cross-browser testing reliability. Skilled in Java, Selenium, Kubernetes, and AWS, he also holds certifications including CKA and ISTQB. Ramit earned his MCA in Computer Science from Birla Institute of Technology, Mesra, and specializes in bridging QA practices with modern DevOps pipelines.

Blogs: 10

linkedintwitter