Selenium 简明教程
Selenium WebDriver - Wait Support
Selenium Webdriver 可与 implicit 、 fluent waits 和 @{s11} 等各种等待配合使用,以实现同步并提供等待支持。等待主要应用在测试中,以处理在 Selenium 测试需要其出现在页面或 Dom 中时 Web 元素不可用的情况。
Selenium Webdriver can be used with various waits like the explicit, implicit, and fluent waits to achieve synchronization and provide Wait support. The waits are mainly applied in the tests to deal with situation when a web element is unavailable at the time, the Selenium test expects it to be available on the page or in the Dom.
整个页面加载之前通常会存在一些延迟时间,并且 Web 元素在网页上完全可用。Selenium Webdriver 中的等待有助于阻止测试执行,直到元素以其正确状态出现在/消失在网页上。
Often there lies some lag time before the whole page loads, and web elements are completely available on the web page. The waits available in Selenium Webdriver help to hold back the test execution till an element appears/disappears on a web page in its correct state.
Basic Waits Available in Selenium Webdriver
Selenium Webdriver 中提供了多种等待。它们如下所示:
There are multiple waits available in Selenium Webdriver. They are listed below −
Implicit Wait
这是 Selenium 中提供的默认等待。这是一种适用于整个驱动程序会话的全局等待。默认等待时间为 0,这意味着如果没有找到元素,则会立即抛出错误。
It is the default wait available in Selenium. It is a kind of global wait applicable to the whole driver session. The default wait time is 0, meaning if an element is not found, an error will be thrown straight away.
Explicit Wait
这类似于添加到代码中的循环,该循环将轮询网页以等待特定场景变为 true,然后退出循环并移到下一行代码。
It is similar to loops added to code that poll the web page for a particular scenario to become true prior exiting the loop and moving to the next line of code.
Fluent Wait
这是驱动程序等待元素达到特定条件的最高时间。它还决定了驱动程序在定位元素或抛出异常之前进行验证(轮询间隔)的时间间隔。Fluent wait 是自定义显式等待,它提供了一个选项,可以在发生异常时使用自定义消息自动处理特定异常。FluentWait 类用于向测试中添加 Fluent 等待。
This is the maximum time the driver waits for a specific condition for an element to be true. It also determines the interval at which the driver will verify(polling interval) prior to locating an element or throwing an exception. The fluent wait is a customized explicit wait which gives the option to handle specific exceptions automatically along with customized messages when an exception occurs. The FluentWait class is used to add fluent waits to tests.
Wait wt = new FluentWait(driver)
.withTimeout(20, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(ElementNotInteractableException.class);
wt.until(ExpectedConditions.titleIs("Tutorialspoint"));
在上例中,指定了超时时间和轮询间隔,这意味着驱动程序将等待 20 秒并在超时时间内以 5 秒的间隔进行轮询,直到满足 Tutorialspoint 浏览器标题条件。如果条件在该时间范围内未得到满足,将抛出异常,否则将执行下一步。
In the above example, timeout and polling interval are specified meaning that the driver would wait for 20 seconds and perform a polling in an interval of 5 seconds within the timeout time for the Tutorialspoint browser title condition to be met. If the condition is not satisfied, within that time frame, an exception will be thrown, else the next step will be executed.
Example 1 - Explicit Wait
我们来看一下下图的示例,我们首先单击 Click Me 按钮。
Let us take an example of the below image, where we would first click on the Click Me button.
在单击“Click Me”后,我们将使用显式等待并等待文本 You have done a dynamic click 出现在网页上。
After clicking on the Click Me, we would use explicit wait and wait for the presence of the text You have done a dynamic click to be available on a web page.
代码实现
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
public class ExplicitsWait {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// launching a browser and open a URL
driver.get("https://www.tutorialspoint.com/selenium/practice/buttons.php");
// identify button then click on it
WebElement l = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[1]"));
l.click();
// Identify text
WebElement e = driver.findElement(By.xpath("//*[@id='welcomeDiv']"));
// explicit wait to expected condition for presence of a text
WebDriverWait wt = new WebDriverWait(driver, Duration.ofSeconds(2));
wt.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='welcomeDiv']")));
// get text
System.out.println("Get text after clicking: " + e.getText());
// Quitting browser
driver.quit();
}
}
Example 2 - Fluent Wait
我们再来看一下以下页面的示例,我们首先单击 Color Change 按钮。
Let us take another example of the below page where we would first click the Color Change button.
在单击 Color Change 后,我们将使用 Fluent 等待并等待按钮 Visible After 5 Seconds 出现在网页上。
After clicking on the Color Change, we would use fluent wait and wait for the presence of the button Visible After 5 Seconds to be available on a web page.
代码实现
Code Implementation
package org.example;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import java.time.Duration;
public class Fluentwts {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// launching a browser and open a URL
driver.get("https://www.tutorialspoint.com/selenium/practice/dynamic-prop.php");
// identify button then click
WebElement l = driver.findElement(By.xpath("//*[@id='colorChange']"));
l.click();
// fluent wait of 6 secs till other button appears
Wait<WebDriver> w = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(20))
.pollingEvery(Duration.ofSeconds(6))
.ignoring(NoSuchElementException.class);
WebElement m = w.until(ExpectedConditions.visibilityOfElementLocated
(By.xpath("//*[@id='visibleAfter']")));
// checking button presence
System.out.println("Button appeared: " + m.isDisplayed());
// Quitting browser
driver.quit();
}
}
Conclusion
这就结束了我们对 Selenium Webdriver Wait Support 教程的全面了解。我们首先描述了 Selenium Webdriver 中提供的基本等待,并给出了显式等待和 Fluent 等待在 Selenium Webdriver 中的示例。这使你深入了解了 Selenium Webdriver Wait Support。明智的做法是不断练习你所学的内容,并探索与 Selenium 相关的其他内容,以加深你的理解并拓宽你的视野。
This concludes our comprehensive take on the tutorial on Selenium Webdriver Wait Support. We’ve started with describing Basic Waits Available in Selenium Webdriver, and examples to illustrate explicit, and fluent waits in Selenium Webdriver. This equips you with in-depth knowledge of the Selenium Webdriver Wait Support. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.