wait

ImplicitlyWait命令

目的:Selenium WebDriver借用了来自Watir隐式等待的想法这意味着我们可以告诉Selenium我们希望它在抛出一个无法在页面上找不到元素异常之前等待一段时间我们应该注意,在浏览器打开的整个时间内都会有隐含的等待。这意味着对页面上元素的任何搜索都可能需要设置隐式等待的时间。

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

FluentWait命令

目的:每个FluentWait实例定义等待条件的最大时间量,以及检查条件的频率。此外,用户可以将等待配置为在等待时忽略特定类型的异常,例如在搜索页面上的元素时的NoSuchElementExceptions

// Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 500 millisSeconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofMillis(500))
.ignoring(NoSuchElementException.class);

//wait until the element is returned, the timeout is not returned and throws a TimeOutException
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
}); 

ExpectedConditions命令

目的:建模可能合理预期最终评估为既不为空也不为假的条件。

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

睡眠指挥

目的:这很少使用,因为它总是迫使浏览器等待特定时间。Thread.Sleep绝不是一个好主意,这就是Selenium提供等待原语的原因。如果使用它们,您可以指定更高的超时值,这使得测试更加可靠,而不会减慢它们的速度,因为可以根据需要经常评估条件。

Thread.sleep(1000);

PageLoadTimeout命令

目的:设置在抛出错误之前等待页面加载完成的时间。如果超时为负,则页面加载可能是无限期的。

 driver.manage().timeouts().pageLoadTimeout(100, SECONDS);

SetScriptTimeout命令

用途:设置在抛出错误之前等待异步脚本完成执行的时间。如果超时为负,则允许脚本无限期运行。

 driver.manage().timeouts().setScriptTimeout(100,SECONDS);

  

原文地址:https://www.cnblogs.com/wldan/p/10544881.html