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  WebDriverEventListener is 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

WebDriver  driver = new FirefoxDriver();

3-Creating one object of  EventFiringWebDriver  just by calling  one of its constructor which takes instance of WebDriver as parameter like this
EventFiringWebDriver eDriver  = new EventFiringWebDriver(driver);

4-Registering EventListener with EventFiringWebDriver:
So lets 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

OverrideClass oc = new OverrideClass();
Now let’s register the object of this event listener class with EventFiringWebDriver

eDriver.register(oc);
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();

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;

public class OverrideClass implements WebDriverEventListener{
    @Override
    public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void afterClickOn(WebElement arg0, WebDriver arg1) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void afterNavigateBack(WebDriver driver) {
        System.out.println("After clicking back  "+ driver.getCurrentUrl());       
    }
    @Override
    public void afterNavigateForward(WebDriver arg0) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void afterNavigateTo(String arg0, WebDriver arg1) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void afterScript(String arg0, WebDriver arg1) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void beforeClickOn(WebElement arg0, WebDriver arg1) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void beforeNavigateBack(WebDriver driver) {
        System.out.println("Before clicking Back"+driver.getCurrentUrl());       
    }
    @Override
    public void beforeNavigateForward(WebDriver arg0) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void beforeNavigateTo(String arg0, WebDriver arg1) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void beforeScript(String arg0, WebDriver arg1) {
        // TODO Auto-generated method stub       
    }
    @Override
    public void onException(Throwable arg0, WebDriver arg1) {
        // TODO Auto-generated method stub       
    }
Now let’s see the test script where we are going to implement everything that we have seen above


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

public class ImplementClass {
     public static void main(String[] args) {
        //Initializing instance of Webdriver
        WebDriver wDriver = new FirefoxDriver();
        //Initializing instance of EventFiringWebDriver
        EventFiringWebDriver driver = new EventFiringWebDriver(wDriver);

        //Now create object of OverrideClass to register it with EventFiringWebDriver

        OverrideClass oc = new OverrideClass();
        //Registering it with EventFiringWebDriver
        driver.register(oc);
        driver.get("http://gmail.com");
        driver.get("http://outlook.com");
        driver.navigate().back();
        /*
         * Here we are just using     driver.navigate().back() then two method of OverrideClass
         * will be called before and after this event is triggered
         *
         */
    }


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

Before clicking Back  https://login.live.com/login.srf?wa=wsignin1.0&ct=1402256444&rver=6.1.6206.0&sa=1&ntprob=-1&wp=MBI_SSL_SHARED&wreply=https:%2F%2Fmail.live.com%2F%3Fowa%3D1%26owasuffix%3Dowa%252f&id=64855&snsc=1&cbcxt=mail
After clicking back  https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1
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.

1 comment: