In TestNG, there is a interface called TestNGListener which contains multiple interfaces. Lets learn about the ITestListener feature that will listen to the behavior of the tests during the execution of test cases, such as when the test gets started,pass or failed or skipped the testcases. You can implement this listener to monitor the test scripts. In the following example, we will learn about the use of ITestListener interface and will learn to implement in the testing framework.
Common TestNG Listener Interfaces :
Let's see the details about the ITestListener interface Implementations in the class file and XML file for the test execution.
Listeners can implement in two ways in TestNG:
At the Class level: Annotate the listener on each class in the test script. Â
Eg: @Listeners(ITestListenerFile.class)
At the Suite level: Right click on project and convert to TestNG to create the XML file.
Now the XML file has all the class names after converting to TestNG XML
file .Implement listeners at suite level and remove @Listener from the
test script which you have already mention at class level.
@Listeners(ITestListenerFile.class)
Eg: <listeners>
<listener class-name="com.utilities.ITestListenerFile" />
</listeners>
Key Features of ITestListener :Â
It provides methods to perform actions at different stages of test execution.
Useful for custom logging, generating reports.
Works at the level of individual test cases or test suite level.
Methods in ITestListener :Â
Here are the key methods that you can override:
onTestStart(ITestResult result)
Called this onTestStart method when a test method starts execution.
onTestSuccess(ITestResult result)
     Called this onTestSuccess method when a test method finishes successfully.
onTestFailure(ITestResult result)
Called this onTestFailure when a test method fails and we can use this method to
capture screenshots, logs and error details.
onTestSkipped(ITestResult result)
Called this onTestSkipped method when a test method is skipped due to
internet issue and some other external conditions causing the test to be skipped.
onTestFailedButWithinSuccessPercentage(ITestResult result)
Called this onTestFailedButWithinSuccessPercentage method when the test method
get failed but it is within the success percentage. Used for tests where partial success
is acceptable.
onStart(ITestContext context)
Called this onStart method before any test methods in a test class start executing.
onFinish(ITestContext context)
Called this onFinish method after all the test methods in a test class have
finished execution. Used to generate results and final test reports.
Note:-
ITestResult       - It is an interface that contains the result of the current
test script.
result                - The argument 'result' is the instance of the ITestResult.
ITestContext    - It is a interface that defines the current test run information.Â
context             - It contains data such as test name, parameters, suite
and more.
Implementation Steps :
Step 1:
Create an class file which extends Base class contains the browser, driver details and implements the ITestListener Interface as shown in picture below:
Import the ITestListener interface from org.testng.ITestListener.
Eg : - import org.testng.ITestListener;
Step 2 :
Then hover over on ITestListener, right click on ITestListener, go to source, then click
to override / implement methods.
Step 3:
The window opens with ITestListener Override/Implement Methods:
Step 4:
Select the methods as shown in the blow picture:
Step 5:
Remove these two lines in all the below methods to write the custom reports.
Step 6:
The class file has the empty method as shown in the picture.
Check it here: Write the code as per the required custom reports.
package com.utilities;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.Reporter;
import com.testBase.BaseClass;
public class ITestListernerFile extends BaseClass implements ITestListener {
public static String getMethodName(ITestResult result) {
return result.getMethod().getConstructorOrMethod().getName();
}
@Override
public void onTestStart(ITestResult result) {
LoggerLoad.info(getMethodName(result) + " Test is starting.");
System.out.println(getMethodName(result) + " Test is starting.");
Reporter.log(getMethodName(result) + " Test is starting.");
}
@Override
public void onTestSuccess(ITestResult result) {
LoggerLoad.info(getMethodName(result) + " Test is succeed.");
System.out.println(getMethodName(result) + " Test is starting.");
Reporter.log(getMethodName(result) + " Test is succeed.");
}
@Override
public void onTestFailure(ITestResult result) {
LoggerLoad.info(getMethodName(result) + " Test is failed.");
Reporter.log("onTestFailure"Â + result.getName());
}
@Override
public void onTestSkipped(ITestResult result) {
LoggerLoad.info(getMethodName(result) + " Test is Skipped.");
Reporter.log("onTestSkipped"Â + result.getName());
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
LoggerLoad.info(getMethodName(result) + " Test Failed within Success Percentage.");
Reporter.log("onTestFailedButWithinSuccessPercentage"Â + result.getName());
}
@Override
public void onTestFailedWithTimeout(ITestResult result) {
LoggerLoad.info(getMethodName(result) + " Test Failed with Timeout.");
Reporter.log("onTestFailedWithTimeout"Â + result.getName());
}
@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());
}
}
Implementing the ITestListener at Class Level :
Annotate the listeners in each class in the test script as shown in the picture below for executing the test at only class level.
Implementing the ITestListener at the Suite Level :Â Â
The ITestListener class file is specified with package name as show 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 the console output.
Emailable-Report HTML:
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 is used and below is the example.
Pass Test Case HTML Report:
Fail Test Case HTML Report:
Conclusion:
Now you have an idea about ITestListener. Listeners makes the automation testing simpler by collecting the skipped, pass test cases, failure messages and the reporter logs etc.,
Implement this ITestListener, combining with multiple listeners to get advanced customs reports of the test scripts.