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
1-@Override public void onFinish(ITestContext arg0) { } 2- @Override public void onStart(ITestContext arg0) { } 3- @Override public void onTestFailure(ITestResult arg0) { }
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
Step 2- Override all unimplemented methods
Once you implement all your java program will look like
package testngDemo; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; public class ListenerDemoExample 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 } }
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
package testngDemo; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; import org.testng.annotations.Test; public class ListenerDemoExample implements ITestListener{ // This is dummy tescase for facebook login @Test public void loginFB(){ WebDriver driver=new FirefoxDriver(); driver.get("http://www.facebook.com"); driver.manage().window().maximize(); driver.findElement(By.id("email")).sendKeys("mukesh@facebook.com"); driver.findElement(By.id("wronglocator")).sendKeys("dont-tell"); driver.findElement(By.id("loginbutton")).click(); } @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) { System.out.println("Screen shot captured====="+arg0.toString()); } @Override public void onTestSkipped(ITestResult arg0) { // TODO Auto-generated method stub } @Override public void onTestStart(ITestResult arg0) { // TODO Auto-generated method stub System.out.println("TestCase started====" +arg0.toString()); } @Override public void onTestSuccess(ITestResult arg0) { // TODO Auto-generated method stub System.out.println("Congrates Testcase has been passed===="+ arg0.toString()); } }
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.
<listeners> <listener class-name="testngDemo.ListenerDemoExample"/> </listeners>
so your complete testng.xml file will look like.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="none"> <listeners> <listener class-name="testngDemo.ListenerDemoExample"/> </listeners> <test name="Test"> <classes> <class name="testngDemo.ListenerDemoExample"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
Step 4- Simple run this testng.xml file and analyze the output
Now if you analyze above output this event automatically getting triggered like once testcase start and testcase failed or testcase pass.
Deepak Kumar says
Hi Sir,
How to create pdf reports using listeners
Mukesh Otwani says
Hi Deepak,
What is your requirement?
Thanks
Mukesh
Gaurav Khurana says
So there are 4 ways of logging now i have learned so far from the site.
Can you please correct me if i am wrong somewhere or anything to be added. Its like a summary
1) Reporter.log
A simple print statement
2) testNG
The most easiest way since you just have to extend the interface and you get advance logging
3) WebDriverEventliStener
This a little complex since you have yo create 2 classes . One extending other class and use different object than driver but logging details are very good liked
you click on which link, you navigated where
4) Log4j
external library, it has 4 different things under it and has its own syntax and classes
Mukesh Otwani says
You missed Extent report 🙂
Santosh Adhikari says
Which one will be best ?
Mukesh Otwani says
Hi Santosh,
Extent Report is the best way of reporting. You can add WebDriverEventListener logs, testng logs into extent report only
rajeev singh says
Hi Mukesh,
i would like to learn a lot from u in the coming future, plz let me know if there is any selenium job in ur city, Banglore, i would like to move to Banglore and would like to learn more and more from you.
saipradeep says
Hi Mukesh,
Would you let us know how to create a .bat file to run the webdriver script by doubclicking on the bat file?Searched entire youtube but found no luck.
Mukesh Otwani says
Hi Pradeep,
Visit this video for the same https://www.youtube.com/watch?v=1e8I3A49ERc
Sathiya says
Hi Mukesh,
I am completely new to java and automation world. But after watching your videos , i never felt that i am new and i am still learning. Your videos are awesome to groom the new comers. Whenever i face any difficulties to understand any concepts, i use to search some videos in your name only.
Mukesh Otwani says
Thanks a ton Sathiya, let me know if any help from my side.
Mahesh Bharti says
Sir please share a video on Framework from scratch.
Mukesh Otwani says
Hi Mahesh,
Below course will help http://learn-automation.usefedora.com/courses/selenium-frameworks-and-selenium-question-answers
Pradeep says
no words from my side about your hardwork to serve the Software industry. awesome mukesh
Mukesh Otwani says
Hi Pradeep,
Thanks a ton mate :). Keep visiting and let me know if any help from my side.
aashish mathur says
hi mukesh, Thanks for the video.
You mentioned that in next video you will cover TestListner Adaptor. I am unable to find that video. Can You share it please.
Mukesh Otwani says
Hi Aashish,
Video will upload soon.
Ashish Mathur says
Hi Mukesh, People in my office know you by your name name now because of such these videos 🙂
In the beginning of this video you mentioned by your next post about webdriver Listners and how to extend ListnerAdaptors. I am unable to find that video.
Mukesh Otwani says
Hey Ashish,
Thanks a ton 🙂 You made my day.
I have not created video for WebDriver listener but post is available you can have a look. Will update video soon.
http://learn-automation.com/what-is-listeners-in-selenium-webdriver/
jayesh hoondlani says
great work big bro
Mukesh Otwani says
Thanks
Chandrasekhar Vadde says
If i just say thanks, really that is very small to you 8055.
You are rallying great …. sharing this valuable information for free.
_/\_ ….. a big Thank you (what i can do more than this 🙂 )
Mukesh Otwani says
Hi Chandra,
You made my day thank you so much.