Listeners

What is Listeners?

As the name says, We can make TestNG to listen when a test starts, fails , passes, skips etc. Listeners can be useful for reporting and modify TestNG behavior during run time. There are several interfaces that allow you to modify TestNG’s behavior. It is used in selenium by applying listeners interface. TestNG can generate step by step logs with the help of Listeners. It allows customizing TestNG reports or logs with the help of listeners.

Listeners

Types of Listeners in TestNG

There are many types of listeners which allows you to change the TestNG’s behavior. Below are the TestNG listeners:

  1. IAnnotationTransformer
  2. IConfigurable
  3. IConfigurationListener
  4. IExecutionListener
  5. IHookable
  6. IInvokedMethodListener
  7. IMethodInterceptor
  8. IReporter
  9. ISuiteListener
  10. ITestListener

These interfaces are used in selenium to generate logs or customize the testing reports with the help of Step by Step Scenario’s.

Lets start with ITestListener

So the first question is when we use ITestListener:

  • After successfully completing each test.
  • After each failed test.
  • After each test skipped.
  • After the completion of all tests.

ITestListener has following methods :

  1. OnStart( ): OnStart method is called when any Test starts.
  2. onTestSuccess( ): onTestSuccess method is called on the success of any Test.
  3. onTestFailure( ): onTestFailure method is called on the failure of any Test.
  4. onTestSkipped( ): onTestSkipped method is called on skipped of any Test.
  5. onTestFailedButWithinSuccessPercentage( ): This method is called each time Test fails but is within success percentage.
  6. onFinish( ): onFinish method is called after all Tests are executed.

Lets take a Orange HRM login process and try to implement the ITestListener

Step 1: Create a simple class and implement ITestListener listener

public class ListenersTest implements ITestListener { 
       
}

Step 2: Let’s modify the ListenersTest class

We will modify following methods- onTestFailure, onTestSkipped, onTestStart, onTestSuccess

package testNGFrameWork;

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

public class ListenersTest implements ITestListener {

	@Override
	public void onFinish(ITestContext arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStart(ITestContext arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
		// TODO Auto-generated method stub

	}

	// When Test case get failed, this method is called.
	@Override
	public void onTestFailure(ITestResult Result) {
		System.out.println("The name of the testcase failed is :- " + Result.getName());
	}

	// When Test case get Skipped, this method is called.
	@Override
	public void onTestSkipped(ITestResult Result) {
		System.out.println("The name of the testcase Skipped is :- " + Result.getName());
	}

	// When Test case get Started, this method is called.
	@Override
	public void onTestStart(ITestResult Result) {
		System.out.println(Result.getName() + " :- Test case started");
	}

	// When Test case get passed, this method is called.
	@Override
	public void onTestSuccess(ITestResult Result) {
		System.out.println("The name of the testcase passed is :- " + Result.getName());
	}
}

Step 3: Create another class for the Orange HRM login process automation.

package testNGFrameWork;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class ITestListner_OrangeHRMLoginTC {

	// Test to pass as we need to verify listeners
	@Test
	public void loginTestCase() {

		System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

		// Navigate to Orange HRM login page
		driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login");

		// Enter the user name
		driver.findElement(By.xpath("//input[@id='txtUsername']")).sendKeys("admin");

		// Enter the password
		driver.findElement(By.id("txtPassword")).sendKeys("admin123");

		// Click on login button
		driver.findElement(By.id("btnLogin")).click();

		// Verify home page
		Assert.assertEquals(driver.findElement(By.id("welcome")).getText(), "Welcome Admin");

	}

	// Forcefully failed this test as to verify listener.
	@Test
	public void TestToFail() {
		System.out.println("This method is for test fail");
		Assert.assertTrue(false);
	}
}

Step 4: Now, we implement our created listener in this Orange HRM login test case class i.e. ITestListner_OrangeHRMLoginTC.

There are two different ways to connect to the class and interface-

The first way is to use Listeners annotation is Implement TestNG Listener to your test class as shown as below:

Syntax: @Listeners(Listener_Demo.ListenerTest.class)
package testNGFrameWork;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(testNGFrameWork.ListenersTest.class)

public class ITestListner_OrangeHRMLoginTC {

	// Test to pass as we need to verify listeners
	@Test
	public void loginTestCase() {

		System.setProperty("webdriver.chrome.driver",
				"F:\\Software\\programming Software\\Selenium With Java\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

		// Navigate to Orange HRM login page
		driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login");

		// Enter the user name
		driver.findElement(By.xpath("//input[@id='txtUsername']")).sendKeys("admin");

		// Enter the password
		driver.findElement(By.id("txtPassword")).sendKeys("admin123");

		// Click on login button
		driver.findElement(By.id("btnLogin")).click();

		// Verify home page
		Assert.assertEquals(driver.findElement(By.id("welcome")).getText(), "Welcome Admin");

	}

	// Forcefully failed this test as to verify listener.
	@Test
	public void TestToFail() {
		System.out.println("This method is for test fail");
		Assert.assertTrue(false);
	}
}

So Second way to use Listeners annotation is XML as shown below:

Although approach 1 is more than enough to get you started, but it’s not recommended way to use Listeners, because here you need to add this @Listeners section to each of your test case classes. So what we do is, we create a TestNG Suite XML and then add up the listeners section to this suite XML file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
	<listeners>
		<listener class-name="testNGFrameWork.ListenersTest"/>
	</listeners>
	<test thread-count="5" name="Listeners Test">
		<classes>
			<class name="testNGFrameWork.ITestListner_OrangeHRMLoginTC" />
		</classes>
	</test> <!-- Listeners Test -->
</suite> <!-- Suite -->

Next
TestNG
Home Page

Leave a comment

Design a site like this with WordPress.com
Get started