Skip to content

Instantly share code, notes, and snippets.

@allanlewis
Forked from krmahadevan/ClassWithListener
Created August 31, 2016 09:01
Show Gist options
  • Save allanlewis/55a87e51e05249b2ae9d0e03e90ee1ff to your computer and use it in GitHub Desktop.
Save allanlewis/55a87e51e05249b2ae9d0e03e90ee1ff to your computer and use it in GitHub Desktop.
Demo that shows how to make use of Event Firing Web Driver.
package com.test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.testng.annotations.Test;
import com.test.listeners.WebDriverListener;
import com.test.webdriver.MyRemoteWebDriver;
import com.test.webdriver.MyWebElement;
public class ClassWithListener {
@Test
public void testMethod() throws MalformedURLException {
DesiredCapabilities dc = new DesiredCapabilities();
dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
MyRemoteWebDriver rwd = new MyRemoteWebDriver(new URL(MyConstants.URL.toString()), dc);
EventFiringWebDriver efwd = new EventFiringWebDriver(rwd);
WebDriverListener eventListener = new WebDriverListener(rwd);
efwd.register(eventListener);
efwd.get("http://www.linkedin.com/");
MyWebElement element = new MyWebElement(rwd.findElement(By.name("search")));
element.click();
efwd.quit();
}
}
enum MyConstants {
URL("http://localhost:4444/wd/hub");
private String value;
private MyConstants(String value) {
this.value = value;
}
public String toString() {
return this.value;
}
}
package com.test.webdriver;
import java.net.URL;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class MyRemoteWebDriver extends RemoteWebDriver {
public MyRemoteWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
}
}
package com.test.webdriver;
import org.openqa.selenium.WebElement;
public class MyWebElement {
private WebElement element;
public MyWebElement(WebElement element) {
this.element = element;
}
public void click() {
System.out.println("About to perform clicking on a web element");
validateWebElement();
element.click();
}
public void setElement(WebElement element) {
this.element = element;
}
private void validateWebElement() {
if (this.element == null)
throw new IllegalStateException("WebElement cannot be null");
}
}
package com.test.listeners;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.WebDriverEventListener;
public class WebDriverListener implements WebDriverEventListener {
private RemoteWebDriver webDriver;
public WebDriverListener(RemoteWebDriver webDriver){
this.webDriver = webDriver;
}
public void beforeNavigateTo(String url, WebDriver driver) {
System.out.println("Before navigating to url printing the browser associated capabilities");
System.out.println(this.webDriver.getCapabilities());
}
public void afterNavigateTo(String url, WebDriver driver) {
// TODO Auto-generated method stub
}
public void beforeNavigateBack(WebDriver driver) {
// TODO Auto-generated method stub
}
public void afterNavigateBack(WebDriver driver) {
// TODO Auto-generated method stub
}
public void beforeNavigateForward(WebDriver driver) {
// TODO Auto-generated method stub
}
public void afterNavigateForward(WebDriver driver) {
// TODO Auto-generated method stub
}
public void beforeFindBy(By by, WebElement element, WebDriver driver) {
// TODO Auto-generated method stub
}
public void afterFindBy(By by, WebElement element, WebDriver driver) {
// TODO Auto-generated method stub
}
public void beforeClickOn(WebElement element, WebDriver driver) {
// TODO Auto-generated method stub
}
public void afterClickOn(WebElement element, WebDriver driver) {
// TODO Auto-generated method stub
}
public void beforeChangeValueOf(WebElement element, WebDriver driver) {
// TODO Auto-generated method stub
}
public void afterChangeValueOf(WebElement element, WebDriver driver) {
// TODO Auto-generated method stub
}
public void beforeScript(String script, WebDriver driver) {
// TODO Auto-generated method stub
}
public void afterScript(String script, WebDriver driver) {
// TODO Auto-generated method stub
}
public void onException(Throwable throwable, WebDriver driver) {
// TODO Auto-generated method stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment