What is TestNG listener and How to implement in Selenium

What is TestNG Listener

In last post, we have discussed WebDriver Listener and hope you understood the actual concept of listeners, if not I will strongly recommend you that please go through WebDriver Listener  also.

This is a very common question also in interviews that what is the actual difference between TestNG Listener and Webdriver Listener.

Let’s discuss now- When we talk about TestNG Listener we will use ITestListener Interface.

This interface has some methods which we need to override as per our requirement.

 

Recently I posted a video on youtube about Listeners hope it will help.

 

For example- take some method of ITestListener

 

 

etc..

 

How to implement TestNG Listener

Its not that tough as compared to WebDriver Listeners we simply have to implement ITestListener interface and override all method as per requirement and its not required that you should have some statements in all methods but yes you should use some of that method Wisely

 

Scenario-

1-If testcase is failing then what action should be performed

2- If testcase is skipped then what should be action requested and so far..

 

 

Step 1- Create a simple class and implement ITestListener listener

 

 

TestNG Listener

 

 

Step 2- Override all unimplemented methods

 

 

TestNG Listener

 

 

Once you implement all your java program will look like

 

 

So let me implement some of the methods and I will start my test.

 

I will override following method onTestStart, onTestSuccess(), onTestFailure()

 

I have written a small test for facebook login and I am failing this testcase forcefully so that we can check how different events will be fired if testcase pass or fail

 

Program

 

 

 Step 3- It not completed yet now we need to modify our xml file that is known as testng.xml and we have to specify this listener and we have to execute that xml file only.

 

We have predefined listeners tag so that we will use today.

 

 

 

so your complete testng.xml file will look like.

 

 

Step 4- Simple run this testng.xml file and analyze the output

 

TestNG Listener
TestNG Listener

 

 

Now if you analyze above output this event automatically getting triggered like once testcase start and testcase failed or testcase pass.

 

19 Comments

  1. saipradeep
  2. Sathiya
  3. Pradeep
  4. Ashish Mathur
  5. jayesh hoondlani
  6. Chandrasekhar Vadde