TestNG Listeners in Selenium Webdriver

In this post, we will learn what is TestNG Listeners and how to use it in automation testing with Selenium.

What is TestNG Listeners?

Listener is use to modify the default behavior of TestNG reporting and logs.

TestNG listeners are piece of code that listens to the events occurring in the TestNG and execute the code.

It is used in selenium by implementing Listeners Interface and @Listeners annotation.

We have earlier discussed use of ITestListener interface in taking the screenshot of failed test cases.

Read this link : How To Take screenshot Of failed Test Cases using ITestListener


Types of Listeners in TestNG:

There are different types of listeners available in TestNG.









These listeners can be implemented in TestNG in the following ways:

Using <Listeners> tag in testng.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <listener class-name = "Listener"/>
  <test thread-count="5" name="Test">
  </test> <!-- Test -->
</suite> <!-- Suite -->

Using Listeners annotation (@Listeners) in a testng class like below –

Syntax –



This is the interface which we need to implement into our class. It has different methods which are mentioned below.

onStart: This method is invoked before any test method gets executed.

onFinish: This method is invoked after all test methods gets executed.

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

onTestSkipped: This method is invoked when any tests method is skipped.

onTestSuccess: This method is invoked when any tests method is success.

onTestFailure: This method is invoked when any tests method is failed.


For every ITestListener method we pass the following arguments:

 ITestResult: It is an interface which describes the result of a test.

 ITestContext: It is an interface which describes the text context containing all information of the given test run.


Implementing ITestListener at class level:

package datapkg;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class ListenerClass implements ITestListener {
    public void onStart(ITestContext context) { 
         System.out.println("onStart method started");
         public void onFinish(ITestContext context) {
         System.out.println("onFinish method started");
         public void onTestStart(ITestResult result) {
         System.out.println("New Test Started" +result.getName());
         public void onTestSuccess(ITestResult result) {
         System.out.println("onTestSuccess Method" +result.getName());
         public void onTestFailure(ITestResult result) {
         System.out.println("onTestFailure Method" +result.getName());
         public void onTestSkipped(ITestResult result) {
         System.out.println("onTestSkipped Method" +result.getName());
         public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
         System.out.println("onTestFailedButWithinSuccessPercentage" +result.getName());

Now our listener class set. We need to implement the listeners in another class.

I have created this class with name as TestNGEx.

In this we will use @Listeners annotation.

package datapkg;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

public class TestNGEx {
    static WebDriver driver = null;
    @Test //failed Test case
    public void OpenBrowser() {
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
    String expectedTitle = "AutomationTesting";
    String ActualTitle = driver.getTitle();
    Assert.assertEquals(expectedTitle, ActualTitle);
    Reporter.log("This Test cases is failed");
    @Test  // Passed 
    public void closeBrowser() {
        Reporter.log("Driver closed after testing.");



TestNG Listeners Output

TestNG Listeners for Reporting:

IReporter Listeners In TestNG

This listener in the testng provides us to customize the testng reports. This Listener contains a method called generateReport(). This method takes three arguments.

xmlSuite: This is the list of suites that exist in the xml file.

suites: This is an object which contains all the information about the test execution.

outputDirectory: It contains the path of  generated output report.


How to Implement IReporter Listeners:

In below example, we will be logging the number of tests that have passed, failed and skipped.

import java.util.List;
import java.util.Map;

import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.xml.XmlSuite;

public class ListenersTestNGEx1 implements IReporter{
   public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
      String outputDirectory) {
      //Iterate over every suite assigned for execution
      for (ISuite suite : suites) {
         String suiteName1 = suite.getName();
         Map<String, ISuiteResult> suiteResults = suite.getResults();
         for (ISuiteResult sr1 : suiteResults.values()) {
            ITestContext tc = sr1.getTestContext();
            System.out.println("Passed tests for suite '" + suiteName1 +
               "' is:" + tc.getPassedTests().getAllResults().size());
            System.out.println("Failed tests for suite '" + suiteName1 +
               "' is:" + tc.getFailedTests().getAllResults().size());
            System.out.println("Skipped tests for suite '" + suiteName1 +
               "' is:" + tc.getSkippedTests().getAllResults().size());

In the above tests, we are iterating through the object suites that contain all the information . Since it contains much information, we are storing the test execution results in a map and printing the required details.

The TestNG file would like follows:


import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNG {
   public void Method1() {
   public void Method2() {
   @Test(dependsOnMethods = {"Method1"})
   public void thirdMethod() {


As the name suggest, the listener work at suite level. It listens and runs before the start and end of suite execution. It has two methods.

onStart: It invoked before test suite execution starts.

onFinish: It invoked after test suite execution finished.


Read More about other Listeners.

Also see below video to clear the concept of TestNG Listener.


In this post, we have covered ‘TestNG Listeners in Selenium Webdriver’ and it’s methods and how to use it.

I am sure this content added some additional value in your skills and also helpful to preparation of your interviews.

Final word, Bookmark this post TestNG Listeners in Selenium Webdriver for future reference.

If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below!


Leave a Comment