WebDriver Wait
- Implicit Wait sets internally a timeout that will be used for all consecutive WebElement searches. It will try lookup the element again and again for the specified amount of time before throwing an NoSuchElementException if the element could not have been found. The default setting is 0 once set, the implicit wait is set for the life of the webdriver object instance. See the below example code -
public static void turnOnImplicitWait
(WebDriver wd,int timeout)
{
wd.manage().timeouts().implicitlyWait(timeout,
TimeUnit.SECONDS);
}
public static void turnOffImplicitWait
(WebDriver wd)
{
wd.manage().timeouts().implicitlyWait(0,
TimeUnit.SECONDS);
}
- Explicit Wait is a one-timer used by you for a particular search. It is more extendible in the means that you can set it up to wait for any condition you might like. Usually, you can use some of the prebuilt ExpectedConditions to wait for elements to become clickable, visible, invisible, etc., or just write your own condition that suits your needs. See the below example code –
WebDriverWait wait = new
WebDriverWait(driver, 10);
WebElement element =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='tl_login']")));
- FluentWait defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.
// Waiting 30 seconds for an element to be
present on the page, checking
//
for its presence once every 5 seconds.
public
static WebElement fluentWait(final By locator){
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30,
TimeUnit.SECONDS)
.pollingEvery(5,
TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver)
{
return
driver.findElement(locator);
}
});
return foo;
};
No comments:
Post a Comment