Event Listener in Selenium

EVENT LISTENER IN SELENIUM

We have already learned about the TestNG Listeners, which are used to alter the behavior of TestNG implementation. Event Listener in selenium is similar to the implementation of TestNG listeners.

Event Listener listens to certain events (defined in WebDriverEventListener interface) and triggers whenever these events occur. Event Listeners are primarily used for reporting and logging purposes.

 

Types of Events

 

Selenium can keep track of events like afterNavigateBack, afterClickOn, onException, etc. to name a few. Using the WebDriverEventListener interface in a class, we can use these methods which help in tracking and logging these events.

We have to override these methods according to our requirement.

 

To See All The Methods Provided by WebDriverEventListener

click here

Case Study

 

Write a script which should perform the following:​

  • Open chrome browser and navigate to google homepage.
  • Enter some search string and click on the Google Search button.
    • Track the click event and print the web element/ locator value using afterClickOn() method.
  • Once the search page is displayed, browse back to the Google homepage using navigate().back()
    • Track the browser back event using afterNavigateBack() method and print the current URL (https://www.google.com/)

 

Solution

 

Following are the components used to implement the event listener in selenium:

  • Create a class EventListener which should implement WebDriverEventListener interface.
    • WebDriverEventListener interface contains the methods which are triggered when a certain event occurs.
  • Override the methods afterClickOn(WebElement element, WebDriver driver) and afterNavigateBack(WebDriver driver) in EventListener class.
  • Create another class MainEventListener which will drive the test. This class also include:
    • the main() method.
    • the object of EventFiringWebDriver class. EventFiringWebDriver is a WebDriver wrapper used to register the events.
    • the object of EventListener class.

Below is the pictorial representation of the above:

 

Event Listener in Selenium

EventListener.java

package events;

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

//Class to implement the event listeners

public class EventListener implements WebDriverEventListener{

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

    @Override
    public void afterAlertDismiss(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 afterClickOn(WebElement arg0, WebDriver arg1) {
        
        System.out.println("WebElement " + arg0.toString() + " clicked");
        
    }

    @Override
    public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public <X> void afterGetScreenshotAs(OutputType<X> arg0, X arg1) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void afterGetText(WebElement arg0, WebDriver arg1, String arg2) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void afterNavigateBack(WebDriver arg0) {
        System.out.println("Navigated back to " + arg0.getCurrentUrl());
        
    }

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

    @Override
    public void afterNavigateRefresh(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 afterSwitchToWindow(String arg0, WebDriver arg1) {
        // 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 beforeClickOn(WebElement arg0, WebDriver arg1) {
        
        
    }

    @Override
    public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public <X> void beforeGetScreenshotAs(OutputType<X> arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void beforeGetText(WebElement arg0, WebDriver arg1) {
        // 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 beforeScript(String arg0, WebDriver arg1) {
        // TODO Auto-generated method stub
        
    }

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

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

}

MainEventListener.java

package events;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class MainEventListener {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "/home/dhawal/selenium jars/chromedriver/chromedriver");
        
        WebDriver driver = new ChromeDriver();
        
        //WebDriver wrapper used to register the events
        EventFiringWebDriver eventFire = new EventFiringWebDriver(driver);
        
        //Create object of EventListener class 
        EventListener eventListener = new EventListener();
        
        //register the events in EventListener class. This will invoke the methods in EventListener class when a certain event occurs        
        eventFire.register(eventListener);
        
        WebDriverWait wait = new WebDriverWait(driver, 300);
        eventFire.get("https://www.google.com/");
        
        eventFire.findElement(By.xpath("//input[@name='q']")).sendKeys("Selenium");
        
        eventFire.findElement(By.xpath("//input[@name='q']")).sendKeys(Keys.TAB);
        wait.until(ExpectedConditions.visibilityOf(eventFire.findElement(By.xpath("//div[@class='FPdoLc VlcLAe']//input[@name='btnK']"))));
        
        eventFire.findElement(By.xpath("//div[@class='FPdoLc VlcLAe']//input[@name='btnK']")).click();
        eventFire.navigate().back();
        
        eventFire.quit();
        
        
        

    }

}

Execute the above class and it should log the events overridden in EventListener class. Below is the output after execution:

 

Event Listener in Selenium output

Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

Leave a Reply

Your email address will not be published. Required fields are marked *