【发布时间】:2013-08-18 11:02:52
【问题描述】:
我的测试使用 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
}
}
然后,它进入了一个无限循环。未检测到按键(空格)
【问题讨论】:
-
你想达到什么目的?
-
我正在尝试使用自动化来演示产品功能
-
在 IDE 的调试模式下运行测试并控制执行怎么样??
-
@TestAutomationEngr - 您应该将此作为答案发布,并更全面地描述它如何解决 OP 的问题。
-
@TestAutomationEngr - 我正在寻找可以从命令行运行的解决方案。使用 IDE 和断点将是一个非常繁琐的过程。假设您正在演示一个有 20 个页面要导航的测试。
标签: java selenium-webdriver automation webdriver testng