What is ITestListner interface and its Use in Selenium

Test Automation
1

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used:

1- IAnnotationTransformer
2- IAnnotationTransformer2
3- IHookable
4- IInvokedMethodListener
5- IMethodInterceptor
6- IReporter
7- ISuiteListener
8 – ITestListener

In this blog, I will be covering ITestListner, we will learn how to use this particular listner.

When you implement one of these interfaces, you can let TestNG know about it with either of the following ways:
Using -listener on the command line.

a- Using with ant.
b- Using in your testng.xml file.
c- Using the @Listeners annotation on any of your test classes.
d- Using ServiceLoader.

In Below code, I have created a class Reporting_Listner which i have used ItestListner by extending it. Once after writing this syntax and class name, Java editor(Eclipse in this case) will ask to add method defined for this listner, on clicking yes it well add ITestListner method, method defined in ITestListner are-

1- onFinish
2- onStart
3- onTestFailureButWithSuccessPercentage
4- onTestFailure
5- onTestSkipped
6- onTestStart
7- onTestSuccess

In below code, I have customized onTestFailure and added System.out.println syntax, same will trigger whenever failure happen. Console will print after execution- On Test Skip-Inside Listner class as i have deliberately failed a test case, Refer second code snippet.

package TestNG_Classes;

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

public class Reporting_Listners implements ITestListener{

public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub

}

public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
//System.out.println("On Test Pass-Inside Listner class");
}

public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("On Test Failure-Inside Listner class");

}

public void onTestSkipped(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("On Test Skip-Inside Listner class");
}

public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub

}

public void onStart(ITestContext context) {
// TODO Auto-generated method stub

}

public void onFinish(ITestContext context) {
// TODO Auto-generated method stub

}

}

Second code snippet

After above listner class customization, call it in your class where test cases are written, Method which have @Test annotation.

Deliberately, i have failed Test2, which will fail this test and trigger execution of listner syntax onTestFailure method.

package TestNG_Classes;
import junit.framework.Test;

import junit.framework.Assert;
//import org.testng.annotations.AfterTest;
//import org.testng.annotations.BeforeSuite;
//import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Listeners;

@Listeners(TestNG_Classes.Reporting_Listners.class)
public class TestNGWalkthrough
{

@org.testng.annotations.Test(priority=1)
public void Test1()
{
System.out.println("I am in Test1");
}

@org.testng.annotations.Test(priority=2)
public void Test2()
{
System.out.println("I am in Test2");
Assert.fail();

}

@org.testng.annotations.Test(priority=3)//, dependsOnMethods={"Test2"})
public void Test3()
{
System.out.println("I am in Test1");
}

}

Output of above code- Test 2 is failed and listner method has beed triggered on its failure

Summary-

Above code is an example, similarly you can call any piece of code in listner class method, on test pass, fail or Skip method.

It is recomended to call screenshot capture, or other log generating methods in listners methods.

Listners make test automation framework more effective as it helps in writing lesser code and can perform many activities by simply listening to test execution.

Listners can be called across multiple classes, In above example i have implemented for one class only

Its Prashant Chaudhary, I have 10+ years of experience in software test automation, performance & process automation with various commercial and open source tools. This blog shares my experience on the topic of Automation. The content published on my blog is solely mine, it does not express the views of any of my employers (current or previous).
1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Selenium Basic
1
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Test Automation
How to Capture Screenshot in Selenium Webdriver-3 different ways

Abstract– Capturing screenshot is an essential feature of a test automation framework, They help in defect analyses once after test execution is over. In this blog post we will see how to capture screenshot, there are different ways to capture screenshots. Which includes- 1- Capture full Page 2- Capturng full …

Test Automation
What is Headless browser automation

Hello Readers, In this post we will read about headless browser automation and how to use or invoke different headless browser using selenium. First of all lets understand What is a headless browser? A headless browser is a web-browser without a GUI-Graphical User Interface. The major difference with browser having …

Shares