Different kinds of Wait in selenium webdriver are
1. Thread Sleep
Purpose: This is rarely used, as it always force the browser to wait for a specific time. Thread.Sleep is never a good idea and that’s why Selenium provides wait primitives. If you use them you can specify much higher timeout value which makes tests more reliable without slowing them down as the condition can be evaluated as often as it’s required.
Thread.Sleep (1000);
2. Set Script Timeout
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,SECONDS);
3. Page Load Timeout
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, SECONDS);
4. Implicit Wait
Purpose: Selenium WebDriver has borrowed the idea of implicit waits from Watir. This means that 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.
Import Statements:import java.util.concurrent.TimeUnit – To be able to access and apply implicit wait in our test scripts, we are bound to import this package into our test script.
WebDriver driver => new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://facebook.com"); WebElement Element = driver.findElement(By.id("Element"));
Include the above line of code into your test script soon after instantiation of WebDriver instance variable. Thus, this is all that is required to set an implicit wait into your test script.
Code Walkthrough:
The implicit wait mandates to pass two values as parameters. The first argument indicates the time in the numeric digits that the system needs to wait. The second argument indicates the time measurement scale. Thus, in the above code, we have mentioned the “30” seconds as default wait time and the time unit has been set to “seconds”.
When should we use implicit waits?
Normally, it is not recommended to use implicit waits, when we can use explicit waits or fluent waits.
5. Explicit Wait
Purpose: The explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an “ElementNotVisibleException” exception
The explicit wait is an intelligent kind of wait, but it can be applied only for specified elements. Explicit wait gives better options than that of an implicit wait as it will wait for dynamically loaded Ajax elements.
Once we declare explicit wait we have to use “ExpectedCondtions” or we can configure how frequently we want to check the condition using Fluent Wait. These days while implementing we are using Thread.Sleep() generally it is not recommended to use
In the below example, we are creating reference wait for “WebDriverWait” class and instantiating using “WebDriver” reference, and we are giving a maximum time frame of 20 seconds.
Import Statements:
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/section/div[2]")));
In the above example, wait for the amount of time defined in the “WebDriverWait” class or the “ExpectedConditions” to occur whichever occurs first.
The above Java code states that we are waiting for an element for the time frame of 20 seconds as defined in the “WebDriverWait” class on the webpage until the “ExpectedConditions” are met and the condition is “visibilityofElementLocated”.
The following are the Expected Conditions that can be used in Explicit Wait
alertIsPresent() elementSelectionStateToBe() elementToBeClickable() elementToBeSelected() frameToBeAvaliableAndSwitchToIt() invisibilityOfTheElementLocated() invisibilityOfElementWithText() presenceOfAllElementsLocatedBy() presenceOfElementLocated() textToBePresentInElement() textToBePresentInElementLocated() textToBePresentInElementValue() titleIs() titleContains() visibilityOf() visibilityOfAllElements() visibilityOfAllElementsLocatedBy() visibilityOfElementLocated()
When should we use explicit waits?
We would normally use explicit wait if an element takes a long time to load. We also used explicit wait to check CSS property of an element (presence, clickability. etc) which can change in Ajax applications.When should we use explicit waits?
We would normally use explicit wait if an element takes a long time to load. We also used explicit wait to check CSS property of an element (presence, clickability. etc) which can change in Ajax applications.
6. Fluent Wait
Purpose:
The fluent wait is used to tell the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an “ElementNotVisibleException” exception.
Let’s consider a scenario where an element is loaded at different intervals of time. The element might load within 10 seconds, 20 seconds or even more then that if we declare an explicit wait of 20 seconds. It will wait till the specified time before throwing an exception. In such scenarios, the fluent wait is the ideal wait to use as this will try to find the element at different frequency until it finds it or the final timer runs out.
Wait wait = new FluentWait(WebDriver reference) .withTimeout(timeout, SECONDS) .pollingEvery(timeout, SECONDS) .ignoring(Exception.class);
Code Workthrough:
The frequency with which FluentWait has to check the conditions defined.
Ignore specific types of exception waiting such as NoSuchElementExceptions while searching for an element on the page. The maximum amount of time to wait for a condition
When should we use FluentWait?
When you try to test the presence of an element that may appear after every x seconds/minutes.
Difference Between Implicit, Explicit and Fluent Wait
Implicit Wait:
During Implicit wait if the Web Driver cannot find it immediately because of its availability, it will keep polling (around 250 milli seconds) the DOM to get the element. If the element is not available within the specified Time an NoSuchElementException will be raised. The default setting is zero. Once we set a time, the Web Driver waits for the period of the WebDriver object instance.
Explicit Wait:
There can be instance when a particular element takes more than a minute to load. In that case you definitely not like to set a huge time to Implicit wait, as if you do this your browser will going to wait for the same time for every element.
To avoid that situation you can simply put a separate time on the required element only. By following this your browser implicit wait time would be short for every element and it would be large for specific element.
Fluent Wait:
Let’s say you have an element which sometime appears in just 1 second and some time it takes minutes to appear. In that case it is better to use fluent wait, as this will try to find element again and again until it find it or until the final timer runs out.
Solutions:
We always get confuse when it comes to using Wait commands, to better understand it we need to remember that there is a difference between several scenarios:
An element not being present at all in the DOM.
An element being present in the DOM but not visible.
An element being present in the DOM but not enabled. (i.e. clickable)
There are pages which get displayed with the JavaScript, the elements are already present in the browser DOM, but are not visible. The implicit wait only waits for an element to appear in the DOM, so it returns immediately, but when you try to interact with the element you get a NoSuchElementException. You could test this hypothesis by writing a helper method that explicit wait for an element to be visible or clickable.
For more information on Wait in Selenium Webdriver, Please drop an email to- info@oditeksolutuons.com