What are Selenium Wait Commands? Implicit, Explicit and Fluent Wait in Selenium
In this post, you will learn about different waits in Selenium. Waits are required to tell Selenium to wait for a specific amount for elements on the web page before throwing "No Such Element Exception". There are three types waits in Selenium WebDriver, out of which first two are the most popular.
- Implicit Wait
- Explicit Wait
- Fluent Wait
Implicit Wait in Selenium WebDriver
Implicit wait is used to set a default wait time (say 30 secs) in your automation script to wait for an element on the page.
Key Points
- Implicit wait should be declared in your setup method (at beginning lines of your code).
- Default wait time for Implicit wait is 0 secs.
- Once implicit wait is set, it is applicable for the whole automation script.
- In simple words, you can also say that implicit wait is the maximum time set between two steps/commands of the automation script.
- Webdriver throws "No Such Element Exception" if element is not found in specified time.
Syntax:
// Implicit wait - Set wait of 10 seconds driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Explicit Wait in Selenium WebDriver
Explicit wait can be called as conditional wait. It tells the WebDriver to wait for certain conditions before maximum time exceeded.
Key Points
- Explicit wait is intelligent wait, as it waits certain conditions.
- It applies to only single element and not to whole script.
- It provides a better approach to handle dynamic ajax elements.
- Explicit wait is the best way to replace Thread.sleep() (as static waits are not recommended in automation).
- WebDriver throws "ElementNotVisibleException" if explicit wait fails.
Syntax:
// Explicit Wait WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.alertIsPresent());
All 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()
Fluent Wait in Selenium WebDriver
Fluent wait is kind of conditional wait but with frequency. It means fluent wait is used to wait for a condition with a regular frequency/time interval to be checked before throwing "ElementNotVisibleException".
Example
There might be a scenario where an element X might load with in 30 secs. In this case we can set 5 secs of frequency, so it will check after every 5 secs for the element X. So, ideally it will check for 6 times before throwing "ElementNotVisibleException". If element X loads within 30 secs, program flow would move to the next step of automation script.
Syntax:
// Fluent Wait Wait wait = new FluentWait(WebDriver reference) .withTimeout(timeout, SECONDS) .pollingEvery(timeout, SECONDS) .ignoring(Exception.class); WebElement foo = wait.until(new Function() { public WebElement applyy(WebDriver driver) { return driver.findElement(By.id("foo")); } });
Actions Class in Selenium << Previous || Next >> Take Screenshot in Selenium
Follow Us
Feel free to ask queries or share your thoughts in comments or email us.
Comments
Post a Comment