Learn Selenium Wait Commands: Implicit, Explicit and Fluent Wait
In this post, you will learn about different waits in Selenium. Waits are required to tell Selenium to wait for a specific amount of elements on the web page before throwing "No Such Element Exception". There are three types of waits in Selenium WebDriver, out of which the first two are the most popular.
- Implicit Wait
- Explicit Wait
- Fluent Wait
1. Selenium WebDriver Wait Commands
1.1. Implicit Wait in Selenium WebDriver
The 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
- The implicit wait should be declared in your setup method (at the beginning lines of your code).
- The default wait time for Implicit wait is 0 secs.
- Once the implicit wait is set, it is applicable to 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 a "No Such Element Exception" if an element is not found in the specified time.
Syntax:
// Implicit wait - Set wait of 10 seconds driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
1.2. Explicit Wait in Selenium WebDriver
An explicit wait can be called a conditional wait. It tells the WebDriver to wait for certain conditions before the maximum time is exceeded.
Key Points
- The explicit wait is an intelligent wait, as it waits for certain conditions.
- It applies to only a single element and not to the whole script.
- It provides a better approach to handling dynamic Ajax elements.
- The 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()
1.3. Fluent Wait in Selenium WebDriver
The fluent wait is a kind of conditional wait but with frequency. It means the 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 element X might load within 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 6 times before throwing "ElementNotVisibleException". If element X loads within 30 secs, the program flow would move to the next step of the 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
Author
Vaneesh Behl
Passionately writing and working in Tech Space for more than a decade.
Follow Techlistic
Feel free to ask queries or share your thoughts in comments or email us.
Comments
Post a Comment