Waits | Explicit wait
Explicit waits allow you to wait for a specific condition to be met before proceeding with the test.
The ExpectedConditions class is under the package org.openqa.selenium.support.ui
public static ExpectedCondition<java.lang.Boolean> titleIs(java.lang.String title) An expectation for checking the title of a page. Parameters: title – the expected title, which must be an exact match Returns: true when the title matches, false otherwise
public static ExpectedCondition<java.lang.Boolean> titleContains(java.lang.String title) An expectation for checking that the title contains a case-sensitive substring Parameters: title – the fragment of title expected Returns: true when the title matches, false otherwise
public static ExpectedCondition<java.lang.Boolean> urlToBe(java.lang.String url) An expectation for the URL of the current page to be a specific url. Parameters: url – the url that the page should be on Returns: true when the URL is what it should be
public static ExpectedCondition<java.lang.Boolean> urlContains(java.lang.String fraction) An expectation for the URL of the current page to contain specific text. Parameters: fraction – the fraction of the url that the page should be on Returns: true when the URL contains the text
public static ExpectedCondition<java.lang.Boolean> urlMatches(java.lang.String regex) Expectation for the URL to match a specific regular expression Parameters: regex – the regular expression that the URL should match Returns: true if the URL matches the specified regular expression
public static ExpectedCondition<WebElement> presenceOfElementLocated(By locator) 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. Parameters: locator – used to find the element Returns: the WebElement once it is located
public static ExpectedCondition<WebElement> visibilityOfElementLocated(By locator) 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. Parameters: locator – used to find the element Returns: the WebElement once it is located and visible
public static ExpectedCondition<java.util.List<WebElement>> visibilityOfAllElementsLocatedBy(By locator) An expectation for checking that all elements present on the web page that match the locator are visible. Visibility means that the elements are not only displayed but also have a height and width that is greater than 0. Parameters: locator – used to find the element Returns: the list of WebElements once they are located
public static ExpectedCondition<WebElement> visibilityOf(WebElement element) An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. Parameters: element – the WebElement Returns: the (same) WebElement once it is visible
public static ExpectedCondition<java.util.List<WebElement>> presenceOfAllElementsLocatedBy(By locator) An expectation for checking that there is at least one element present on a web page. Parameters: locator – used to find the element Returns: the list of WebElements once they are located
public static ExpectedCondition<java.lang.Boolean> textToBePresentInElement(WebElement element, java.lang.String text) An expectation for checking if the given text is present in the specified element. Parameters: element – the WebElement, text – to be present in the element Returns: true once the element contains the given text
public static ExpectedCondition<WebElement> elementToBeClickable(By locator) An expectation for checking an element is visible and enabled such that you can click it. Parameters: locator – used to find the element Returns: the WebElement once it is located and clickable (visible and enabled)
public static ExpectedCondition<WebElement> elementToBeClickable(WebElement element) An expectation for checking an element is visible and enabled such that you can click it. Parameters: element – the WebElement Returns: the (same) WebElement once it is clickable (visible and enabled)
public static ExpectedCondition<java.lang.Boolean> stalenessOf(WebElement element) Wait until an element is no longer attached to the DOM. Parameters: element – The element to wait for. Returns: false if the element is still attached to the DOM, true otherwise.
public static ExpectedCondition<java.lang.Boolean> elementToBeSelected(WebElement element) An expectation for checking if the given element is selected. Parameters: element – WebElement to be selected Returns: true once the element is selected
public static ExpectedCondition<Alert> alertIsPresent()
public static ExpectedCondition<java.lang.Boolean> numberOfWindowsToBe(int expectedNumberOfWindows)
Using WebDriverWait
WebDriverWait wait = new WebDriverWait(driver, 60);
import org.openqa.selenium.support.ui.WebDriverWait;
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login")));
WebElement login = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login")));
login.click();
wait.until(ExpectedConditions.alertIsPresent());