Home » Software Testing » Automation Testing » Selenium WebDriver Tutorial » WebDriverEventListener in Selenium WebDriver

WebDriverEventListener in Selenium WebDriver

WebDriverEventListener is provided in WebDriver to track events that take place in WebDriver during script execution. But you would be thinking “Why I am talking about this and who cares to take the track on Events in WebDriver script execution”. Then I would say one, “Every automation engineer “and they do care about it and closely want to see how all actions are triggered either this is clicked event on a button, URL or navigating back and forward in browser or some exception occur and Some time they thought like oh God what is happening and some time they talk themselves to find the answer,  to take screenshot once some exceptions occur during execution. So WebDriverEventListener is just the answer of all the queries that run into each of the test developer or automation engineer around the globe.
If you want to read more post on Selenium then visit Selenium WebDriver Page and take Selenium WebDriver Quiz to see the area of improvement.

 

So let’s see how to implement it…So remember these thumb rules

1-      Create a class to override events just by Implementing WebDriverEventListener interface or by Extending AbstractWebDriverEventListener abstract class where  WebDriverEventListener is an interface which has near about  15 methods that we can override to see after and before tracking of each event that we trigger through our scripts.

2-      Initializing WebDriver instance  in our actual test class or test script like this

3-      Creating one object of  EventFiringWebDriver  just by calling  one of its constructor which takes an instance of WebDriver as a parameter like this

4-      Registering EventListener with EventFiringWebDriver:
So let’s call the class where we have just implemented WebDriverEventListener or have extended AbstractWebDriverEventListener. Suppose the name of that class is OverrideClass()
So creating one object of this class like this

Now let’s register the object of this event listener class with EventFiringWebDriver

Above we have seen all the thumb rules now its time to see its working in real time.

So let’s create the class in which we will override the events function and here i am going to manipulate the methods which are related to  driver.navigate().back();

Now let’s see the test script where we are going to implement everything that we have seen above

Once we will execute this program, we will see these results

So you can see that two URL(s) are printed but from where…then think once again and remember you have written these code in OverridClass and once navigate().back(); is called it first called beforeNavigateBack(WebDriver driver) method and later afterNavigateBack(WebDriver driver) method implicitly..and so these results are in Console.

Please don’t forget to share it if you like this post !!

Reference:
seleniumhq.org

6 thoughts on “WebDriverEventListener in Selenium WebDriver”

  1. Pingback: Testing Bits – 6/8/14 – 6/14/14 | Testing Curator Blog

  2. Respected Sir,

    The site was really helpful, I want to use Event Listeners in Page Object Patter following Page-factory.
    Can you guide me.

    Its urgent.

Leave a Reply

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

%d bloggers like this: