In automation testing, Selenium WebDriver wait commands direct test execution to pause for a certain length of time before moving onto the next step. Listing out the different WebDriver Wait statements that can be useful for an effective scripting.
Types of Waits in Selenium
1.Implicit Waits
The main function of implicit wait is to tell the web driver to wait for some time before throwing a Exception. We should note that implicit waits will be in place for the entire time the browser is open. This means that any search for elements on the page could take the time the implicit wait is set for.
Syntax:
driver. manage().timeouts().implicitlyWait(TimeOut,TimeUnit.SECONDS);
2.Fluent Wait Command
Fluent Wait in Selenium marks the maximum amount of time for Selenium WebDriver to wait for a certain condition (web element) becomes visible. 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.
Syntax:
Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
3. PageLoadTimeout Command
Sets the amount of time to wait for a page-load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.
Syntax:
driver.manage().timeouts().pageLoadTimeout(100, SECONDS);
4.SetScriptTimeout Command
Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.
Syntax:
driver.manage().timeouts().SetScriptTimeout(100, SECONDS);
Comentarios