Why do users need Selenium Wait?
Most web applications are developed with Ajax and Javascript. When a page loads on a browser, the various web elements that someone wants to interact with may load at various time intervals.
This obviously creates difficulty in identifying any element. On top of that, if an element is not located then the “ElementNotVisibleException” appears. Selenium Wait commands help resolve this issue. Read more about the Common Exceptions in Selenium.
Implicit Wait in Selenium
Implicit Wait directs the Selenium WebDriver to wait for a certain measure of time before throwing an exception. Once this time is set, WebDriver will wait for the element before the exception occurs.
Once the command is in place, Implicit Wait stays in place for the entire duration for which the browser is open. Its default setting is 0, and the specific wait time needs to be set by the following protocol.
To add implicit waits in test scripts, import the following package.
import java.util.concurrent.TimeUnit;
Syntax
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Add the above code into the test script. It sets an implicit wait after the instantiation of WebDriver instance variable.
Explicit Wait in Selenium
By using Explicit Wait command, the WebDriver is directed to wait until a certain condition occurs before proceeding with executing the code.
Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load. If one sets an implicit wait command, then the browser will wait for the same time frame before loading every web element. This causes an unnecessary delay in executing the test script.
Explicit wait is more intelligent, but can only be applied for specified elements. However, it is an improvement on implicit wait since it allows the program to pause for dynamically loaded Ajax elements.
In order to declare explicit wait, one has to use “ExpectedConditions”. The following Expected Conditions can be used in Explicit Wait.
- alertIsPresent()
- elementSelectionStateToBe()
- elementToBeClickable()
- elementToBeSelected()
- frameToBeAvaliableAndSwitchToIt()
- invisibilityOfTheElementLocated()
- invisibilityOfElementWithText()
- presenceOfAllElementsLocatedBy()
- presenceOfElementLocated()
- textToBePresentInElement()
- textToBePresentInElementLocated()
- textToBePresentInElementValue()
- titleIs()
- titleContains()
- visibilityOf()
- visibilityOfAllElements()
- visibilityOfAllElementsLocatedBy()
- visibilityOfElementLocated()
To use Explicit Wait in test scripts, import the following packages into the script.
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
Then, Initialize A Wait Object using WebDriverWait Class.
WebDriverWait wait = new WebDriverWait(driver,30);
Here, the reference variable is named <wait> for the <WebDriverWait> class. It is instantiated using the WebDriver instance. The maximum wait time must be set for the execution to layoff. Note that the wait time is measured in seconds.
Refer this javadocs for each method explanation
http://javadox.com/org.seleniumhq.selenium/selenium-support/2.53.0/org/openqa/selenium/support/ui/WebDriverWait.html
http://javadox.com/org.seleniumhq.selenium/selenium-support/2.53.0/org/openqa/selenium/support/ui/ExpectedConditions.html
Examples Jmeter webdriver scripts to show explicit wait
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import java.util.concurrent.TimeUnit;
WebDriverWait wait = new WebDriverWait(WDS.browser, 20); // explicit wait
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(“p[ng-if^=’vm.assetCountWithoutLocation’]”))); //An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(“p[ng-if^=’vm.assetCountWithoutLocation’]”))); //An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
wait.until(ExpectedConditions.textToBePresentInElement(WDS.browser.findElement(By.id(“displayed-asset-count”)),”50″)); //An expectation for checking if the given text is present in the element that matches the given locator.
wait.until(ExpectedConditions.elementToBeClickable(By.id(“toplevel-dropdown”)));
//An expectation for checking an element is visible and enabled such that you can click it.