26
Jul

Registering multiple event listeners to WebDriver

Registering multiple event listeners to WebDriver
====================================================

Selenium WebDriver provides an API for tracking the various events that happen when test scripts are executed using WebDriver. 

Many navigation events get fired before and after a WebDriver event occurs (such as before and after navigating to a URL and before and after browser back-navigation), and these can be tracked and captured.

To throw an event, WebDriver gives you a class named EventFiringWebDriver, and to catch that event, it provides the test-script developer with an interface named WebDriverEventListener. The test-script developer should provide its own implementations for the overridden methods from the interface.

Introducing the eventFiringWebDriver and eventListener classes
The EventFiringWebDriver class is a wrapper around the WebDriver that gives the driver the capability to fire events.

The EventListener class, on the other hand, waits to listen to EventFiringWebDriver and handles all events that are dispatched.

There can be more than one listener waiting to hear from the EventFiringWebDriver class for an event to fire. All event listeners should be registered with the EventFiringWebDriver class to get notified.

Step 1: Create your Custom event handlers (either by  extends AbstractWebDriverEventListener or implements WebDriverEventListener)
======================================================================================================================================

package examples;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.AbstractWebDriverEventListener;

public class MyDefaultEventHandler extends AbstractWebDriverEventListener {

	@Override
	public void afterNavigateTo(String url, WebDriver driver) {
		waitForLoad(driver);
	}

	@Override
	public void afterNavigateBack(WebDriver driver) {
		waitForLoad(driver);
	}

	@Override
	public void afterNavigateForward(WebDriver driver) {
		waitForLoad(driver);
	}

	@Override
	public void afterClickOn(WebElement element, WebDriver driver) {
		waitForLoad(driver);
	}

	@Override
	public void beforeNavigateTo(String url, WebDriver driver) {
		System.out.println("Before Navigate To " + url);
		waitForLoad(driver);
	}
	
	public void afterChangeValueOf(WebElement element, WebDriver driver) {
		waitForLoad(driver);
	}

	private void waitForLoad(WebDriver driver) {
		long start = System.currentTimeMillis();
		((JavascriptExecutor) driver).executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 500);");
		System.out.println("Wait time over :" + (System.currentTimeMillis() - start));

	}
}


package examples;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.AbstractWebDriverEventListener;

public class MyDefaultEventHandler2 extends AbstractWebDriverEventListener {

	@Override
	public void afterNavigateTo(String url, WebDriver driver) {
		waitForLoad(driver);
	}

	@Override
	public void afterNavigateBack(WebDriver driver) {
		waitForLoad(driver);
	}

	@Override
	public void afterNavigateForward(WebDriver driver) {
		waitForLoad(driver);
	}

	@Override
	public void afterClickOn(WebElement element, WebDriver driver) {
		waitForLoad(driver);
	}

	@Override
	public void beforeNavigateTo(String url, WebDriver driver) {
		System.out.println("$$$$$$$$$$$$$$ Before Navigate To " + url);
		waitForLoad(driver);
	}
	
	public void afterChangeValueOf(WebElement element, WebDriver driver) {
		waitForLoad(driver);
	}

	private void waitForLoad(WebDriver driver) {
		long start = System.currentTimeMillis();
		((JavascriptExecutor) driver).executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 500);");
		System.out.println("Wait time over :" + (System.currentTimeMillis() - start));

	}
}


Step 2:  Instantiate Local WebDriver or RemoteWebDriver
========================================================

package examples;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Platform;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.testng.annotations.Test;

public class jack {

	@Test
	public void browserWait() throws MalformedURLException {
		System.setProperty("webdriver.chrome.driver", "c:/selenium/chromedriver.exe");
		String node_host = "10.174.88.159";
		String node_port = "4444";
		ChromeOptions options = new ChromeOptions();
		options.setCapability(CapabilityType.PLATFORM_NAME, Platform.WINDOWS);
		options.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
		options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
		options.addArguments("--start-maximized");
		options.addArguments("disable-infobars");

		String node_URL = "http://" + node_host + ":" + node_port + "/wd/hub";
		WebDriver driver = new RemoteWebDriver(new URL(node_URL), options);
		
		EventFiringWebDriver eventFiringDriver =  new EventFiringWebDriver(driver);
		MyDefaultEventHandler eventListener = new MyDefaultEventHandler();
		MyDefaultEventHandler2 eventListener2 = new MyDefaultEventHandler2();
		eventFiringDriver.register(eventListener);
		eventFiringDriver.register(eventListener2);
		eventFiringDriver.get("https://google.com");
		// ------------
		long start = System.currentTimeMillis();
		((JavascriptExecutor) driver).executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 500);");
		System.out.println("Elapsed time: " + (System.currentTimeMillis() - start));

		// ------------
		driver.quit();

	}

}


Output:
=======

[RemoteTestNG] detected TestNG version 6.14.2
Jul 26, 2019 7:25:31 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Before Navigate To https://google.com
Wait time over :531
$$$$$$$$$$$$$$ Before Navigate To https://google.com
Wait time over :545
Wait time over :527
Wait time over :524
Elapsed time: 525
PASSED: browserWait