【问题标题】:Control Test Execution Progress with Keyboard Interrupts - using WebDriverEventListener & EventFiringWebDriver使用键盘中断控制测试执行进度 - 使用 WebDriverEventListener 和 EventFiringWebDriver
【发布时间】: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


【解决方案1】:

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

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

此 SO 答案中的更多信息如何模拟键盘输入 Which is the easiest way to simulate keyboard and mouse on Python?

【讨论】:

  • 但是添加一个包装器只会负责触发测试,但不会真正进入键盘中断测试
【解决方案2】:

我能够通过 Krishnan Mahadevan 的博客 - https://gist.github.com/krmahadevan/1728633 中提供的指针完成工作

这是它的一个sn-p -

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 重定向,系统都会提示我按回车键。按下任意键时,测试会继续进行。

【讨论】:

    猜你喜欢
    • 2019-06-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多