Tuesday, February 16, 2016

Listeners and EventFiringWebDriver in Selenium Webdriver


Before getting in we should talk about webdriverEvent listeners and EvebtFiringWebDriver interfaces.
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
How to implement Listener in our Script
Step 1- Create a new Class that will implement WebDriverEventListener methods
In general for every action that runs we need to perform some activity logging ex. click, navigate etc and these are called events. So when ever we run script these actions logging will happen through these listeners and track them .
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.
Program for what is listeners in selenium webdriver
and call this as ActivityCapture



for more details about webDriver listener click here : 



In above method we are just printing on console and this method will automatically called once click events fired.   
Step 2- Now create your simple script, create EventFiringWebDriver object, and pass your driver object.
EventFiringWebDriver myevent=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 handler=new ActivityCapture();
Step 4- Now register that event using register method and pass the object of ActivityCapture class
myevent.register(handler);
 Implementation of Webdriver listener
Now create a main java class as ListenersAndEventFiring 
In below sample scripts it will try to log in twitter account and for each actions the WebDriverEventListener and print on the screen. 


Sample console output :

Started Webdriver listener demo...

Before navigating https://twitter.com/login
Before navigating FirefoxDriver: firefox on MAC (ec52d8fa-3c3c-5140-a30e-3daf2dc4536f)
After navigating https://twitter.com/login
After navigating FirefoxDriver: firefox on MAC (ec52d8fa-3c3c-5140-a30e-3daf2dc4536f)
before FindBY By.xpath: //*[@id='page-container']/div/div[1]/form/fieldset/div[1]/input
After FindBy By.xpath: //*[@id='page-container']/div/div[1]/form/fieldset/div[1]/input
before FindBY By.xpath: //*[@id='page-container']/div/div[1]/form/fieldset/div[2]/input
After FindBy By.xpath: //*[@id='page-container']/div/div[1]/form/fieldset/div[2]/input
before FindBY By.xpath: //*[@id='page-container']/div/div[1]/form/div[2]/button
After FindBy By.xpath: //*[@id='page-container']/div/div[1]/form/div[2]/button
before click [[FirefoxDriver: firefox on MAC (ec52d8fa-3c3c-5140-a30e-3daf2dc4536f)] -> xpath: //*[@id='page-container']/div/div[1]/form/div[2]/button]
After click [[FirefoxDriver: firefox on MAC (ec52d8fa-3c3c-5140-a30e-3daf2dc4536f)] -> xpath: //*[@id='page-container']/div/div[1]/form/div[2]/button]
End of listener demo....