What is Webdriver Listeners-
Hello Welcome to Selenium tutorial in this post we will talk about WebDriver Listener,EventFiringWebDriver and WebDriverEventListener in detail.
I know all of you might have heard of Listeners but what exactly Listeners is let us discuss today.
In general, terms, Listeners are whom that listen to you and my favorite quotes is “Be a better listener”.
If you talk about Webdriver Listener so you should make a note of some classes and interfaces that we will use so will talk about it.
1- WebDriverEventListener – This is an interface, which have some predefined methods so we will implement all of these methods.
2-EventFiringWebDriver- This is an class that actually fire Webdriver event.
Why we are using Webdriver Listeners
If you talk about Webdriver we are doing some activity like type, click, navigate etc this is all your events which you are performing on your script so we should have activity which actually will keep track of it.
Take an example if you perform click then what should happen before click and after click.
To capture these events we will add listener that will perform this task for us.
How to implement Listener in our Script
Program for what is listeners in selenium webdriver
Step 1- Create a new Class that will implement WebDriverEventListener methods
package listerDemo; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.events.WebDriverEventListener; public class ActivityCapture implements WebDriverEventListener { @Override public void afterChangeValueOf(WebElement arg0, WebDriver arg1) { } @Override public void afterClickOn(WebElement arg0, WebDriver arg1) { System.out.println("After click "+arg0.toString()); } @Override public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) { System.out.println("After FindBy "+arg0.toString()); } @Override public void afterNavigateBack(WebDriver arg0) { System.out.println("After navigating back "+arg0.toString()); } @Override public void afterNavigateForward(WebDriver arg0) { System.out.println("After navigating forword "+arg0.toString()); } @Override public void afterNavigateTo(String arg0, WebDriver arg1) { System.out.println("After navigating "+arg0.toString()); System.out.println("After navigating "+arg1.toString()); } @Override public void afterScript(String arg0, WebDriver arg1) { } @Override public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) { } @Override public void beforeClickOn(WebElement arg0, WebDriver arg1) { System.out.println("before click "+arg0.toString()); } @Override public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) { System.out.println("before FindBY "+arg0.toString()); } @Override public void beforeNavigateBack(WebDriver arg0) { System.out.println("Before navigating back "+arg0.toString()); } @Override public void beforeNavigateForward(WebDriver arg0) { System.out.println("Before navigating Forword "+arg0.toString()); } @Override public void beforeNavigateTo(String arg0, WebDriver arg1) { System.out.println("Before navigating "+arg0.toString()); System.out.println("Before navigating "+arg1.toString()); } @Override public void beforeScript(String arg0, WebDriver arg1) { } @Override public void onException(Throwable arg0, WebDriver arg1) { System.out.println("Testcase done"+arg0.toString()); System.out.println("Testcase done"+arg1.toString()); } }
Let’s Discuss one of these methods
@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {
System.out.println(“After click “+arg0.toString());
}
In above method we are simply printing on console and this method will automatically called once click events done. In same way you have to implement on methods.
Note- We generally use Listener to generate log events
Step 2- Now create your simple script, create EventFiringWebDriver object, and pass your driver object.
EventFiringWebDriver event1=new EventFiringWebDriver(driver);
Step 3- Create an object of the class who has implemented all the method of WebDriverEventListener so in our case ActivityCapture is a class who has implemented the same.
ActivityCapture handle=new ActivityCapture();
Step 4- Now register that event using register method and pass the object of ActivityCapture class
event1.register(handle);
We are done now using event1 object write your script so now let us implement the same
Implementation of Webdriver listener
package testcases; import listerDemo.ActivityCapture; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.events.EventFiringWebDriver; public class ListnerDemo { public static void main(String []args){ System.out.println("Started"); WebDriver driver=new FirefoxDriver(); EventFiringWebDriver event1=new EventFiringWebDriver(driver); ActivityCapture handle=new ActivityCapture(); event1.register(handle); event1.navigate().to("http://www.facebook.com"); event1.findElement(By.id("email")).sendKeys("asdsadsa"); event1.findElement(By.id("loginbutton")).click(); event1.quit(); event1.unregister(handle); System.out.println("End"); } }
Output-
Please comment below if you have any issue in Selenium. Thanks for visiting my blog keep in touch.
Bye.
Navneet Singh says
Hi Mukesh,
It would be very helpful if you post a video as well on this topic. I think there is no video uploaded on this topic. If it’s there could you please provide the link.
Thanks
Mukesh Otwani says
Hi Navneet,
Thanks for your reminder…:)
I’ll post it soon
soni says
Hi Mukesh,
Your videos are helping us so much….
Mukesh Otwani says
You’re welcome…:)
viki says
do you have c# code of the same ?
Mukesh Otwani says
Hi Viki,
May be this link will help you…
Gaurav Khurana says
Firstly i felt lazy to type the first program but finally i started writing it.
Best part was as soon as you type implements WebDriverEventListner, eclipse give you the option to implement all the unimplemented methods of the interface
I clicked on it and it has written all the fucntions fo that class.
Thanks for such facilities and making the logic so simple.
Coudl you share some shortcoming of these classes WebDriverEventListener and EventFiringWebdriver , So that we can think of why they should not be used in certain situation..
Till now as per my observation
1) reporter.log is simplest to implement
2) This one. WebDriverEventListener
3) Log4j the most complicated
Mukesh Otwani says
Hi Gaurav,
They mainly used for reporting and some special event as well. Let’s say after every click I want to generate log in that case we use this.
Nikhil says
Mukesh where eaxctly are listeners used?
Lajis says
Hi Nikhil,
It is mainly used for reports and logs.
hemant says
Thanks Mukesh youe site is really helpful..
Mukesh Otwani says
Thanks Hemant
Vishwaa says
Hi Mukesh,
Will you please let me know how to implement Reflection in selenium WebDriver.
Mukesh Otwani says
Hi Vishwaa,
I will post article on this soon.
Lokesh Sharma says
Hi Mukesh, I found your videos and article very helpful.
Mukesh Otwani says
Thanks Lokesh. Keep in touch.
Priyanka says
Hi Mukesh….Its a wonderful article. I m getting a lil confused. Can you plz explain me step by step i.e how are the two codes are interconnected . event1.navigate().to(“http://www.facebook.com”)
Its opening facebook page but how exactly it is workingm not geeting. Pls help
Mukesh Otwani says
Hi Priyanka,
In this case we are attaching listerner with our code.
Mithilesh Singh says
Hi Priyanka,
This communication is possible just because of register() method which we are calling using the object of EventFiringWebDriver class and passing object of Listener class.
Since We have already overridden those methods which we are using in current class so as soon as we call that method it shows implemented log message.
Priyaranjan says
Every time I get an issue, I visit your site or mail you. That’s really great of you to answer them all promptly. Your videos really works in understanding. Thanks a lot 🙂
Mukesh Otwani says
Thanks mate 🙂 Keep visiting.
Aruna says
Hi Sir,
Your vedios and tutorials are awesome. Everything is in detailed. Its very very useful for me….great job
Thankyou.
Mukesh Otwani says
Hey Aruna,
Welcome and thanks for nice feedback 🙂 Let me know if any help from my side.
yes aruna says
Nice comment with nice picture
Mukesh Otwani says
Thanks Aruna 🙂
Nawaz says
Could you plz add a video on the same WebDriver Listners.
Thanks in Advance.
Mukesh Otwani says
Yes Nawaz will upload soon.
Mahesh says
How webdriver listeners are different from TestNG listeners and when to use what and which is best ?
Mukesh Otwani says
Hey Mahesh,
TestNG listener for test related events.
WebDriver listener for driver events and we should use both for maximum output.
yogi says
Hi Mukesh Otwani,
good article, it helps a lot, Please upload how to handle javascript alert dynamically and automatically for entire web application using listeners in selenium webdriver.
Mukesh Otwani says
Hey Yogi yes will try to upload soon.
sujay kumar says
Hi Mukesh,
Very good explanation. Your articles help us to learn advanced topics in easy way. Keep your good work.Many thanks.
Mukesh Otwani says
Hi Sujay,
Welcome mate 🙂 keep visiting.
Reshma Sultana says
Hi Mukesh,
Could you please upload a video tutorial of Webdriver Listener as you have done for TestNG Listener? Then it would be easy for me to understand properly. Thank you.
Mukesh Otwani says
Hi Reshma,
Yes sure will try to upload video on the same.
saikiran says
Hi Mukesh,
I am getting error at line(20) ActivityCapture handle=new ActivityCapture();
when i mouse hover it doesn’t show any options. Is there any jar we need to download and add that to our project.
Mukesh Otwani says
Hi Sai,
ActivityCapture is the class that we have created which implements the listener.
Gaurav Khurana says
@Saikiran you need to create 2 programs as mentioned above. 1st program has the ActivityCapture class whose object we are creating in the second program. SO check if you miss to create the first program or forgot the import statement in the second class
Ram Narayan says
Thanks for detailed explanation … Mean it ….
Mukesh Otwani says
Thanks Ram
Ramesh says
Thank you so much. Clearly mentioned each and every thing about listeners and it implementation in Framework.:)_
Mukesh Otwani says
Hi Ramesh,
Thank you. Glad to know it helped 🙂 Keep visiting.