Selenium测试运行得太快,而不是等待选择单选按钮

Pat*_*Pat 5 java selenium selenium-webdriver

我有一个运行速度太快的Selenium Grid和WebDriver 2.48.2测试.大多数时间测试停止,因为在按下按钮之前未选择单选按钮.

单选按钮是使用基于JSON文件的JavaScript设置的,可以在一个部分内动态创建任意数量的单选按钮.单击"继续"按钮后,该部分将被销毁,并使用新的单选按钮创建一个新部分.

我尝试了隐含的等待但没有成功.

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)

对我有用的唯一解决方案是延迟,以便有足够的时间点击单选按钮.

driver.findElement(By.id("radiobuttonid")).click();
Thread.sleep(delay);
Run Code Online (Sandbox Code Playgroud)

但在我看来,这不是一个理想的解决方案,延迟可能不够长或太长,浪费时间; 可以有任意数量的单选按钮,因此时间会呈指数增长.

我已尝试使用不同的预期条件设置各种显式等待,但没有成功.

我已经尝试等待创建单选按钮,因为它可能不存在(presenceOfElementLocated&elementToBeClickable).我已经尝试等待它被选中(elementToBeSelected).

由于描述简短且容易被误解,我无法确切地找到预期的条件应该做什么.

理想情况下,我希望在单击单选按钮后立即继续测试.如果可能的话,最好的方法是什么?

编辑

L.Bar的建议对我来说不起作用,但确定单选按钮存在非常有用,只是在单选按钮有机会被选中之前点击了继续按钮.

编辑2

这只是为了扩展耶利米的正确答案.我将代码放入一个方法,使其可重用.

private static Predicate<WebDriver> forceSelectionOfElement (final String id)
{
    return new Predicate<WebDriver>()
    {
        @Override
        public boolean apply(WebDriver arg0)
        {
            WebElement element = arg0.findElement(By.id(id));                
            boolean rval = element.isSelected();

            if (!rval) {
                element.click();
            }

            return rval;
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

用法

WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(forceSelectionOfElement("q___01_02_01___1___a"));
Run Code Online (Sandbox Code Playgroud)

记得导入这个命名空间,我花了很长时间才弄清楚我导入了错误的一个:)

import com.google.common.base.Predicate;
Run Code Online (Sandbox Code Playgroud)

Jer*_*iah 4

我也遇到过类似的问题,但就我而言,我的“点击”事件在某处丢失了。我的测试将继续,点击代码将触发,但元素的状态永远不会明显改变,测试将失败。我最终通过添加一个自定义谓词来利用 WebDriverWait 的循环行为,该谓词对所选状态更加持久。

WebDriverWait wait = new WebDriverWait(driver, 5);
        final By lookup = By.id("radio");
        LOG.debug("Wait for radio to be clickable.");
        wait.until(ExpectedConditions.elementToBeClickable(lookup)); //I assume this implies visibility.
        LOG.debug("Element clickable.  Proceeding into click behavior.");
        wait.until(new Predicate<WebDriver>() {
            @Override
            public boolean apply(WebDriver arg0) {
                LOG.debug("Resolving 'myId' Element");
                WebElement radio = arg0.findElement(lookup);                
                boolean rval = radio.isSelected();
                LOG.debug("Element Resolved and has a state of " + (rval ? "selected" : "not selected"));
                if (!rval) {
                    LOG.debug("Clicking on the Element!");
                    radio.click();
                }
                //If we return false we will loop.  So the first time through we let this click the radio for us.
                //Second time through we should find that the element is clicked.  If it's not we click it again until it represents the state we're wanting.
                return rval;;
            }});
Run Code Online (Sandbox Code Playgroud)

自定义谓词将

  1. 解析WebElement
  2. 捕获当前选定状态
  3. 如果未选择,请单击 WebElement
  4. 返回当前选择的状态。

因此,在元素的单击状态达到我想要的状态之前,这不应允许测试继续进行。到目前为止,基本上已经完成了我想要的事情。

还值得注意的是,Thread.sleep 被视为不是 selenium 交互的最佳实践。这就是 ExplicitWait 概念要解释的内容。我已经链接了我知道正在讨论的最新主题之一。

Thread.sleep 可以工作,但隐式等待、webdriverwait 和流畅等待却不能?

祝你好运!