Understanding TestNG Listeners: Vital Selenium Automation Components
Tags: testng, selenium, testgrid
Published: 2024-03-01 10:50:16

TestNG Listeners in Selenium: A few years ago, when films and television series were made about the world in 2050, technologies looked no less than magic. With the progress we are going through, those imaginations seem rather old compared with the technologies we live with now. 

That is what we are living through; there is a solution for almost everything when it comes to the matter of technology. From helping small-scale businesses with the intricate digital scale to augmented reality, everything has been designed to give ease to humans. 

When it comes to software testing, the notion seems 100% true. The TestNG listeners in  Selenium are one such example. Perplexed about what we are trying to say? Read on and find out!
		
TestNG is, first and foremost, a testing framework widely used in the Selenium community to perform unit and functional testing of software applications. You need to know that the TestNG listener is a kind of interface that is usually used to employ and further implemented to modify the given default behavior of TestNG during the execution of test cases. 
Want to know more? This article will give you all the ins and outs of TestNG and TestNG listeners in Selenium to get you started.

## Introduction

Before delving into further details, you will need to have a detailed Idea about TestNG listeners in selenium code. The concept is basically a popular outline used in Selenium test automation.

TestNG listeners are Java classes that implement specific interfaces defined by TestNG. These interfaces define methods that get called before or after specific events in the test lifecycle. 

At the time you choose to implement these listener interfaces, you can customize the behavior of your tests based on the events that occur during test execution. For example, you could implement a listener that takes a screenshot of the browser window whenever a test fails or logs additional information about test execution.

