Home Guide Listeners in Selenium : Basics and Types of Listeners

Listeners in Selenium : Basics and Types of Listeners

Shreya Bose, Technical Content Writer at BrowserStack -

What are Listeners in Selenium?

Often, testing websites means testing multiple web pages, which requires passing data from one page to another. Selenium, the most popular test automation framework used to test websites, provides numerous functionalities to enable this interaction between web pages. One of these tools is Listeners.

As the name suggests, Listeners possess the ability to “listen” to a certain event. Often used for customizing reports and logs, it serves as an interface that can modify system behavior.

Types of Listeners in Selenium

There are two types of Selenium Listeners:

  • WebDriver Listeners
  • TestNG Listeners

In this article, we’ll be covering WebDriver Listeners. TestNG Listeners will be discussed in Part 2.

WebDriver Listeners

This type of Listener is an interface holding predefined methods. They are helpful for viewing events triggered by the WebDriver. This helps testers analyze results and debug any resulting issues.

The WebDriverEventListener interface can implement classes and methods like EventFiringWebDriver and WebDriverEventListener. It can also track events like “beforeNavigateTo” , “afterNavigateTo”, “BeforeClickOn”, “AfterClickOn” and more.

Implementing Listeners in Selenium WebDriver Script

  • Create a class named “EventCapture” to implement WebDriverEventListener methods
package listeners;

public class EventCapture implements WebDriverEventListener{

}
  • Create another class named “ListenerMainClass” and write the necessary script
  • Create the “EventFiringWebDriver” object in the Class named “ListenerMainClass“. Pass the driver object as a parameter
EventFiringWebDriver eventHandler = new EventFiringWebDriver(driver);
  • Create an object of the class “EventCapture” under the “ListenerMainClass“, to implement all methods of WebDriverEventListener in order to register with the EventFiringWebDriver
EventCapture eCapture = new EventCapture();

Implementing WebDriver Event Listeners

In this example, the code automates the page “browserstack.com” to demonstrate how WebDriver event listeners function

  • Create a class named “EventCapture” which will implement the WebDriverEventListener.
  • Add different methods to help with easy execution
  • The “afterChangeValueOf()” method is called every time the code has to return the value of the element after it is been modified or changed
  • Override methods that help invoke functions from the base class in the derived class.

Code for Webdriver Event Listeners

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
//WebDriver Event Listeners
public class EventCapture implements WebDriverEventListener{


public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("In afterChangeValueOf");
}

@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 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

}

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 arg0) {
// TODO Auto-generated method stub

}

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

}

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

}

@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub

}

@Override
public void onException(Throwable arg0, WebDriver arg1) {
// TODO Auto-generated method stub

}

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

}

@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
// TODO Auto-generated method stub

}

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

}

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

}

@Override
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
// TODO Auto-generated method stub

}

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

}

@Override
public void beforeScript(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub

}

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

}

@Override
public void afterScript(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub

} 

}

After the above, one needs to create a main class in order to perform actions on the methods that were declared in the EventCapture program.

  • Instantiate the WebDriver instance with the browser driver
  • Create an object of EventFiringWebDriver. Name it eventHandler
  • Create an object of EventCapture, eCapture. Register eCapture on eventHandler using register method
eventHandler.register(eCapture);
  • Use the object of the EventFiringWebDriver to navigate to a new page:
eventHandler.navigate().to("http://www.google.com");
  • Use eventHandler to find elements and perform actions.
  • Quit the driver execution using the command:
eventHandler.quit();
  • Unregister the object of the EventCapture, eCapture

Code

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import java.net.URL;

public class ListenerMainClass {

public static final String USERNAME = "prakharkulshresh2";
public static final String AUTOMATE_KEY = "dphXto2bxDtgA3DsVRVy";
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

public static void main(String[] args) throws Exception {

DesiredCapabilities caps = new DesiredCapabilities();
WebDriver driver = new RemoteWebDriver(new URL(URL), caps); 
JavascriptExecutor jse = ((JavascriptExecutor)driver);
EventFiringWebDriver eventHandler = new EventFiringWebDriver(driver);
EventCapture ecapture = new EventCapture();

eventHandler.register(ecapture); 
eventHandler.navigate().to("http://www.google.com");
WebElement element = eventHandler.findElement(By.name("q"));
element.sendKeys("BrowserStack");
element.submit();
System.out.println(driver.getTitle());
eventHandler.unregister(ecapture);
driver.quit();
}
}

Now, that we have covered WebDriver Listeners using Selenium Webdriver in detail, we’ll be moving on to TestNG Listeners in Part 2.

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free