top of page

Using the TestListenerAdapater class to implement the ITestListener Interface IAnnotationTransformer Interface and other Interfaces.

Writer's picture: Nithya GunasekaranNithya Gunasekaran

TestListenerAdapter is a class available in the TestNG framework and this can be used to customize and relate the events handling during the test execution lifecycle. By extending this TestListenerAdapter class you can use the features available in the ITestListener Interface. TestNGListener Interface and other interfaces can be implemented in one class file and the steps involved are extending this TestListenerAdapter class is explained in this following blog. All the interface methods have different uses and can be handled during the test execution life cycle.


Key Features of TestListenerAdapter:


  • TestListenerAdapter class is extended and the different combination of interface combinations by implementing their different methods are used to customize the unique reports.

  • It is used for logging the behaviour of the test results.


TestListenerAdapter Implementation:

If you extend the TestListenerAdapter class, you must define all the override methods. However, in most cases, you only need to override a very few of these methods according to the requirement of the reports.The Interface simplifies this by providing default override methods and also lets you to override the methods only what you need. For better understanding of this TestListenerAdapter, you must first learn how to implement the ITestListener interface. Please click the Link ITestListener to understand and learn how to implement the interface.


Interfaces:

  • IConfigurationListener

  • org.testng.internal.IResultListener

  • org.testng.internal.IResultListener2

  • ITestListener

  • ITestNGListener


There are several interfaces that allow you to modify TestNG behavior. These interfaces are broadly used. Here are a few listeners methods:


Commonly Overridden Methods:

onTestStart(ITestResult result) - This method get invoked each time before a test will

                                                                          be invoked.

onTestSuccess(ITestResult result) - This method get invoked each time when the test

success.

onTestFailure(ITestResult result) - This method get invoked each time when a test fails.

onTestSkipped(ITestResult result) - This method get invoked each time when a test

is skipped.

onStart(ITestContext context) - This method get invoked before running all the test

methods that belong to the classes.

onFinish(ITestContext context) - This method get invoked after running all the test  methods that belong to the classes.

transform(ITestAnnotation annotation,

Class testClass,

Constructor testConstructor,

Method testMethod) - This method get invoked in TestNG allows you to

to modify the content of all the annotations at

runtime.

onConfigurationSuccess(ITestResult result) - This method get invoked when the test

                                                                                      starts execute the configuration method is setup and

                                                                                      close .


Implementation Steps :

Step 1:

Create an class file which extends TestListenerAdapter as shown in picture below:

  • Import the TestListenerAdapter from org.testng.ITestListener as shown in Step 1B Picture.

    Eg : - import org.testng.ITestListener;


Step 1A:

Step 1B:


For the TestListenerAdapter class, you are not able to implement the methods directly as shown in the below picture. Because of that you already know how to implement the interface ITestListener which show in the above ITestListener blog Link. If you did not go through the link ITestListener. Please go through it for better implementation of this TestListenerAdapter class.


Step 2:

Right click on ITestListener, go to source, then click to override/Implement Methods and use the methods that you need for the test execution.


Step 3:

Now for retrying or rerunning the failed test cases, the IAnnotationTransformer Interface can be used by implementing the IAnnotationTransformer interface. Right click on IAnnotationTransformer, go to source, then click to override/Implement Methods and use the methods what you need for the test execution.

Step 4:

Then hover over on IAnnotationTransformer, right click on IAnnotationTransformer, go to source, then click to override / implement methods.


Step 5:

The window opens with IAnnotationTransformer Override/Implement Methods:

Step 6:

Select the transform method as shown in the picture below:

Step 7:

Delete the line 16 to write the customized script as per the test needs. To understand better please go through the Link IRetryAnalyzer Interface blog about the steps implemented with the IAnnotationTransformer interface.


Check the below script: Write the code as per the required custom reports.


package com.utilities;

import java.lang.reflect.Constructor;

