chapter 27: TestNG Listeners

TestNG provide us the @Listeners annotation which listens to every event that occurs in a selenium code.Listeners are activated either before the test or after the test case. There are 2 types of listeners

  • Webdriver Listeners
  • TestNG Listeners

In this chapter we will study TestNG Listeners.

What is Listeners in TestNG?

It is an interface that modifies the TestNG behavior. It is used in selenium by implementing Listeners Interface. It allows customizing TestNG reports or logs.

Types of Listeners in TestNG:

There are many types of listeners available in TestNG

 for example IAnnotationTransformer, IAnnotationTransformer2, IConfigurable, IConfigurationListener, IConfigurationListener2, IExecutionListener, IHookable, IInvokedMethodListener, IInvokedMethodListener2, IMethodInterceptor, IReporter, ISuiteListener, ITestListener.

As far as testing is concern only few can be used effectively such as :

ISuiteListener: It has two method in it onStart() & onFinish(). Whenever a class implements this listener, TestNG pledge the end-user that it will invoke the methods onStart() and onFinish() before and after running a TestNG Suite. So before TestNG picks up your suite for execution, it first makes a call to onStart() method and runs whatever has been scripted in this method. In a similar way, it again makes a call to onFinish() method after a suite has been run.

ITestListener: The working of this listener is also exactly the same as ISuiteListerner but the only difference is that it makes the call before and after the Test not the Suite. It has seven methods in it.

onFinish(): Invoked after all the tests have run and all their Configuration methods have been called.

onStart(): Invoked after the test class is instantiated and before any configuration method is called.

onTestFailedButWithinSuccessPercentage: Invoked each time a method fails but has been annotated with success Percentage and this failure still keeps it within the success percentage requested.

onTestFailure: Invoked each time a test fails.

onTestSkipped: Invoked each time a test is skipped

onTestStart: Invoked each time before a test will be invoked.

onTestSuccess: Invoked each time a test succeeds.

 IInvokedMethodListener: The working of this listener is also exactly the same as ISuiteListerner & ITestListerner and the only difference is that it makes the call before and after every Method. It has only two methods in it.

afterInvocattion(): Invoke after each method

beforeInvocation(): Invoke before each method

Steps to create a TestNG Listener

Step 1.Create class “listenerTestng” that implements ‘ITestListener’. Move the mouse over redline text, and Eclipse will suggest you quick fixes as shown below.

Just click on import ”ITestListener”(org.testng)..check the code below:

package listener_demo;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class listenerTestng  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				
    }		
@Override		
  public void onTestFailure(ITestResult arg0) {					
  // TODO Auto-generated method stub				
     }		
 @Override		
  public void onTestSkipped(ITestResult arg0) {					
        // TODO Auto-generated method stub				
     }		
@Override		
   public void onTestStart(ITestResult arg0) {					
  // TODO Auto-generated method stub				
      }		
@Override		
  public void onTestSuccess(ITestResult arg0) {					
   // TODO Auto-generated method stub				
      }		
}

Let’s modify the ‘listenerTestng’ class. we will modify following methods-

onTestSuccess, onTestSkipped,onTestFailure

The modification is simple. We just print the name of the Test.Logs are created in the console. It is easy for the user to understand which test is a pass, fail, and skip status.

After modification, the code looks like-

package listener_demo;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class listenerTestng  implements ITestListener{
@Override		
public void onFinish(ITestContext Result) 					
	    {		
	                		
	    }		
  @Override		
 public void onStart(ITestContext Result)					
	    {		
	            		
	    }		

@Override		
public void onTestFailedButWithinSuccessPercentage(ITestResult Result)					
	    {		
	    		
	    }		
// 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());					
	    }		
  @Override		
public void onTestStart(ITestResult Result)					
	    {		
	   				
	    }		
// 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 2. Create another class “TestCases” for the login process automation. Selenium will execute this ‘TestCases’ to login automatically.

package listener_demo;
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 TestCases {
WebDriver driver= new ChromeDriver();					// Test to pass as to verify listeners .		
@Test		
	public void Login()				
	{		
	    driver.get("http://newtours.demoaut.com/");					
	    driver.findElement(By.name("userName")).sendKeys("Testology");							
	    driver.findElement(By.name("password")).sendKeys("Testology12");							
	    driver.findElement(By.cssSelector(
	  		  "input[name='login'][value='Login']")).click();					
	}		

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

Step 3. Next, implement this listener in our regular project class i.e. “TestCases”.use Listeners annotation (@Listeners) as shown below:

@Listeners(listener_demo.listenerTestng.class)

We use this method in our class

 Finally the class ” TestCases ” looks like after using Listener annotation:

package listener_demo;
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(listener_demo.listenerTestng.class)
public class TestCases {
	WebDriver driver= new ChromeDriver();					
// Test to pass as to verify listeners .		
	@Test		
	public void Login()				
	{		
	    driver.get("http://newtours.demoaut.com/");					
	    driver.findElement(By.name("userName")).sendKeys("Testology");							
	    driver.findElement(By.name("password")).sendKeys("Testology12");							
	    driver.findElement(By.cssSelector(
	  "input[name='login'][value='Login']")).click();					
	}		
// Forcefully failed this test as to verify listener.		
	@Test		
	public void TestToFail()				
	{		
	    System.out.println("This method to test fail");					
	    Assert.assertTrue(false);			
	}		
}

Step 4. Execute the “TestCases ” class.Methods in class “listenerTestng ” are called automatically according to the behavior of methods annotated as @Test.

Step 5. Verify the Output that logs displays at the console.

Output of the ‘TestCases’ will look like this:

In this way we can create multiple Listener classes.