The different WebDriver Wait statements that can be useful for an effective scripting are listed below. It is a bad practice to use Thread.sleep() command because it makes the script slow and doesn't work with unpredictable environments.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
Wait wait = new FluentWait(driver);
.withTimeout(30, Timeunit.SECONDS);
.pollingEvery(5, Timeunit.SECONDS);
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(<someid>)));
driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
ImplicitlyWait Command
Purpose: We can tell Selenium that we would like it to wait for a certain amount of time before throwing an exception that it cannot find the element on the page. 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.WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
FluentWait Command
Purpose: Each FluentWait instance 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.Wait wait = new FluentWait(driver);
.withTimeout(30, Timeunit.SECONDS);
.pollingEvery(5, Timeunit.SECONDS);
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});
ExpectedConditions Command
Purpose: Models a condition that might reasonably be expected to eventually evaluate to something that is neither null nor false.WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(<someid>)));
ExpectedConditions.presenceOfElementLocated(By.id(“elm”)); - returns WebElement
ExpectedConditions.visibilityOfElementLocated(By.id("elm")); - returns WebElement
ExpectedConditions.alertIsPresent(); - returns Alert object
ExpectedConditions.invisibilityOfElementLocated(By.id("elm")); - returns boolean
For more information click here
PageLoadTimeout Command
Purpose: 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.driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
setScriptTimeout Command
Purpose: 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.
driver.manage().timeouts().setScriptTimeout(100, TimeUnit.SECONDS);
return new ExpectedCondition<WebElement>(){
public WebElement apply(WebDriver driver1){
try{
return driver1.findElement(by);
}catch(Exception e){
return null;
}
}};
}
WebElement element = wait.until(linkpresent(By.id(<someid>)));
Code to create your own ExpectedCondition command
public static ExpectedCondition<WebElement> linkpresent(By by){return new ExpectedCondition<WebElement>(){
public WebElement apply(WebDriver driver1){
try{
return driver1.findElement(by);
}catch(Exception e){
return null;
}
}};
}
WebElement element = wait.until(linkpresent(By.id(<someid>)));
Comments
Post a Comment