One of the classes that you should know well from the Selenium library is ExpectedConditions.
This class is essential for implementing stable Selenium tests through synchronization between the tests and the site. There is usually a discrepancy between how fast the test runs and how fast the site is loaded. Synchronization brings both tests and the site at the same level.
Most testers use ExpectedConditions just for waiting until an element is visible or clickable as follows:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
wait.until(ExpectedConditions.elementToBeClickable(by));
And that’s about it.
But this means using just 2 expected conditions out of the 57 offered out of the box by Selenium.
What are the other 55 conditions about?
The expected conditions offered by the Selenium library can be grouped in several categories:
conditions about web elements
conditions about lists of web elements
conditions about frames
conditions about windows (tabs)
conditions about Java Script
conditions about a nested element
conditions about lists of nested elements
conditions about page title and url
miscellaneous conditions
A quick summary of each category follows.
No details will be added to each condition as they are pretty easy to understand.
Conditions about web elements
attributeContains(By locator, String attribute, String value)
attributeContains(WebElement element, String attribute, String value)
attributeToBe(By locator, String attribute, String value)
attributeToBe(WebElement element, String attribute, String value)
attributeToBeNotEmpty(WebElement element, String attribute)
domAttributeToBe(WebElement element, String attribute, String value)
domPropertyToBe(WebElement element, String property, String value)
elementSelectionStateToBe(By locator, boolean selected) elementSelectionStateToBe(WebElement element, boolean selected)
elementToBeClickable(By locator)
elementToBeClickable(WebElement element)
elementToBeSelected(By locator)
elementToBeSelected(WebElement element)
presenceOfElementLocated(By locator)
visibilityOf(WebElement element)
visibilityOfElementLocated(By locator)
invisibilityOfElementWithText(By locator, String text)
invisibilityOf(WebElement element)
invisibilityOfElementLocated(By locator)
stalenessOf(WebElement element)
textMatches(By locator, Pattern pattern)
textToBe(By locator, String value)
textToBePresentInElement(WebElement element, String text)
textToBePresentInElementLocated(By locator, String text) textToBePresentInElementValue(By locator, String text) textToBePresentInElementValue(WebElement element, String text)
There are conditions related to an attribute, the text , visibility or invisibility, state and staleness of elements.
Everything that you need for your tests.
Conditions about lists of web elements
invisibilityOfAllElements(List<WebElement> elements)
invisibilityOfAllElements(WebElement... elements)
numberOfElementsToBe(By locator, Integer number) numberOfElementsToBeLessThan(By locator, Integer number) numberOfElementsToBeMoreThan(By locator, Integer number) presenceOfAllElementsLocatedBy(By locator)
visibilityOfAllElements(List<WebElement> elements)
visibilityOfAllElements(WebElement... elements) visibilityOfAllElementsLocatedBy(By locator)
Here we have conditions about the size of a list of web elements (is it equal to a value, is it above a value, is it below a value), the presence, visibility and invisibility of the elements of the list.
Conditions about frames
frameToBeAvailableAndSwitchToIt(int frameLocator) frameToBeAvailableAndSwitchToIt(String frameLocator) frameToBeAvailableAndSwitchToIt(By locator)
frameToBeAvailableAndSwitchToIt(WebElement frameLocator)
These conditions wait until a frame is available (yes, it may not be available all the time, a frame is a web element like any other type of element) and then switch to it. The frame is identified in different ways, by id, name, web element or locator.
Conditions about windows
numberOfWindowsToBe(int expectedNumberOfWindows)
Just one condition here useful for waiting until a specific number of browser windows is found.
Conditions about page url and page title
titleContains(String title)
titleIs(String title)
urlContains(String fraction)
urlMatches(String regex)
urlToBe(String url)
Few conditions useful for checking if the url/title are either equal to a value or contain one.
Conditions about a nested element
presenceOfNestedElementLocatedBy(By locator, By childLocator)
presenceOfNestedElementLocatedBy(WebElement element, By childLocator)
These 2 are pretty self-explanatory.
Conditions about lists of nested elements
visibilityOfNestedElementsLocatedBy(By parent, By childLocator)
visibilityOfNestedElementsLocatedBy(WebElement element, By childLocator)
presenceOfNestedElementsLocatedBy(By parent, By childLocator)
Conditions about Java Script
javaScriptThrowsNoExceptions(String javaScript)
jsReturnsValue(String javaScript)
These 2 conditions allow waiting until either some java script code returns a value or until java script code runs without throwing an exception.
Miscellaneous conditions
alertIsPresent()
refreshed(ExpectedCondition<T> condition)
In summary, it pays off studying all classes of the Selenium library well.
In particular, the ExpectedConditions one.