WebDriver Event Listener for effective Logging and Reporting

We discussed a lot more things about Selenium WebDriver. Today we are going to discuss Selenium WebDriver Event Listener. A framework without the implementation of the listener is incomplete because listeners help in many ways for advancement in the Automation framework.

Before we jump to the topic on WebDriver Event Listener, I would like to recommend you some of the awesome articles posted on inviul.

What is WebDriver Event Listener?

WebDriver Event Listener is fundamentally an interface, which is implemented by the class to override the methods written inside it. In general, listener’s work is to listen to the events and behave according to the definition given by the defined methods.

Listeners are mainly used to create effective logging and reporting in Selenium. They basically modify the default behavior of the driver. We can implement WebDriver Event listener along with log4j logging.

Webdriver event listener image

How to set up WebDriver Event Listener?

You need to implement the WebDriverEventListener interface to your class. Once implemented you will get some methods defined within it without the statement written inside the method. See below the default implementation.

package com.inviul.selenium.project;

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;

public class EventActivityCapturer implements WebDriverEventListener {

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

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

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

	public void afterClickOn(WebElement arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		
	}

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

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

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

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

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

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

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

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

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

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

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

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

	public void beforeClickOn(WebElement arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		
	}

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

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

	public void beforeGetText(WebElement arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		
	}

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

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

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

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

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

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

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

}

Once you have implemented the WebDriver Event Listener then go to that class or TestNG file where you have instantiated WebDriver. In this class, create an instance of EventFiringWebDriver and pass the instance of WebDriver as an argument in it.

If the above steps are done, then create the instance of that class where you have implemented the WebDriver event listener. Now you have to register the instance of the implemented class with the EventFiringWebDriver.

Let’s look at the detailed steps below.

Steps to implement WebDriver Event Listener

Step# 1: Implement WebDriverEventListener

At first, you need to create a class (let’s say, EventActivityCapturer.java). Now you must implement this class with WebDriver Event Listener.

I have edited some of the methods which I am going to use in my project. So, you can do the same as per your convenience.

package com.inviul.selenium.project;

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;

public class EventActivityCapturer implements WebDriverEventListener {

	public void afterAlertAccept(WebDriver arg0) {
		System.out.println("After Alert Accept- "+arg0);
		
	}

	public void afterAlertDismiss(WebDriver arg0) {
		System.out.println("After Alert Dismiss- "+arg0);
		
	}

	public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
		System.out.println("After change in value of- "+arg2);
		
	}

	public void afterClickOn(WebElement arg0, WebDriver arg1) {
		System.out.println("After Clicking on WebElement- "+arg0);
		
	}

	public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
		System.out.println("After Finding by- "+arg0);
		
	}

	public <X> void afterGetScreenshotAs(OutputType<X> arg0, X arg1) {
		System.out.println("After Screenshot- "+arg0);
		
	}

	public void afterGetText(WebElement arg0, WebDriver arg1, String arg2) {
		System.out.println("Text is- "+arg2);
		
	}

	public void afterNavigateBack(WebDriver arg0) {
		System.out.println("After Navigate back- "+arg0);
		
	}

	public void afterNavigateForward(WebDriver arg0) {
		System.out.println("After Navigate forward- "+arg0);
		
	}

	public void afterNavigateRefresh(WebDriver arg0) {
		System.out.println("Page Refresh- "+arg0);
		
	}

	public void afterNavigateTo(String arg0, WebDriver arg1) {
		System.out.println("Navigated to- "+arg0);
		
	}

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

	public void afterSwitchToWindow(String arg0, WebDriver arg1) {
		System.out.println("Switched to window- "+arg0);
		
	}

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

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

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

	public void beforeClickOn(WebElement arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		
	}

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

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

	public void beforeGetText(WebElement arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		
	}

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

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

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

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

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

	public void beforeSwitchToWindow(String arg0, WebDriver arg1) {
		System.out.println("Before window switching- "+arg0);
		
	}

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

}

Step# 2: Register with EventFiringWebDriver class

Next, create the instance of WebDriver and pass it to the EventFiringWebDriver.

WebDriver driver = new ChromeDriver();

EventFiringWebDriver drivingEvent = new EventFiringWebDriver(driver);

Now we need to register the implemented class with EventFiringWebDriver.

EventActivityCapturer activity = new EventActivityCapturer();

drivingEvent.register(activity);

Here is the full program.

package com.inviul.selenium.project;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

public class ListenerImplementation {

	public static void main(String[] args) throws InterruptedException {
		  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
		  	 
	  	  WebDriver driver = new ChromeDriver();
	  	  
	  	  EventFiringWebDriver drivingEvent = new EventFiringWebDriver(driver);
	  	  
	  	  EventActivityCapturer activity = new EventActivityCapturer();
	  	  
	  	  	drivingEvent.register(activity);
	  	  
	  	  	drivingEvent.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  	  
	  		drivingEvent.get("https://www.naukri.com/");
	  	  
	  		drivingEvent.manage().window().maximize();
	  	  
	  	  Thread.sleep(4000);
	  	  
	  	  String defaultWindow = drivingEvent.getWindowHandle();
	  	  
	  	  System.out.println("Default window name is- " +defaultWindow);
	  	  
	  	  Thread.sleep(3000);
		  
	  	  Set<String> childWindows = drivingEvent.getWindowHandles();
	  	  
	  	  for(String child : childWindows){
	  		  
	  		  if(!child.equalsIgnoreCase(defaultWindow)){
	  			drivingEvent.switchTo().window(child);
	  			  System.out.println("Child windows- "+child);
	  		  } else {
	  			  System.out.println("There are no child windows");
	  		  }
	  		  
	  		  
	  	  }
		  
	  	  drivingEvent.close();
	  	  
	  	  drivingEvent.quit();
	  }

	}


Below is the console output.

WebDriver Event Listener Console output

Hope you enjoyed the article. Please comment for any queries. Don’t miss our latest announcement, join our Facebook group.

Join Inviul fb group

2 Comments

Leave a Reply