Advance Selenium

What is Listeners and EventFiringWebDriver in Selenium Webdriver

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”.

what is listeners in selenium webdriver

what is listeners in selenium webdriver

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-

what is listeners in selenium webdriver

what is listeners in selenium webdriver

Please comment below if you have any issue in Selenium. Thanks for visiting my blog keep in touch.

Bye.

 

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

42 thoughts on “What is Listeners and EventFiringWebDriver in Selenium Webdriver

  1. 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

    1. Hi Navneet,

      Thanks for your reminder…:)
      I’ll post it soon

  2. soni says:

    Hi Mukesh,
    Your videos are helping us so much….

    1. You’re welcome…:)

  3. viki says:

    do you have c# code of the same ?

    1. Hi Viki,

      May be this link will help you…

  4. 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

    1. 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.

  5. Nikhil says:

    Mukesh where eaxctly are listeners used?

    1. Lajis says:

      Hi Nikhil,

      It is mainly used for reports and logs.

  6. hemant says:

    Thanks Mukesh youe site is really helpful..

  7. Vishwaa says:

    Hi Mukesh,

    Will you please let me know how to implement Reflection in selenium WebDriver.

    1. Hi Vishwaa,

      I will post article on this soon.

  8. Lokesh Sharma says:

    Hi Mukesh, I found your videos and article very helpful.

    1. Thanks Lokesh. Keep in touch.

  9. 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

    1. Hi Priyanka,

      In this case we are attaching listerner with our code.

    2. 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.

  10. 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 🙂

    1. Thanks mate 🙂 Keep visiting.

  11. Aruna says:

    Hi Sir,
    Your vedios and tutorials are awesome. Everything is in detailed. Its very very useful for me….great job
    Thankyou.

    1. Hey Aruna,

      Welcome and thanks for nice feedback 🙂 Let me know if any help from my side.

    2. yes aruna says:

      Nice comment with nice picture

  12. Nawaz says:

    Could you plz add a video on the same WebDriver Listners.

    Thanks in Advance.

    1. Yes Nawaz will upload soon.

  13. Mahesh says:

    How webdriver listeners are different from TestNG listeners and when to use what and which is best ?

    1. Hey Mahesh,

      TestNG listener for test related events.

      WebDriver listener for driver events and we should use both for maximum output.

  14. 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.

    1. Hey Yogi yes will try to upload soon.

  15. 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.

    1. Hi Sujay,

      Welcome mate 🙂 keep visiting.

  16. 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.

    1. Hi Reshma,

      Yes sure will try to upload video on the same.

  17. 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.

    1. Hi Sai,

      ActivityCapture is the class that we have created which implements the listener.

    2. @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

  18. Ram Narayan says:

    Thanks for detailed explanation … Mean it ….

  19. Ramesh says:

    Thank you so much. Clearly mentioned each and every thing about listeners and it implementation in Framework.:)_

    1. Hi Ramesh,

      Thank you. Glad to know it helped 🙂 Keep visiting.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.