使用键盘中断控制测试执行进度 - 使用 WebDriverEventListener & EventFireringWebDriver



我用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
    }
}

但随后,它进入了一个无限循环。未检测到按键(空格)

我很确定Selenium不能直接支持您想要实现的目标。但我可以分享我们为一个项目所做的一些事情。,我们使用python来模拟Android设备的键盘输入。

但是,您可以用Python编写一些包装器代码,等待键盘输入,然后s执行Selenium代码。

更多信息,请回答如何模拟键盘输入在Python上模拟键盘和鼠标最简单的方法是什么?

我能够通过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时都会提示我按回车键。一旦按下任何键,测试将继续进行。

最新更新