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 have partially found the solution. I used the code from github - https://gist.github.com/krmahadevan/1728633
Test Class -
import com.shn.library.WebDriverListener;
public class DummyTest {
@Test
public void testMethod(){
WebDriver driver = new FirefoxDriver();
EventFiringWebDriver efwd = new EventFiringWebDriver(driver);
WebDriverListener eventListener = new WebDriverListener(efwd);
efwd.register(eventListener);
efwd.get("http://www.yahoo.com");
efwd.get("https://www.mail.google.com");
}
}
Implementing WebDriverEventListener -
package com.shn.library;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.concurrent.CountDownLatch;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
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
}
}
But then, its getting into an infinite loop. The key press (space) is not being detected