Wait commands in WebDriver
Listing out the different WebDriver Wait statements that can be useful for an effective scripting and can avoid using the Thread.sleep() comamnds
After few searches and digging into the WebDriver Java doc, I managed to design a mindmap of the different WebDriver commands available
WebDriver.manage().timeouts()
implicitlyWait
WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
The ImplicitWait will tell the webDriver to poll the DOM for a certain duration when trying to find the element, this will be useful when certain elements on the webpage will not be available immediately and needs some time to load.
By default it ill take the value to 0, for the life of the WebDriver object instance through out the test script.
pageLoadTimeout
driver.manage().timeouts().pageLoadTimeout(100, SECONDS);
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.
setScriptTimeout
driver.manage().timeouts().setScriptTimeout(100,SECONDS);
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.
FluentWait
// Waiting 30 seconds for an element to be present on the page, checking // for its presence once every 5 seconds. Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("foo")); } });
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.
ExpectedConditions
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>)));
Please refer below link for expected conditions in Selenium WebDriver:
https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html
Example:
Below shown code will handle the loading of Ajax elements in a web page:
An expectation for checking that an element with text is either invisible or not present on the DOM.
try {
WebDriverWait wait = new WebDriverWait(getFromTestBase.getDriver(),180);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(ajaxElementXpath)));
} catch (Exception e) {
throw e;
}
}
}
Example 2: Below method helps to stops the script execution until page loading is complete
public static void waitForPageToLoad() {try {
(new WebDriverWait(getFromTestBase.getDriver(), 180))
.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return (((org.openqa.selenium.JavascriptExecutor) getFromTestBase
.getDriver())
.executeScript("return document.readyState")
.equals("complete"));
}
});
} catch (Exception e) {
throw e;
}
}
Models a condition that might reasonably be expected to eventually evaluate to something that is neither null nor false.
Examples : Would include determining if a web page has loaded or that an element is visible.
Note that it is expected that ExpectedConditions are idempotent. They will be called in a loop by the WebDriverWait and any modification of the state of the application under test may have unexpected side-effects.
Examples : Would include determining if a web page has loaded or that an element is visible.
Note that it is expected that ExpectedConditions are idempotent. They will be called in a loop by the WebDriverWait and any modification of the state of the application under test may have unexpected side-effects.
WebDriverWait will be used as we used in the Expected conditions code snippet as above.
Sleeper is something same as the Thread.sleep() method, but this with an Abstraction around the thread.sleep() for better testability
thank you for information aboutselenium webdriver
ReplyDelete