Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
}
@krmahadevan

This comment has been minimized.

Copy link
Owner Author

@krmahadevan krmahadevan commented Feb 3, 2012

Explanation :
In this example :
ClassWithListener is my class which implements WebDriverEventListener.

MyWebElement is my class which represents any given web element. Here I am just encapsulating the actual webelement and providing wrapper methods to perform operations on the webelements. The only difference between this class provided methods and the methods provided by RemoteWebElement is that, I can add more functions such as writing to log files etc., before actually performing the UI operation.

ClassWithListener - This is my test class which will show how all of these work together.

MyRemoteWebDriver - This is my customized RemoteWebDriver class.

Note : This is not a complete implementation, but just a mock demo to help you understand how to work with all of these.

@predlapalle

This comment has been minimized.

Copy link

@predlapalle predlapalle commented Aug 16, 2013

Hi,

I have my tests automated with Webdriver+Java+TestNG+Maven

I am looking for a solution wherein the progress (Step Into next step) of the test can be controlled using keyboard interrupts on every navigation.

Ex: Lets say we are automation navigation of an application. The progress of the test should be driven by a key press for every page redirection.

I am using the above implementation to achieve this. But hitting on the key is causing an infinite loop & not being detected.

Code -

@Test
public void testMethod(){
    WebDriver driver = new FirefoxDriver();
    EventFiringWebDriver efwd = new EventFiringWebDriver(driver);
    WebDriverListener eventListener = new WebDriverListener(efwd);
    efwd.register(eventListener);
    efwd.get("https://www.myshn.net/shndash");
    efwd.get("https://www.mail.google.com");

  }
}

public class WebDriverListener implements WebDriverEventListener {
private WebDriver webDriver;

public WebDriverListener(WebDriver webDriver){
    this.webDriver = webDriver;
}

public void beforeNavigateTo(String url, WebDriver driver) {

}

public void afterNavigateTo(String url, WebDriver driver) {
    final CountDownLatch latch = new CountDownLatch(1);
    KeyEventDispatcher dispatcher = new KeyEventDispatcher() {
        // Anonymous class invoked from EDT
        public boolean dispatchKeyEvent(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE)
                latch.countDown();
            return false;
        }
    };
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
    try {
        latch.await();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }  // current thread waits here until countDown() is called
    KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);
    System.out.println(this.webDriver.getTitle());
    // TODO Auto-generated method stub

        }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.