我使用Webdriver + Java + TestNG + Maven自动测试我的测试
我正在寻找一种解决方案,其中可以使用每个导航上的键盘中断来控制测试的进度(Step Into next step)。
Ex:假设我们是应用程序的自动导航。 对于每个页面重定向,应通过按键来驱动测试的进度。
我已经部分找到了解决方案。我使用了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
}
}
然而,它进入了一个无限循环。没有检测到按键(空格)
答案 0 :(得分:0)
我非常确定Selenium无法直接支持您想要实现的目标。但我可以分享我们为我们的一个项目做的事情。我们使用python来模拟Android设备的键盘输入。
但是,您可以在Python中编写一些包装器代码,等待键盘输入,然后执行Selenium代码。
此SO中的更多信息回答了如何模拟键盘输入Which is the easiest way to simulate keyboard and mouse on Python?
答案 1 :(得分:0)
我能够通过Krishnan Mahadevan博客中提供的指示来完成 - https://gist.github.com/krmahadevan/1728633
以下是它的片段 -
public EventFiringWebDriver initateWebDriverWithListener(){
driver1 = new FirefoxDriver();
EventFiringWebDriver driver = new EventFiringWebDriver(driver1);
WebDriverListener eventListener = new WebDriverListener(driver);
driver.register(eventListener);
}
public
class WebDriverListener implements WebDriverEventListener {
public void afterNavigateTo(String url, WebDriver driver) {
Logger.debug("Hit return ....");
System.out.println("Hit return ....");
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Logger.debug("Proceeding further");
System.out.println("Proceeding further");
}
}
public class RunFromCommandLine{
public static void main(String[] args) throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
TestNG testng = new TestNG();
testng.setXmlSuites((List <XmlSuite>)(new Parser("src"+File.separator+"test"+File.separator+"resources"+File.separator+"xml_Suites"+File.separator+"TestNG.xml").parse()));
testng.setSuiteThreadPoolSize(1);
testng.run();
}
}
maven命令 - mvn -X -P runClass clean test -DskipTests exec:java -Dexec.mainClass =“com.shn.test.RunFromCommandLine”-Dexec.classpathScope = test -e
我被提示为每个发生的URL重定向命中返回键。在点击任何键时,测试会进一步进行。