Selenium+Java错误侦听器ext.js



当我不知道错误何时发生时,如何捕捉错误?

我使用的是Selenium+Java。正在为JS网页创建测试。

当出现错误时,测试会继续单击元素。错误在那一刻是可见的,所以它不是一个消息框或类似的东西。一个错误框变得可见。然后一段时间过去了,测试崩溃,表示无法单击某个元素。

在测试运行时,我如何监听任何可见(或可点击)的随机元素?这将使我能够发现错误,并且测试不会失败。

我应该把我的测试放入两个线程中吗?一个线程监听错误,另一个线程运行我的测试用例?

我建议使用以下

List<WebElement> Elements = getDriver().findElements(method);

首先,当您进入页面时,您只需准备页面元素的列表。然后在代码中多次包含此过程。将您的列表与新列表进行比较,查看是否出现错误元素,这意味着新列表中还会有一个元素。然后你就抓住了。

最好的选择是理解并解决潜在的错误条件。

如果该错误仅发生在测试中,或者无法解决,我建议查看您的驱动程序配置,并确保您看到的对话框不会被Unexpected_Alert_Behavior的驱动程序设置覆盖。如果你把它关掉了,我会试着打开它,看看它会对你的行为产生什么影响。

  • 如何用";出乎意料的AlertBehavior";硒的能力
  • https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/ie/InternetExplorerDriver.html

我不完全确定是否能解决列出的问题,因为你提到的对话框是dom的一部分,从我所看到的情况来看,"意外"警报行为在该上下文中通常不可见。我相信这也是非常具体的IE实现。

最后,我想我会使用类中的一个方法来为我执行所有的findby。在该方法中,我会使用显式等待来检查随机添加,如果等待未能解决"错误引用",则让它失败来解决实际请求的对象。

  /** By identifier for the error dialog addition.*/
    private static final By ERR_DLG = By.className("error");
    /**
     * Delegate method that will attempt to resolve the occasional page error condition prior to performing the delegate lookup on the WebDriver.
     * <p/>
     * This method will fail on Assert behavior if the error dialog is present on the page.
     * 
     * @param driver WebDriver referenced for the test instance.
     * @param lookup By reference to resolve the desired DOM reference.
     * @return WebElement of the specified DOM.
     */
    private final WebElement safeResolve(WebDriver driver, By lookup) {
        WebDriverWait wait = new WebDriverWait(driver, 1);
        WebElement errDlgRef = null;
        try {
            errDlgRef = wait.until(ExpectedConditions.visibilityOfElementLocated(ERR_DLG));
        } catch (TimeoutException te) {
            //This is actually OK, in that in the majority of cases this is the expected behavior.
            //Granted this is bad form for Unit-Testing, but Selenium at an Integration-Test level changes the rules.
        }
        Assert.assertNull("Unexpected Error Dialog exists in DOM", errDlgRef);
        return driver.findElement(lookup);
    }

这个解决方案有点像锤子,但我认为它会起作用。所有查找都需要通过此方法。如果你在测试中使用ListWebDriver.findElements(By)函数,或者从这个函数中抽象出来,你可能还需要一个类似的方法。

最新更新