However, to save the most unnecessary uses of resources, there is a restriction to listen to all the [Selenium codes](https://testgrid.io/blog/selenium-with-java/) every time. 

## **Types of TestNG Listeners in Selenium:**

There are indeed various types of TestNG Listeners. These are typically based on the usage and application of the program. A few kinds are: 

- IAnnotationTransformer
- IAnnotationTransformer2
- IConfigurable
- IConfigurationListener
- IExecutionListener
- IHookable
- IInvokedMethodListener
- IInvokedMethodListener2
- IMethodInterceptor
- IReporter
- ISuiteListener
- ITestListener

These TestNG listeners are based on different types of events that occur during the execution of TestNG tests. These events can be categorized into the following types:

The level of the test events: TestNG Listeners in Selenium
These events occur at the test suite, test case, or test method level. They include events such as the start and end of a test suite, test case, or test method and the failure, success, or skip of a test method.

**Event of the configurational execution:** TestNG Listeners in Selenium

These are typically the events that occur either before or after the execution of test methods and are related to the setup and tear down of test environments. They include events such as the start and end of a configuration method and the failure, success, or skip of a configuration method.

**Events based on the suit-level:** TestNG Listeners in Selenium
The events occur at the level of the entire selenium test suite and are related to the setup and tear-down of the entire test suite. They include events such as the start and end of a suite and the failure, success, or skip of a suite.

Based on these event types, testing listeners for reporting is usually done. Also, TestNG provides various listeners that can be employed to handle different types of events during the execution of tests. 

For example, the Test Listener interface can handle test-level events, the IInvokedMethodListener interface can handle configuration-level events, and the ISuiteListener interface can be used to handle suite-level events.
Several methodologies are used to formulate programming, such as 
onStart, 
onTestStart, 
onTestSuccess, 
onTestFailure, 
onTestSkipped,
onTestFailed But Within Success Percentage, 
onTestFailedWithTimeout, onFinish, 

Using this methodology, you can customize the behavior of your test suites by handling specific events during the test execution lifecycle. 

For example, you can use the onTestFailure method of the TestNG Test Listener to capture the screenshot of the failed test case and attach it to the test report.
Reasons Behind The Popularity Of The TestNG Listeners in Selenium

The main motto of creating TestNG is to enable the developer to write a test and employ the annotations, which can further be prioritized, grouped, or even executed. This is also the reason behind the popularity of this programming. 

The program supports data-driven testing and parameterization if you consider integrating with the IDE tools like IntelliJ and Eclipse and a few build tools such as Ant, Maven, and Gradle. 

A few further reasons behind the popularity of TestNG Listeners are:

**The Annotations**

You need to know that different listeners in TestNG provide several annotations that you can use to mark your test methods, such as @Test, @BeforeTest, @AfterTest, etc. These annotations make it easy to write and organize your test cases.

**The Prioritization**

TestNG allows you to prioritize your test methods, so you can ensure that critical tests run first. You can also group your test methods and run a specific group of tests.

**The Data-Driven testing**

With TestNG, you can perform data-driven testing by providing data to your test methods from various sources such as Excel, XML, or CSV files.

**The Parameterization**

TestNG allows you to parameterize your test methods, so you can run the same test with different inputs.

**The integration with IDEs and build tools**

TestNG can be easily integrated with popular IDEs like Eclipse and IntelliJ and build tools like Ant, Maven, and Gradle.

**The Parallel Execution**

You need to know that TestNG allows you to run your test cases in parallel, which can significantly reduce the time to execute your test suite.

**The Test Reports** 

TestNG generates detailed HTML reports showing your test cases’ results, including which tests passed, failed, or skipped. It also generates logs that you can use to debug your test cases.

Now let us understand more about TestNG listeners with one of the most frequently used TestNG listeners- ITestListener.

**What is ITestListener In TestNG?**

If you have been working with the ITest Listener, you must be quite familiar with the default listener. It is the very thing that allows you out of the box, that is, before the test, before the method, and after the test, and after the method. 
In the case of ITest, the Listener plays an essential role in the overall setting up the tearing down of the entire test. 

This is because they are limited to the behavior they expose. 
Suppose you need to perform a rather specific operation using the operation when a test passes. In that case, if you would like to perform a step right before the method, you will certainly need an ITestListener. By implementing ITestListener, you can perform custom actions based on these events. 
A well-fitted testing listeners example would be if you can take a screenshot of the page when a test method fails or send an email with the test results after the test execution completes.

There is a typical interface for the ITestListeners, and it has a typical method, which is:
•onTestStart: you need to know that an On TestStart is provoked at the time when any kind of method starts. 
•onTestSuccess: TestSuccess is a typical method usually executed when any test method is successful.
•onTestFailure: it is a method invoked at the time when the test method fails. 
•onTestSkipped: At the time when any test method has been skipped, the onTestSkipped gets invoked. 
•onTestFailedButWithinSuccessPercentage: This is a typical method that is widely used every time a certain test method fails but with a certain percentage of success. 
•onStart: it is a method that is usually executed at the start of any method. 
•onFinish: Typically used at the time when any of the tests typically finishes its execution. 

**Steps to Create TestNG Listener:**

TestNG provides a powerful feature called “Listeners” that allows you to customize the test execution process. TestNG Listeners allow you to listen to different events that occur during the test execution and customize the test behavior accordingly. 

In order to create a TestNG Listener, follow these steps:
Create a Java class that implements one of the TestNG Listener interfaces. TestNG provides the following Listener interfaces:
IAnnotationTransformer
IAnnotationTransformer2
IConfigurable
IConfigurationListener
IExecutionListener
IHookable
IInvokedMethodListener
IInvokedMethodListener2
IMethodInterceptor
IReporter
ISuiteListener
ITestListener

You will be able to understand better with an example. In case you want to listen to the test execution events, you can implement the ITestListener interface as discussed above.
Implement the methods defined in the interface. Each interface defines a set of methods that correspond to different events during the test execution. You can implement the methods based on your requirements. 
Register the Listener with TestNG. You can do this in two ways:
Add the Listener to the testng.xml file. You can specify the Listener class in the <listeners> tag as follows:
<listeners>
  < Listener class-name=”com.example.MyListener”/>
</listeners>

Register the Listener programmatically. You can create an instance of the Listener class and register it with the TestNG object as follows:
TestNG testng = new TestNG();
testng.addListener(new MyListener());
testng.setTestClasses(new Class[] {MyTestClass.class});
testng.run();

That’s it! Your TestNG Listener is now ready to use. By implementing the appropriate Listener interface methods, you can customize the Listener behavior based on your requirements.

**Use of Listener for multiple classes:**

There is a specific use of Listener for multiple classes. If a project has more than one class, then adding up the listeners to each other is a wise idea. 

Suppose you want to use a listener for multiple classes. In that case, you can create a single listener class that implements the appropriate listener interface(s), and then register instances of this listener class with each of the classes that need to be listened to.

For example, let’s say you have two classes, Class A and Class B, and want to listen for changes in both. You can create a listener class, let’s call it MyListener, that implements the appropriate listener interfaces for both classes. 
Here’s an example:
public class MyListener implements ActionListener, ChangeListener {
    public void actionPerformed(ActionEvent e) {
        // Handle action events from ClassA
    }

    public void stateChanged(ChangeEvent e) {
        // Handle state changes from ClassB
    }
}

Then, in both ClassA and ClassB, you can create an instance of MyListener and register it as a listener for the appropriate events:
public class ClassA {
    private MyListener listener;

    public ClassA() {
        listener = new MyListener();
        someButton.addActionListener(listener);
    }
}

public class ClassB {
    private MyListener listener;

    public ClassB() {
        listener = new MyListener();
        someComponent.addChangeListener(listener);
    }
}

In this example, ClassA registers the MyListener instance as an action listener for a button, and ClassB registers the same instance as a change listener for a component. When the button is clicked, or the component’s state changes, the appropriate method in MyListener will be called to handle the event.

## Conclusion

Now that you have a detailed idea about TestNG Listeners, you might need to give it a go. Listeners are crucial for developing an end-to-end architecture for online applications. They further assist in enhancing and making automation testing simpler by collecting screenshots when test cases fail, customizing reports, and modifying variables during run time.
With TestNG Listeners in Selenium, there are simply so many things to learn out of the box. Once you have an idea about all of that, you will see how TestNG listeners can make the automation process more efficient. 

This blog is originally published at [Testgrid](https://testgrid.io/blog/testng-listeners-in-selenium/)