TestNG Listeners In Selenium

TestNG Listeners in Selenium

There are several interfaces provided by TestNG which could alter the behavior of TestNG implementation. TestNG Listeners are invoked on the occurrence of certain events (for eg. failure of scripts, execution completion of test suite etc.).

So far we have learned how to use several annotations and attributes which helps in test execution. In addition to these annotations, TestNG provides a Listeners interface which we can implement in our test class to further control the test execution.

The class TestListenerAdapter extends java.lang.Object and also contains beforementioned interfaces, along with some other useful methods., which we must override in order to use them. 

Implementation of TestListenerAdapter Class is shown below:

 

TestListenerAdapter Implementation

Visit the following url to see all the methods of this class.

http://static.javadoc.io/org.testng/testng/6.13/org/testng/TestListenerAdapter.html

Following is the brief on these TestNG listeners interfaces. However, ITestListener is one of the most widely used TestNG listener. The methods inside each interface are quite self-explanatory.

  1. IConfigurationListener: This TestNG listener is invoked whenever any event related to configuration methods occur.

    • Methods: return type of below methods is void.

      • onConfigurationFailure(ITestResult itr)

      • onConfigurationSkip(ITestResult itr)

      • onConfigurationSuccess(ITestResult itr)

  2. IConfigurationListener2: It is invoked before any configuration method is executed. It has only 1 method.

    • Methods: returns void

      • beforeConfiguration(ITestResult tr)

  3. IResultListener: This is also one of the most widely used TestNG listener interface. Most popular methods inside this interface are written below. You may visit HERE to see all the methods. This TestNG listener interface is invoked when the status of test scripts is changed during execution.

    • Methods: returns void

      • onTestFailure(ITestResult itr)

      • onTestSuccess(ITestResult itr)

      • onTestSkipped(ITestResult itr)

ITestResult and ITestContext in TestNG

ITestResult and ITestContext are TestNG interfaces. So far we have only seen ITestResult, as all the above methods use it. ITestResult and ITestContext are populated accordingly, whenever we implement the corresponding testng listener.

Both of these interfaces contain methods which are useful in interacting with a particular test or whole run.

  • ITestResult: This interface contains information related to the result of test runs (test case get fails, pass, skipped etc.).
  • ITestContext: It stores the information related to the whole test run. It could be used to get names of all the passed test cases, get the test suite name etc.
  • itr: This is merely an object reference name which holds the information of ITestResult/ ITestContext. Using this reference we can implement the methods of ITestResult and ITestContext interfaces.

Implement TestListenerAdapter interface

There should be a separate class which will implement TestListenerAdapter interface and all the overridden methods. Click HERE if you have not already set up your TestNG environment.

Once you are done with the initial setup of your project, create a new package listeners. Inside this package create a class MainClass and add the following code.

package listeners;

import org.testng.annotations.Test;

public class MainClass {
    
    @Test
    public void login() {
        System.out.println("Login Test");
    }
    
    @Test
    public void registrationPage() {
        System.out.println("Registration Test");
    }
    
    @Test
    public void fillForm() {
        System.out.println("Fill Form Test");
    }
    
    @Test
    public void logout() {
        System.out.println("Logout Test");
    }
    

}

This is a simple TestNG class with 4 methods/ tests. If you execute the above class as TestNG suite, all 4 tests should pass. Update the code as below (highlighted in bold) to deliberately alter their execution status.

package listeners;

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

public class MainClass {
    
    @Test
    public void login() {
        System.out.println("Login Test");
    }
    
    @Test
    public void registrationPage() {
        System.out.println("Registration Test");
    }
    
    @Test
    public void fillForm() {
        System.out.println("Fill Form Test");
     //fail the test 
        Assert.fail();
    }
    
    @Test(dependsOnMethods = {"fillForm"}) //Since fillForm() will fail, this test case would get skipped 
    public void logout() {
        System.out.println("Logout Test");
    }
    

}

Priority and dependsOnMethods Annotation in TestNG

Create another class ListenerImplementation which will extend the TestListenerAdapter class. If you wish to receive an email after a test gets fail/ pass/ skipped, or email a report after the whole run is executed etc.,  all that code should be written in this class.

Let’s implement some methods in this class:

package listeners;

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class ListenerImplementation extends TestListenerAdapter{
    
    //method inside TestListenerAdapter to work with failed test cases 
    public void onTestFailure(ITestResult tr) {
        //print name of failed test case 
        System.out.println("Failed test case: "+tr.getName());
        
    }
    

    public void onTestSkipped(ITestResult tr) {
        //print name of skipped test case
        System.out.println("Skipped test case: "+tr.getName());
        
    }
    
    public void onTestSuccess(ITestResult tr) {
        //print name of passed test case
        System.out.println("Passed test case: "+tr.getName());
        
    }

}

The methods used above are related to the status of tests, hence ITestResult interface is used. getName is the method inside this ITestResult interface which is overridden in TestListenerAdapter class. This method would return the test case name, which is the method name in class.

Create testng.xml file

To execute the test scripts, we will create an XML testng_listeners.xmlBelow is the XML code:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumTestNG suite">


    <test name="listeners">
        <classes>
            <class name="listeners.MainClass">

            </class>
        </classes>
    </test>

</suite>

Learn how testng.xml works

Click HERE

If you execute your test scripts using above XML, you’ll get same results as you were getting earlier. We need to tell our XML to execute the methods of our listener class if execution status gets changed of any of the tests.

TestNG listener results
Console still prints the results from MainClass

Update testng.xml to implement listeners

Add the following lines of code in your testng_listeners.xml to bind the listener class and XML file.

Paste your text here.<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumTestNG suite">
    <listeners>
        <listener class-name="listeners.ListenerImplementation" />
    </listeners>

    <test name="listeners">
        <classes>
            <class name="listeners.MainClass">

            </class>
        </classes>
    </test>

</suite>

Added lines of code are highlighted in bold. In added lines, just pass the listener class name including package. Execute the above XML and you should get the following result.

Since we have not used priority in any of the @Test annotations, tests would get executed in alphabetical order. Based on each test result output, corresponding methods from ListenerImplementation class gets executed.

Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

5 thoughts on “TestNG Listeners In Selenium

Leave a Reply

Your e-mail address will not be published. Required fields are marked *