import java.lang.reflect.Method;

import org.testng.IAnnotationTransformer;

import org.testng.ITestContext;

import org.testng.ITestResult;

import org.testng.Reporter;

import org.testng.TestListenerAdapter;

import org.testng.annotations.ITestAnnotation;


public class TestListenerAdapterternerClassFile extends TestListenerAdapter implements IAnnotationTransformer {


public String getMethodNameFromITestResult(ITestResult iTestResult) {

return iTestResult.getMethod().getConstructorOrMethod().getName();

}


public String getMethodNameFromIAnnotationMethod(Method method) {

return method.getName();

}


// IAnnotationTransformer Interface Implemented Method

@Override

public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {

annotation.setRetryAnalyzer(IRetryAnalyzerClassFile.class);

System.out.println("transform method " + getMethodNameFromIAnnotationMethod(testMethod));

LoggerLoad.info("transform method " + getMethodNameFromIAnnotationMethod(testMethod));

Reporter.log("transform method " + getMethodNameFromIAnnotationMethod(testMethod));

}


// IConfigurationListener Interface implemented Method

@Override

public void onConfigurationSuccess(ITestResult iTestResult) {

System.out.println("onConfigurationSuccess method " + getMethodNameFromITestResult(iTestResult));

LoggerLoad.info("onConfigurationSuccess method " + getMethodNameFromITestResult(iTestResult));

Reporter.log("onConfigurationSuccess method " + getMethodNameFromITestResult(iTestResult));

}


// ITestListener Interface implemented Methods

@Override

public void onStart(ITestContext context) {

LoggerLoad.info("onStart method " + context.getName());

Reporter.log("onStart method " + context.getName());

}


@Override

public void onFinish(ITestContext context) {

LoggerLoad.info("onFinish method " + context.getName());

Reporter.log("onFinish method " + context.getName());

}


@Override

public void onTestStart(ITestResult iTestResult) {

LoggerLoad.info("onTestStart method " + getMethodNameFromITestResult(iTestResult));

Reporter.log("onTestStart method " + getMethodNameFromITestResult(iTestResult));

}


@Override

public void onTestSuccess(ITestResult iTestResult) {

LoggerLoad.info("onTestSuccess method " + getMethodNameFromITestResult(iTestResult));

Reporter.log("onTestSuccess method " + getMethodNameFromITestResult(iTestResult));

}


@Override

public void onTestFailure(ITestResult iTestResult) {

LoggerLoad.info("onTestFailure method " + getMethodNameFromITestResult(iTestResult));

Reporter.log("onTestFailure method " + getMethodNameFromITestResult(iTestResult));

System.out.println("onTestFailure method " + getMethodNameFromITestResult(iTestResult));

}

}


Implementing the TestListenerAdapter class at the Suite Level :  

The TestListenerAdapter class file is specified with a package name as shown in picture below to implement listeners in the TestNG .XML file.


Reports:

You can use the Reporter Log, Extent Report, Allure Report inside the override method to see the results.


Console Output:

To see the message in console output.


Positive test case only:

Note: I have specified the Test2 annotate method 'enabled =false' to make only positive test case console output which had transform method show Test1, Test2 is coming from logger load which is available in transform method.


Retry testcase sample Example from ITransformAnnotation interface:

After executing the TestNG.XML file, refresh the maven project, right click, then go to emailable-report.html under the test-ouput.



Using the Reporter Log :

To see the message in the Emailable report, the reporter logs are used and below is an example.


Note: Please go through the blog link :


Conclusion:

Now you have an idea about the TestListenerAdapter class and its usage with multiple interfaces. A simple TestListener adapter that stores all the tests that were run by listeners makes the test reports in detailed by collecting the skipped, pass test cases, failure messages and the reporter logs etc.. Implement this TestListenerAdapter class combines with multiple listeners to get advanced customized reports of the test scripts.


39 views

Recent Posts

See All
bottom of page