使用WebDriver暂时绕过隐式等待 [英] Temporarily bypassing implicit waits with WebDriver

查看:328
本文介绍了使用WebDriver暂时绕过隐式等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用隐式等待时, 按此处建议 ,我仍然有时想断言立即不可见或不存在元素。

When using implicit waits, as advised here, I still sometimes want to assert the immediate invisibility or non-existence of elements.

换句话说,我知道某些元素应该被隐藏,并且希望我的测试能够快速快速,而不需要花费几秒钟,因为(否则有用)隐式等待。

In other words, I know some elements should be hidden, and want my tests make that assertion fast, without spending several seconds because of the (otherwise useful) implicit wait.

我尝试过的一件事是这样的辅助方法:

One thing I tried was a helper method like this:

// NB: doesn't seem to do what I want
private boolean isElementHiddenNow(String id) {
    WebDriverWait zeroWait = new WebDriverWait(driver, 0);
    ExpectedCondition<Boolean> c = invisibilityOfElementLocated(By.id(id));
    try {
        zeroWait.until(c);
        return true;
    } catch (TimeoutException e) {
        return false;
    }
}

但在上面的代码中,调用 until()仅在隐含的等待时间过后返回,即它没有做我想要的。

But in the above code, the call to until() only returns after the implicit wait time has passed, i.e., it doesn't do what I wanted.

这是我到目前为止找到的唯一方法:

This is the only way I've found so far that works:

@Test
public void checkThatSomethingIsNotVisible()  {
    turnOffImplicitWaits();
    // ... the actual test
    turnOnImplicitWaits();
}

...例如 turnOffImplicitWaits()是常见Selenium超类中的助手:

... where e.g. turnOffImplicitWaits() is a helper in common Selenium superclass:

protected void turnOffImplicitWaits() {
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
}

但我觉得这不是很优雅。 有没有更干净的方法偶尔绕过隐式等待?

But that is not very elegant, I think. Is there any cleaner way to bypass the implicit wait occasionally?

推荐答案

鉴于Selenium没有似乎直接提供了我想要的东西(基于Mike Kwan和Slanec的说法),这个简单的帮助方法就是我现在所使用的方法:

Given that Selenium doesn't seem to offer what I want directly (based on what Mike Kwan and Slanec said), this simple helper method is what I went with for now:

protected boolean isElementHiddenNow(String id) {
    turnOffImplicitWaits();
    boolean result = ExpectedConditions.invisibilityOfElementLocated(By.id(id)).apply(driver);
    turnOnImplicitWaits();
    return result;
}

private void turnOffImplicitWaits() {
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
}

private void turnOnImplicitWaits() {
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

如果元素被隐藏或根本不存在,则该方法返回true;如果可见,则返回false。无论哪种方式,检查都会立即完成。

If the element is hidden or not present at all, the method returns true; if it is visible, returns false. Either way, the check is done instantly.

使用上述内容至少比通过调用 turnOffImplicitWaits()乱丢测试用例本身更清洁 turnOnImplicitWaits()

Using the above is at least much cleaner than littering the test cases themselves with calls to turnOffImplicitWaits() and turnOnImplicitWaits().

另请参阅这些针对罚款调整版本的答案相同的方法:

See also these answers for fined-tuned versions of the same approach:

  • Using try-finally to turn implicit waits back on
  • Using By locator as the parameter

这篇关于使用WebDriver暂时绕过隐式等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