EVENT LISTENERS IN SELENIUM WEBDRIVER

WebDriverEventListener is provided in WebDriver to track events those take place in WebDriver during script execution. But you would be thinking “Why I am talking about this and who cares to take 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 click 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 runs in to each of the test developer or automation engineer around the globe.
So let’s see how to implement it..So remember these thumb rules
1-      Create class to override events just by Implementing WebDriverEventListener  interface or by Extending AbstractWebDriverEventListener abstract class where  WebDriverEventListeneris a interface which have near about  15 methods that we can override to see  after and before tracking of each events that we trigger through our scripts.
2-      Initializing WebDriver instance  in our actually test class or test script like this
3-      Creating one object of  EventFiringWebDriver  just by calling  one of its constructor which takes instance of WebDriver as parameter like this
4-      Registering EventListener with EventFiringWebDriver:
So lets call the class where we have just implemented WebDriverEventListener or  have extendedAbstractWebDriverEventListener. 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 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.