App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide Types of Listeners in Selenium (with Code Examples)

Types of Listeners in Selenium (with Code Examples)

By Shreya Bose, Community Contributor -

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 can “listen” to a particular 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:

In this article, we’ll cover WebDriver Listeners and then follow up with an article for TestNG Listeners.

WebDriver Listeners

This type of Listener is an interface holding predefined methods. They help view 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 to register with the EventFiringWebDriver
EventCapture eCapture = new EventCapture();

Implementing WebDriver Event Listeners

The code automates the page “browserstack.com” in this example 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 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 must create a main class to perform actions on the methods 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, don’t forget to read about TestNG Listeners, which covers types of TestNG listeners along with a sample report and code examples.

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

All about TestNG Listeners in Selenium

findElement and findElements in Selenium

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack