- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What do you mean by Listeners in TestNG?
TestNG Listeners have the capacity to listen to a specific incident. It is basically an interface that changes the nature of the system. TestNG Listeners are used for logging purposes and creating reports.
There are two Listeners in Selenium. They are listed below −
TestNG Listeners.
WebDriver Listeners.
TestNG can be configured with the Listeners which can change the default behavior of the TestNG. TestNG Listeners are known as iTestListener (a TestNG interface). A java class implements the iTestListeners and at the same time overrides its methods. Each of these methods trigger an event.
The functions of TestNG listeners are listed below.
iSuiteListener − This consists of methods onStart() and onFinish(). As a java class implements this listener, TestNG shall call the onStart() and onFinish() methods at the beginning and at the end of the TestNG suite respectively. So before triggering our test suite for execution, the precondition onStart() test method is picked for execution by TestNG. Similarly after the execution of the test suite, post condition onFinish() test method is picked by TestNG.
iTestListener − This is similar in functionality as iSuiteListener. The only difference is that iTestListener is applicable before and after a Test and not Suite. It has seven methods.
onStart − This test method is invoked when the test begins.
onFinish − This test method is invoked after all the tests have completed execution.
onTestSuccess − This test method is invoked every time a test is successful.
onTestFailure − This test method is invoked every time a test is unsuccessful.
onTestSkipped − This test method is invoked every time a test is bypassed.
onTestFailedButWithinSuccessPercentage − This test method is invoked every time a test method annotated with successPercentage is not successful. But within the requested success percentage.
iInvokedMethodListener − This is similar in functionality as iTestListener and iSuiteListener. The only difference is that iInvokedMethodListener is applicable before and after every test method. It has two methods.
afterInvocation − This is called after every test method.
beforeInvocation − This is called before every test method.
Example
With iTestListener.
package tutorialspoint; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; //ITestListener interface which implements Testng listeners public class Listener implements ITestListener { @Override public void onTestStart(ITestResult result) { // TODO Auto-generated method stub } @Override public void onTestSuccess(ITestResult result) { // TODO Auto-generated method stub } @Override public void onTestFailure(ITestResult result) { // TODO Auto-generated method stub //screenshot code System.out.println ("Failed test case name" + result.getName()); } @Override public void onTestSkipped(ITestResult result) { // TODO Auto-generated method stub } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult result) { // TODO Auto-generated method stub } @Override public void onStart(ITestContext context) { // TODO Auto-generated method stub } @Override public void onFinish(ITestContext context) { // TODO Auto-generated method stub } }
In the Listener java class, onTestFailure() will print the name of the test method which has failed with the help of result.getName() method.
We can add this Listener class to the Java class file in two ways.
Listener tag in testng xml file.
Implement Listener to the test case class.
Example
With Listener tag in testng xml file.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Tutorialspoint Test"> <listeners> <listener class-name="tutorialspoint.Listener"/> </listeners> <test name = "Test Cycle 1"> <classes> <class name = "TestQA" /> </classes> </test> </suite>
Inside the <listeners> tag we have mentioned the path of the Listener class.
Example
By implementing TestNG listener to the test class.
package TestTutorials; import org.testng.annotations.Test; // This code will implement TestNG listeners @Listeners(tutorialspoint.Listener.class) public class TestCase { @Test public void Login() { System.out.println("Login is successful"); } }
- Related Articles
- What do you mean by timeOut in TestNG?
- What do you mean by starch?
- What do you mean by adolescence?
- What do you mean by heat?
- What do you mean by tissue?
- What do you mean by environment?
- What do you mean by Garbage?
- What do you mean by Propagation?
- What do you mean by Silk?
- What do you mean by Sex?
- What do you mean by disease?
- What do you mean by Nutrients?
- What do you mean by Obesity?
- What do you mean by Ruminants?
- What do you mean by Roughage?
