As we
all are aware “wait” is a very important aspect while executing our automation
scripts.
Waits in Selenium
In
selenium, we have 3 types of wait
รผ Implicit wait
รผ Explicit wait
รผ Fluent wait
Implicit
waits are used to provide the latency within every test step of the test
script. Thus, the program control would wait for the specified time before
moving the execution control to the next step.
Thus
the implicit waits are applied to all the test steps of the test script. In
other words, we can say that the system would wait for the specified period to
load the desired object into the DOM.
//Create WebDriver instance variable
WebDriver driver;
//Launch browser
driver=new FirefoxDriver();
//Apply implicit wait
driver.manage().timeouts().implicitlyWait(20,
TimeUnit.SECONDS);
Explicit Wait
Explicit
waits are smarter waits than implicit waits. Explicit waits can be applied in
certain instances instead of applying on every web element within the test
script.
Suppose
if we are creating a login script for Gmail. We know that we are most likely to
wait for a few seconds to let the home page load successfully. Thus, in such
cases, explicit waits can be used.
Another important benefit that we get from an explicit wait is that it waits for
maximum for the specified period or a condition to be met. Thus, if our
condition (Let’s say an element should be visible before we click on it) is met
before the specified time has elapsed, then the system control would move
forward rather than waiting for the complete time to elapse to save our
execution time.
Code Sample
public void explicitWait(WebDriver driver)
{
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("element
id"))));
}
In the
above method, WebDriver would wait for the expected condition
(elementToBeClickable) to be met or for the timeout (20 seconds) to occur. As
soon as the condition is met, the WebDriver would execute the next test step.
Fluent Wait
FluentWait
can define the maximum amount of time to wait for a specific condition and the frequency with which to check the condition before throwing an
“ElementNotVisibleException” exception.
To
understand fluent wait let’s take an example where an element in the webpage
sometimes load in 5 seconds, sometime in 10 seconds or sometimes even take 30
seconds. In such cases, the fluent wait is the ideal wait to use as this will
try to find the element at different frequencies until it finds it or the final timer runs out.
The syntax for Fluent wait
Wait
wait = new FluentWait(WebDriver reference)
.withTimeout(Duration.ofSeconds(SECONDS))
.pollingEvery(Duration.ofSeconds(SECONDS))
.ignoring(Exception.class);
Now comes the most important part
can we use implicitly wait() and explicitly wait() together in the test case?
No. we should not.
Before using the explicitly wait(), reset
the implicitly wait() by using the below code.
Create a method for the explicitly wait():
Public void
explicitlyWaitForWebElement(WebDriver driver, WebElement we){
driver.manage.timeouts().implicitywait(0,
TimeUnit.SECONDS); // nullify the time
WebDriverWait wait = new
WebDriverWait(driver, 10);
Wait.until(ExpectedConditions.presenceOfElementLocated(we));
driver.manage().timeouts().implicitywait(DEFAULT_WAIT_4_PAGE,
TimeUnit.SECONDS); // reset the time
}
Always call this function to set the
explicitly wait().
Thank you. For the explanation and we are expecting detailed explanation about fluent wait and real time scenario when we will use fluent wait
Reply