Why are waits required in selenium?
In web automation waits are required as certain elements get loaded on the page, so after triggering an event a page may get loaded successfully but some of its element may still not get loaded. This causes elementNotFound exception.In such cases we will be using Thread.sleep() i.e. a static wait that will halt the test execution for some specified time and then perform the next step. As Thread.sleep() will wait for the specified time no matter if the elements gets visible before the specified amount of time. So, using the Thread.sleep() method is never advisable for UI automation.
To avoid this, selenium provides different types of waits , in most of the cases we will use Implicit, explicit waits and pageLoadTimeout.
Implicit Waits –
An implicit wait when used is set to the WebDriver instance and is applied to all the web elements. In implicit wait the webdriver polls the DOM to check the availability of the webElement and waits till the maximum time specified before throwing NoSuchElementException.Syntax:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(<interger to represent the time>, TimeUnit.<UNIT>);
- where UNIT might be SECONDS, MINUTES, HOURS, DAYS etc.,
- TimeUnit is available from java.util.concurrent Package.
Example:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
In the above example the value 10 specified in implicit wait method is the maximum time in seconds till which webDriver will wait before throwing NoSuchElementException while locating a webElement.
Explicit Waits –
Unlike implicit waits (which is applied on WebDriver instance without any condition), the explicit waits are applied to each and every webElement. In explicit wait certain conditions are defined for which the webDriver instance waits before locating webElements or performing actions on them. Some of the most common conditions specified in explicit waits are-visibilityOfElementLocated, elementToBeClickable, presenceOfElementLocated etc.
Syntax:
WebDriverWait wait = new WebDriverWait(driver, <Time is Seocnds>);
wait.until(ExpectedConditions.<condition>(ElementLocator));
Example:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(ElementLocator));
Here the webDriver instance will wait until the condition specified is met i.e. the visibility Of Element located by the Locator with the maximum wait time of 30 seconds after which if the condition is still not met than it will throw NoSuchElementException exception.
FluentWait Command :
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 asNoSuchElementExceptions when searching for an element on the page.Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
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.driver.manage().timeouts().pageLoadTimeout(100, SECONDS);
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.driver.manage().timeouts().setScriptTimeout(100,SECONDS);
0 comments:
Post a Comment