使用Java的Selenium WebDriver测试中等效的waitForVisible / waitForElementPresent? [英] Equivalent of waitForVisible/waitForElementPresent in Selenium WebDriver tests using Java?

查看:1049
本文介绍了使用Java的Selenium WebDriver测试中等效的waitForVisible / waitForElementPresent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用HTMLSelenium测试(使用Selenium IDE或手动创建),您可以使用一些非常方便的命令,例如 WaitForElementPresent WaitForVisible

With "HTML" Selenium tests (created with Selenium IDE or manually), you can use some very handy commands like WaitForElementPresent or WaitForVisible.

<tr>
    <td>waitForElementPresent</td>
    <td>id=saveButton</td>
    <td></td>
</tr>

用Java编写Selenium测试时(Webdriver / Selenium RC-我不确定这里的术语),是否有类似的内置

When coding Selenium tests in Java (Webdriver / Selenium RC—I'm not sure of the terminology here), is there something similar built-in?

例如,检查对话框(需要一段时间才能打开)是可见的......

For example, for checking that a dialog (that takes a while to open) is visible...

WebElement dialog = driver.findElement(By.id("reportDialog"));
assertTrue(dialog.isDisplayed());  // often fails as it isn't visible *yet*

什么是最干净的健壮编码此类检查的方式?

What's the cleanest robust way to code such check?

添加 Thread.sleep()调用遍布的地方将是丑陋和脆弱,你自己的循环似乎也很笨拙......

Adding Thread.sleep() calls all over the place would be ugly and fragile, and rolling your own while loops seems pretty clumsy too...

推荐答案

明显和明确的等待



隐含等待

Implicit and Explicit Waits

Implicit Wait


隐式等待是告诉WebDriver在尝试查找一个或多个元素(如果它们是$)时,在某个
的时间内轮询DOM。 b $ b不能马上获得。默认设置为0.设置后,将为WebDriver对象实例的生命周期设置
隐式等待。

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.



driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

显式等待+ 预期条件


显式等待是您定义的代码,用于在继续执行代码之前等待某个条件
。这个
的最坏情况是Thread.sleep(),它将条件设置为
等待的确切时间段。提供了一些方便的方法来帮助您编写
代码,该代码只会在需要时等待。 WebDriverWait $ $ $ $ b与ExpectedCondition的组合是一种可以实现
的方式。

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.



WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
        ExpectedConditions.visibilityOfElementLocated(By.id("someid")));

这篇关于使用Java的Selenium WebDriver测试中等效的waitForVisible / waitForElementPresent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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