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.
You will discover various Selenium wait types in this blog:
Need of Waits In Selenium
Implicit Wait
Explicit Wait
Fluent Wait
Difference Between Implicit Wait Vs Explicit Wait
Need of Waits In Selenium
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.
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”. 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. 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);
Difference between implicit and explicit wait commands.
Implicit Wait
Explicit Wait
It is typically employed when you are positive that the element will be shown at a specific moment.
It is typically used when you are unsure about when an element will be seen. It encounters dynamic nature.
Contrary to explicit, we are unable to wait based on a given criteria like element selectability or clickability.
We can specify the wait in explicit based on a particular situation.
It is automatically applied to all the script’s elements.
It only applies to a certain component that is unique to a particular condition.
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 is never advised. Sleep() is used when testing or creating a framework.
Leave A Comment