Listeners

IMethodInterceptor examples to reorder list of test methods

IMethodInterceptor is used to alter/modify the list of test methods to be executed by TestNG.

When we implement IMethodInterceptor interface which has just one method to implement 'intercept(java.util.List methods, ITestContext context)', using this method we can reorder the list of test methods and return List of IMethodInstance which TestNG runs. TestNG will now run these test methods in the same order which is found in the returned list.

Listeners in JMeter

Listeners facilitates viewers to view responses generated by samplers in the form of tables, graphs, trees or simple text in some log files and also give pictorial access to the data collected by JMeter about those test cases as a Sampler component of JMeter is executed.

To view the details of sampler requests and responses, add either "View Results Tree" or "View Results in table" Listeners to a test plan which displays basic HTML and XML representations of the response. To view the response time graphically, add Graph Results which plots the response times on a graph.

Customize TestNG html Report

When ever we execute tests with TestNG, it will generate a default html report with basic information.

TestNG has provided an ability to implement 'IReporter' an interface which can be implemented to generate a customized report by users. It has 'generateReport()' method which will be invoked after all the suite has completed its execution and gives the report into the specified output directory.

Syntax for generateReport() method:

Webdriver Event Listener example

Webdriver events are helpful to view the events triggered by webdriver. It also plays an important role in analyzing results and helps us in debugging issues if we encounter any.

Selenium webdriver has ability to track different events such as 'beforeNavigateTo' , 'afterNavigateTo' , 'beforeClickOn' , 'afterClickOn', 'onException' and so on. When ever we develop test scripts we can write our own implementation for handling events during the execution.

Take Screenshot and place it in a folder with Test Class name

In this tutorial, we will see taking a screen shot with test name and placing it in a folder by creating a folder with Test Class Name.

As we are taking screen shots on failure, we need to add logic in creating a screen shot and naming it with test name and place it in its test class name respectively.

We can get the test class name using 'result.getInstanceName()'. Test class name looks some thing like "com.pack.sample.HomePage". But we need to create a folder name with test class name i.e 'HomePage'.

Logging with TestNG using Listeners

In TestNG we can generate logs with the help of Listeners. Listeners implement the interface "org.testng.ITestListener". There are methods which gives you a real time information.

You can either extend 'TestListenerAdapter' or implement Interface 'ITestListener' which is a listener for test running.

What are Listeners in TestNG : -