Webdriver Listeners in Selenium | WebDriverEventListener

Webdriver Listeners in Selenium :

Event listener in WebDriver helps to tack event those we trigger and by using these we could get a better idea of sequential order of actions .

Listeners areĀ  helpful for logging and exception section. Suppose you are trying to track exception and the same time wants to take screenshot.

Instead of hard-coding logging requests all over the place, we can leverage an event listener to give us the information we want

If you think, Listeners are only useful in logging then you are wrong, it more helpful on pre and post steps of a action in selenium webdriver.

Selenium provide WebDriverEventListener interface for listening event in selenium, WebDriverEventListener Interface provide methods, which covers most of the actions (not all) in selenium webdriver.

 

WebDriverEventListener in Selenium :

WebDriver Event Listener interface allows us to implement the methods

 

EventFiringWebDriver in Selenium:

EventFiringWebDriver class actually fire WebDriver event

 

Procedure for creating listeners and handling events:

  • First create a listener class in Java and Implement WebDriverEventListener interface
  • Override the predefined functions of WebDriverEventListener interface with your own body or logic
  • Create a new class for testing the listener that you have overridden in above step
  • Open the browser using standard code
  • Create a Event firing web driver Object and then pass the web driver reference in the event firing web driver constructor
  • Register the listener class reference to the Event firing web driver reference
  • Go to your tests and perform some task
package test;

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

public class MyEventListener implements WebDriverEventListener{

	public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {		
		System.out.println("afterChangeValueOf");
	}

	public void afterClickOn(WebElement arg0, WebDriver arg1) {		
		System.out.println("afterClickOn");
	}

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

	public void afterNavigateBack(WebDriver arg0) {		

		System.out.println("afterNavigateBack");	}

	public void afterNavigateForward(WebDriver arg0) {		
		System.out.println("afterNavigateForward");	}

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

	public void afterScript(String arg0, WebDriver arg1) {		
		System.out.println("afterScript");	}

	public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {		

		System.out.println("println");	}

	public void beforeClickOn(WebElement arg0, WebDriver arg1) {		
		System.out.println("beforeClickOn");
	}

	public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {		
		System.out.println("beforeFindBy");
	}

	public void beforeNavigateBack(WebDriver arg0) {		
		System.out.println("beforeNavigateBack");
	}

	public void beforeNavigateForward(WebDriver arg0) {		
		System.out.println("beforeNavigateForward");
	}

	public void beforeNavigateTo(String arg0, WebDriver arg1) {		
		System.out.println("beforeNavigateTo");
	}

	public void beforeScript(String arg0, WebDriver arg1) {		
		System.out.println("beforeScript");
	}

	public void onException(Throwable arg0, WebDriver arg1) {	
		System.out.println("onException");
	}
}

 

Create the test file testing above Listeners

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

public class ListnerExample {

	public static void main(String []args){

		//set the system property***
		WebDriver driver = new FirefoxDriver();
		EventFiringWebDriver event = new EventFiringWebDriver(driver);
		MyEventListener handle = new MyEventListener();
		
		event.register(handle);
		event.navigate().to("http://google.com");
		event.findElement(By.name("q")).sendKeys("chercher tech");
		event.findElement(By.id("btnK")).click();
		event.quit();
		event.unregister(handle);
	}
}

 

Leave a Reply

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