It is required so that our AUT and testing tool are running in sync with each other.For example if our application is taking time to load but our automation tool commands are running fast,than this would result in error. To make sure selenium/UFT waits for the application controls to load, so that tool can take an action on them we need synchronization. In simple words using wait, we are telling tool engine to wait for a certain amount of time before going to the next line of code or step.It plays a significant role in dynamic websites using technologies like Angular JS and Ajax
How to achieve it?
Support to work with wait commands is available under the package,
org.openqa.selenium.support.ui (Thus we need to import this package in our code)
Note:- We can check the classes under the package from “Package Explorer” in Eclipse.
Package Explorer – Referenced Libraries – Selenium-java-version no .Jar -org.openqa.selenium.support.ui (Expand this and click on the available classes to view the structure)
Open Package Explorer(Window – Show View)
|Package for Wait commands|
Selenium WebDriver has borrowed the idea of implicit waits from Watir(web application testing in Ruby). It means that we can tell Selenium that we would like it to wait for a certain amount of time before throwing an exception (when it cannot find the element on the page). We should keep in mind, that
Implicit waits will be in place for the entire time the browser is open. This means selenium will wait maximum of implicit wait time before throwing an exception error, but incase that element exists before that maximum time also, still it will be waiting for that much time.
Logic:-Selenium checks the control in first go, incase it is not able to find the control it will wait for the given implicit time, and then try again, after the end of the wait time to look for the control before throwing an exception. By default its value is 0 .
Limitation: – implicit wait will be applied to all the elements of the test case by default.
It accepts 2 params, time to wait = 20 and TimeUnit which is seconds in our case.Thus here,It asks selenium to wait for 20 seconds for all the controls before throwing an error.
|Applying Implicit Wait|
|Setting Time in Seconds for Implicit wait|
We are explicitly making the execution of selenium code to wait, for a certain condition to occur before, proceeding further in the code.
Why Explicit Wait?
We know implicit wait applies to all the controls of a browser ,but lets take a example where in one control takes bit longer to load compared to others.Does this means we should increase the time for implicit wait to accommodate it ?
Answer is strict no, and we would only apply explicit wait on this(slower) control with an expected condition.
It is applicable to the given control and waits to the maximum amount of time or move forward incase the given condition is met.
WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully
Note:- we need to import the webdriverwait class and expectedCondition Packages
button is enabled:elementToBeClickable
textbox is visible:visibilityOf
|ExpectedConditions in Selenium|
Note:- We can achieve synchronization using following approach also
Fluent wait = Explicit Wait + frequency with which control need to be checked on application + exceptions to be ignored like
It 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.
Package :- org.openqa.selenium.support.ui.FluentWait
It is mostly used, while working with AJAX applications as well as in scenarios when element load time fluctuating.
It is again an extension of Implicit wait but it used in page loading.It sets the amount of time Selenium to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.
waits for 20 seconds for page to load
Note: – we can achieve synchronization using Thread.Sleep, method of Java.However this is not recommended methodology. As it is same like asking selenium to stop executing for given X milliseconds so that our control is loaded and it is not guaranteed that within that hard coded X value, control is fully loaded and our test code can execute safely.
Ex: Thread. Sleep (20000);
Here the execution is halted for 20 Sec., even if the object we are looking exists at that instant, so it unnecessary waits of 20 sec.
Control exists after 10 sec. So here also selenium unnecessarily waits for more 10 sec.