Difference Between Implicit, Explicit & Fluent Wait
Implicit Wait
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.
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"));
Fluent Wait
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.
// Waiting 30 seconds
for an element to be present on the page, checking
// for its presence once every 5 seconds.
Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function() {
public WebElement
apply(WebDriver driver) {
return
driver.findElement(By.id("foo"));
}
});
Explicit Wait
It is more extendible in the means that you can set it up to
wait for any condition you might like. Usually, you can use some of the
prebuilt ExpectedConditions to wait for elements to become clickable, visible,
invisible, etc.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element =
wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Difference Between Implicit, Explicit and Fluent Wait
Implicit Wait:
During Implicit wait if the Web Driver cannot find it immediately because of
its availability, the WebDriver will wait for mentioned time and it will not
try to find the element again during the specified time period. Once the
specified time is over, it will try to search the element once again the last
time before throwing exception. 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.
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.
public WebElement getWhenVisible(By locator, int timeout) {
WebElement element = null;
WebDriverWait wait = new WebDriverWait(driver, timeout);
element =
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
return element;
}
public void clickWhenReady(By locator, int timeout) {
WebElement element =
null;
WebDriverWait wait =
new WebDriverWait(driver, timeout);
element =
wait.until(ExpectedConditions.elementToBeClickable(locator));
element.click();
}
Comments
Post a Comment