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

 

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

 

 

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.

 

32 Comments

  1. Nikhil
  2. hemant
  3. Vishwaa
  4. Lokesh Sharma
  5. Priyanka
  6. Priyaranjan
  7. Aruna
    • yes aruna
  8. Nawaz
  9. Mahesh
  10. yogi
  11. sujay kumar
  12. Reshma Sultana
  13. saikiran
  14. Ram Narayan
  15. Ramesh

Leave a Reply