What is Listeners in Selenium WebDriver?

Here, we are going to discuss one of the most interesting topic of Selenium WebDriver.

In general terms, what listeners do?

So, listeners simply listen to you. But, with respect to Selenium WebDriver, listeners listen to particular events of WebDriver.

What is event in Selenium WebDriver? 

Event refers to any action that we perform using WebDriver like click, type, navigate etc. this all are termed as Events.

Why to use Listener?

Suppose, you want to perform a set of actions before/ after any defined event by WebDriver, we can make use of Listeners and achieve it.

Let’s have a look at the working demo of Listeners.

Step 1 : Create an EventListenerClass.java, which implements WebDriverEventListener interface.

It implements all 15 event methods defined in WebDriverEventListener interface, which we can implement in a way we want.

public class EventListenerClass 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
System.out.println(“Inside after find by method”);
}

@Override
public void afterNavigateBack(WebDriver arg0) {
// TODO Auto-generated method stub

}

@Override
public void afterNavigateForward(WebDriver arg0) {
// TODO Auto-generated method stub

}

@Override
public void afterNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println(“Inside after navigation to method”);
}

@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
System.out.println(“Inside before find by method”);
}

@Override
public void beforeNavigateBack(WebDriver arg0) {
// TODO Auto-generated method stub

}

@Override
public void beforeNavigateForward(WebDriver arg0) {
// TODO Auto-generated method stub

}

@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println(“Inside before navigation to method”);
}

@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

}

}

 

Step 2 : Create a EventListenerDriver.java, from which we will bind  the EventListenerClass.java to EventFiringWebDriver.

public class EventListenerDriver {
             public static void main(String[] args) {

// creating a FirefoxDriver instance.

                          WebDriver firefoxDriver = new FirefoxDriver();

// creating an instance of our custom EventListenerClass class
                          EventListenerClass eventListener = new EventListenerClass();

// creating EventFiringWebDriver class’ instance by passing firefoxDriver instance.
EventFiringWebDriver driver = new EventFiringWebDriver(firefoxDriver);

// calling register method of EventFiringWebDriver class to bind our custom listener class with the EventFiringDriver.
                          driver.register(eventListener);

// that’s all, now the driver instance is bound to our EventListener class
                          driver.get(“http://google.com“);
                          driver.findElement(By.linkText(“Gmail”));

               }
}

Step 3 : Run the EventListenerDriver.java file and you will be able to see listener methods will be called on console.

Output :

Inside before navigation to method
Inside after navigation to method
Inside before find by method
Inside after find by method

 

Important classes and interfaces for implementing Listeners in Selenium WebDriver :

  1. WebDriverEventListener : Its an interface containing all unimplemented methods related to different events of WebDriver.
  2. AbstractWebDriverEventListener : Its an abstract class which implements WebDriverEventListener interface and provides empty implementation of all the methods defined in interface.public abstract class AbstractWebDriverEventListener implements WebDriverEventListener

     

  3. EventFiringWebDriver : Its a class which implements WebDriver interface.It has two important methods:a. register (WebDriverEventListener eventListener) : The method is used to bind our WebDriver instance with custom WebDriverEventListener class to handle different events.

    b. unregister (WebDriverEventListener eventListener) : The method is used to isolate the WebDriver instance from WebDriverEventListener class.

 

Hope it helps you!

Cheers 🙂

Advertisements

#eventfiringwebdriver, #listeners-in-webdriver, #webdrivereventlistener