Listeners in TestNG
Vivek Sharma
Senior Consultant QA - Experience in API and UI Automation Testing, Python behave, Java, rest Assured & AI/ML Testing
If you want to add logs into your test script you can use listeners. Listeners work on action so have below methods.
OnTestStart : Invoked each time before a test will be invoked.
OnTestSuccess : Invoked each time a test succeeds.
OnTestFailure : Invoked each time a test fails
OnTestSkipped : Invoked each time a test is skipped.
OnTestFinish
How to use in your current project
Create Class 'ListenersClass.java'
code like
package com.example.logging;
import org.testng.IClass;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
public class ListenerClass extends TestListenerAdapter {
@Override
public void onTestStart(ITestResult tr) {
log("Test Started....");
}
@Override
public void onTestSuccess(ITestResult tr) {
log("Test '" + tr.getName() + "' PASSED");
Use the above class in your exiting main test class by importing above class.
Add listners in to testng.xml as well
listeners>
<listener class-name="Tru.XXX.logging.ListenerClass" />
</listeners>
Enjoy longing so that you can have better understanding where test fails.