Skip to main content

Quality Assurance

Guide to Implicit, Explicit, and Fluent Waits in Selenium

tiethnic colleagues sitting at desk looking at laptop computer in office. stock photo

One of the most Crucial Abilities to understand if you want to become an expert Selenium WebDriver user is the use of the Wait Commands. They are necessary for running test scripts and for locating and fixing problems with Time Latency in web elements.

 

What do we need Wait?

By synchronizing our test to make sure Selenium WebDriver Waits until the application is available before doing a given action, we can address the predicted timing issues.

Due to the use of Ajax and JavaScript in the application, when a page is loaded by the browser, the components that we want to interact with may load at various times. As a result, we must offer some waits before performing an action on a specific element.

Picture1

 

Implicit Wait

We are instructing WebDriver that it must wait a Predetermined Period Of Time before anticipating that the element will be displayed after loading.

So, it will attempt to find the element After Waiting for a predetermined amount of time. If still element is not found, NoSuchElementFound Exception will be thrown.

Syntax

driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);

 

Example-

WebDriver driver= new FirefoxDriver();

driver.get("https://perficient.com");

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Explicit Wait

We are instructing WebDriver to wait until a Certain Amount Of Time has passed before expecting the element to be visible after loading.

Therefore, it will attempt to find the element after waiting for a predetermined amount of time. NoSuchElementFound Exception will be fired if the element is still missing.

Syntax-

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(“ID”)));

 

In order to declare explicit wait, one must use “ExpectedConditions”. A few Expected Conditions that can be used in Explicit Wait are given below.

  • elementToBeClickable()
  • elementToBeSelected()
  • alertIsPresent()
  • elementSelectionStateToBe()
  • invisibilityOfTheElementLocated()
  • invisibilityOfElementWithText()
  • visibilityOfAllElements()
  • visibilityOfAllElementsLocatedBy()
  • visibilityOfElementLocated()

Example-

// explicit wait - to wait for the button to be click-able

WebDriverWait wait = new WebDriverWait(driver,30);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'BUTTON')]")));

 

Fluent Wait

In order to determine whether the element is displayed on the page, we are instructing WebDriver to query the DOM Every ‘n’ Seconds. The polling frequency for explicit waits is 500ms by Default. Moreover, depending on your demands, the fluent wait Polling Frequency can be Altered. If you do not locate an element while polling, you can ignore any exception, such as the NoSuchElementException, etc.

Syntax-

FluentWait wait = new FluentWait(driver)

.withTimeout(TotalTime, TimeUnit.SECONDS)

.pollingEvery(pollingTime, TimeUnit.SECONDS)

.ignoring(NoSuchElementException.class);

Example-

FluentWait wait = new FluentWait(driver)

.withTimeout(15, TimeUnit.SECONDS)

.pollingEvery(3, TimeUnit.SECONDS)

.ignoring(NoSuchElementException.class);

 

Conclusion:

The different waits in Selenium are Fluent Wait, Explicit Wait, and Implicit Wait. The objects that are loaded at different times determine how these waits are used entirely. The usage of Thread.Sleep() is never advised when testing or creating a framework.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Mangesh Sonwane

Mangesh Sonwane is an Associate Technical Consultant at Perficient in Nagpur GDC. He has an experience of 1+ years in Drupal and AEM. Apart from this passionate about learning new changes and expanding his knowledge in Automation. He is very committed to his work and is always ready to face any challenging projects.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram