我用 Webdriver+Java+TestNG+Maven 自动化了我的测试
我正在寻找一种解决方案,其中可以使用每次导航时的键盘中断来控制测试的进度(进入下一步)。
例如:假设我们是应用程序的自动化导航。测试的进度应该由每次页面重定向的按键驱动。
我已经部分找到了解决方案。我使用了来自 github 的代码 - https://gist.github.com/krmahadevan/1728633
测试班 -
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");
}
}
实现 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
}
}
但是,它进入了一个无限循环。未检测到按键(空格)