04
Sep

How to automate with “ExpectedConditions” in Selenium WebDriver

Return type for “ExpectedConditions.xxxx()” is generally falls under following categories:

Alert
Boolean
WebElement
List
WebDriver
Object

Following code snippet demonstrates usage and validation based on return type. (ExpectedCondition testing with Selenium). We can add necessary asserts based on test automation requirements

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ExpectedConditionsExamples {

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
String URL = “http://www.vpl.ca/”;
driver.get(URL);

Boolean isTitleCorrect = wait.until(ExpectedConditions.titleContains(“Public”));
System.out.println(“titleContains results” + isTitleCorrect);

Boolean titleIs = wait.until(ExpectedConditions.titleIs(“Vancouver Public Library |”));
System.out.println(“Wait completed for title to be \”Vancouver Public Library |\”” + titleIs);

Boolean urlContains = wait.until(ExpectedConditions.urlContains(“vpl”));
System.out.println(“urlContains results” + urlContains);

Boolean urlToBe = wait.until(ExpectedConditions.urlToBe(“http://www.vpl.ca/”));
System.out.println(“urlToBe results” + urlToBe);

String regex = “\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]”;
Boolean urlMatches = wait.until(ExpectedConditions.urlMatches(regex));
System.out.println(“urlMatches results” + urlToBe);

// ExpectedConditions.elementToBeSelected
WebElement searchDDwn = driver.findElement(By.xpath(“.//*[@id=’edit-source’]”));
Select dropdown = new Select(searchDDwn);
dropdown.selectByValue(“Website”);
WebElement selected = dropdown.getFirstSelectedOption();
boolean elementToBeSelected = wait.until(ExpectedConditions.elementToBeSelected(selected));
System.out.println(“elementToBeSelected results” + elementToBeSelected);

// ExpectedConditions.invisibilityOfElementLocated
By searchDDwn1 = By.xpath(“.//*[@id=’edit-source1′]”);
boolean invisibilityOfElementLocated = wait.until(ExpectedConditions.invisibilityOfElementLocated(searchDDwn1));
System.out.println(“invisibilityOfElementLocated results” + invisibilityOfElementLocated);

// ExpectedConditions.invisibilityOfElementWithText
By searchDDwn2 = By.xpath(“.//*[text()=’edit-source1′]”);
boolean invisibilityOfElementWithText = wait
.until(ExpectedConditions.invisibilityOfElementWithText(searchDDwn2, “hello”));
System.out.println(“invisibilityOfElementWithText results” + invisibilityOfElementWithText);

// ExpectedConditions.stalenessOf
driver.navigate().refresh();
By searchDDwn3 = By.xpath(“.//*[text()=’edit-source1′]”);
boolean stalenessOf = wait.until(ExpectedConditions.invisibilityOfElementWithText(searchDDwn2, “hello”));
System.out.println(“stalenessOf results” + stalenessOf);

// ExpectedConditions.textToBePresentInElementLocated
By searchBoxId = By.id(“edit-search”);
WebElement searchBox = wait.until(ExpectedConditions.elementToBeClickable(searchBoxId));
searchBox.sendKeys(“java”);

boolean res = wait.until(ExpectedConditions.textToBePresentInElementLocated(searchBoxId, “java”));
System.out.println(“textToBePresentInElement results” + stalenessOf);

By searchButtonId = By.id(“edit-submit”);
WebElement searchButton = wait.until(ExpectedConditions.visibilityOfElementLocated(searchButtonId));
searchButton.click();

Thread.sleep(2000);

// 1. visibilityOfAllElementsLocatedBy

By titleLocator = By.xpath(“//button[@data-test-id=’list-view-button’]”);
List titles = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(titleLocator));
int titleCount = titles.size();
System.out.println(“Number of titles” + titleCount);

By authorLocator = By.xpath(“//button[@data-test-id=’list-view-button’]”);
List author = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(authorLocator));
int authorCount = author.size();
System.out.println(“Number of authors” + authorCount);

driver.quit();
}
}

Summary of ExpectedConditions checks supported by Selenium:

S.No. return Type => Function => Description
1 static ExpectedCondition => alertIsPresent() => webalert is present
2 static ExpectedCondition => and(ExpectedCondition… conditions) => An expectation with the logical and condition of the given list of conditions.
3 static ExpectedCondition => attributeContains(org.openqa.selenium.By locator, String attribute, String value) => An expectation for checking WebElement with given locator has attribute which contains specific value
4 static ExpectedCondition => attributeContains(org.openqa.selenium.WebElement element, String attribute, String value) => An expectation for checking WebElement with given locator has attribute which contains specific value
5 static ExpectedCondition => attributeToBe(org.openqa.selenium.By locator, String attribute, String value) => An expectation for checking WebElement with given locator has attribute with a specific value
6 static ExpectedCondition => attributeToBe(org.openqa.selenium.WebElement element, String attribute, String value) => An expectation for checking given WebElement has attribute with a specific value
7 static ExpectedCondition => attributeToBeNotEmpty(org.openqa.selenium.WebElement element, String attribute) => An expectation for checking WebElement any non empty value for given attribute
8 static ExpectedCondition => elementSelectionStateToBe(org.openqa.selenium.By locator, boolean selected) => An expectation for checking if the given element is selected.
9 static ExpectedCondition => elementSelectionStateToBe(org.openqa.selenium.WebElement element, boolean selected) => An expectation for checking if the given element is selected.
10 static ExpectedCondition => elementToBeClickable(org.openqa.selenium.By locator) => An expectation for checking an element is visible and enabled such that you can click it.
11 static ExpectedCondition => elementToBeClickable(org.openqa.selenium.WebElement element) => An expectation for checking an element is visible and enabled such that you can click it.
12 static ExpectedCondition => elementToBeSelected(org.openqa.selenium.By locator) => An expectation for checking if the given element is selected.
13 static ExpectedCondition => elementToBeSelected(org.openqa.selenium.WebElement element) => An expectation for checking if the given element is selected.
14 static ExpectedCondition => frameToBeAvailableAndSwitchToIt(org.openqa.selenium.By locator) => An expectation for checking whether the given frame is available to switch to.
15 static ExpectedCondition => frameToBeAvailableAndSwitchToIt(int frameLocator) => An expectation for checking whether the given frame is available to switch to.
16 static ExpectedCondition => frameToBeAvailableAndSwitchToIt(String frameLocator) => An expectation for checking whether the given frame is available to switch to.
17 static ExpectedCondition => frameToBeAvailableAndSwitchToIt(org.openqa.selenium.WebElement frameLocator) => An expectation for checking whether the given frame is available to switch to.
18 static ExpectedCondition => invisibilityOfAllElements(List elements) => An expectation for checking all elements from given list to be invisible
19 static ExpectedCondition => invisibilityOfElementLocated(org.openqa.selenium.By locator) => An expectation for checking that an element is either invisible or not present on the DOM.
20 static ExpectedCondition => invisibilityOfElementWithText(org.openqa.selenium.By locator, String text) => An expectation for checking that an element with text is either invisible or not present on the DOM.
21 static ExpectedCondition => javaScriptThrowsNoExceptions(String javaScript) => An expectation to check if js executable Usefull when you know, that there should be js val or something at the stage
22 static ExpectedCondition => jsReturnsValue(String javaScript) => An expectation for String value from javascript
23 static ExpectedCondition => not(ExpectedCondition condition) => An expectation with the logical opposite condition of the given condition.
24 static ExpectedCondition> => numberOfElementsToBe(org.openqa.selenium.By locator, Integer number) => An expectation for checking number of WebElements with given locator
25 static ExpectedCondition> => numberOfElementsToBeLessThan(org.openqa.selenium.By locator, Integer number) => An expectation for checking number of WebElements with given locator being less than defined number
26 static ExpectedCondition> => numberOfElementsToBeMoreThan(org.openqa.selenium.By locator, Integer number) => An expectation for checking number of WebElements with given locator
27 static ExpectedCondition => numberOfWindowsToBe(int expectedNumberOfWindows) => Number of browser windows
28 static ExpectedCondition => or(ExpectedCondition… conditions) => An expectation with the logical or condition of the given list of conditions.
29 static ExpectedCondition> => presenceOfAllElementsLocatedBy(org.openqa.selenium.By locator) => An expectation for checking that there is at least one element present on a web page.
30 static ExpectedCondition => presenceOfElementLocated(org.openqa.selenium.By locator) => An expectation for checking that an element is present on the DOM of a page.
31 static ExpectedCondition => presenceOfNestedElementLocatedBy(org.openqa.selenium.By locator, org.openqa.selenium.By sub_locator) => An expectation for checking child WebElement as a part of parent element to present
32 static ExpectedCondition => presenceOfNestedElementLocatedBy(org.openqa.selenium.WebElement element, org.openqa.selenium.By sub_locator) => An expectation for checking child WebElement as a part of parent element to be present
33 static ExpectedCondition> => presenceOfNestedElementsLocatedBy(org.openqa.selenium.By locator, org.openqa.selenium.By sub_locator) => An expectation for checking child WebElement as a part of parent element to present
34 static ExpectedCondition => refreshed(ExpectedCondition condition) => Wrapper for a condition, which allows for elements to update by redrawing.
35 static ExpectedCondition => stalenessOf(org.openqa.selenium.WebElement element) => Wait until an element is no longer attached to the DOM.
36 static ExpectedCondition => textMatches(org.openqa.selenium.By locator, Pattern pattern) => An expectation for checking WebElement with given locator has text with a value as a part of it
37 static ExpectedCondition => textToBe(org.openqa.selenium.By locator, String value) => An expectation for checking WebElement with given locator has specific text
38 static ExpectedCondition => textToBePresentInElementLocated(By, String) => An expectation for checking if the given text is present in the specified element identified with By
39 static ExpectedCondition => textToBePresentInElement(org.openqa.selenium.WebElement element, String text) => An expectation for checking if the given text is present in the specified element.
40 static ExpectedCondition => textToBePresentInElementLocated(org.openqa.selenium.By locator, String text) => An expectation for checking if the given text is present in the element that matches the given locator.
41 static ExpectedCondition => textToBePresentInElementValue(org.openqa.selenium.By locator, String text) => An expectation for checking if the given text is present in the specified elements value attribute.
42 static ExpectedCondition => textToBePresentInElementValue(org.openqa.selenium.WebElement element, String text) => An expectation for checking if the given text is present in the specified elements value attribute.
43 static ExpectedCondition => titleContains(String title) => An expectation for checking that the title contains a case-sensitive substring
44 static ExpectedCondition => titleIs(String title) => An expectation for checking the title of a page.
45 static ExpectedCondition => urlContains(String fraction) => An expectation for the URL of the current page to contain specific text.
46 static ExpectedCondition => urlMatches(String regex) => Expectation for the URL to match a specific regular expression
47 static ExpectedCondition => urlToBe(String url) => An expectation for the URL of the current page to be a specific url.
48 static ExpectedCondition => visibilityOf(org.openqa.selenium.WebElement element) => An expectation for checking that an element, known to be present on the DOM of a page, is visible.
49 static ExpectedCondition> => visibilityOfAllElements(List elements) => An expectation for checking that all elements present on the web page that match the locator are visible.
50 static ExpectedCondition> => visibilityOfAllElementsLocatedBy(org.openqa.selenium.By locator) => An expectation for checking that all elements present on the web page that match the locator are visible.
51 static ExpectedCondition => visibilityOfElementLocated(org.openqa.selenium.By locator) => An expectation for checking that an element is present on the DOM of a page and visible.
52 static ExpectedCondition> => visibilityOfNestedElementsLocatedBy(org.openqa.selenium.By locator, org.openqa.selenium.By sub_locator) => An expectation for checking child WebElement as a part of parent element to be visible
53 static ExpectedCondition> => visibilityOfNestedElementsLocatedBy(org.openqa.selenium.WebElement element, org.openqa.selenium.By sub_locator) => An expectation for checking child WebElement as a part of parent element to be visible

ExpectedConditions.alertIsPresent()
ExpectedConditions.and(ExpectedCondition… conditions)
ExpectedConditions.attributeContains(org.openqa.selenium.By locator, String attribute, String value)
ExpectedConditions.attributeContains(org.openqa.selenium.WebElement element, String attribute, String value)
ExpectedConditions.attributeToBe(org.openqa.selenium.By locator, String attribute, String value)
ExpectedConditions.attributeToBe(org.openqa.selenium.WebElement element, String attribute, String value)
ExpectedConditions.attributeToBeNotEmpty(org.openqa.selenium.WebElement element, String attribute)
ExpectedConditions.elementSelectionStateToBe(org.openqa.selenium.By locator, boolean selected)
ExpectedConditions.elementSelectionStateToBe(org.openqa.selenium.WebElement element, boolean selected)
ExpectedConditions.elementToBeClickable(org.openqa.selenium.By locator)
ExpectedConditions.elementToBeClickable(org.openqa.selenium.WebElement element)
ExpectedConditions.elementToBeSelected(org.openqa.selenium.By locator)
ExpectedConditions.elementToBeSelected(org.openqa.selenium.WebElement element)
ExpectedConditions.frameToBeAvailableAndSwitchToIt(org.openqa.selenium.By locator)
ExpectedConditions.frameToBeAvailableAndSwitchToIt(int frameLocator)
ExpectedConditions.frameToBeAvailableAndSwitchToIt(String frameLocator)
ExpectedConditions.frameToBeAvailableAndSwitchToIt(org.openqa.selenium.WebElement frameLocator)
ExpectedConditions.invisibilityOfAllElements(List elements)
ExpectedConditions.invisibilityOfElementLocated(org.openqa.selenium.By locator)
ExpectedConditions.invisibilityOfElementWithText(org.openqa.selenium.By locator, String text)
ExpectedConditions.javaScriptThrowsNoExceptions(String javaScript)
ExpectedConditions.jsReturnsValue(String javaScript)
ExpectedConditions.not(ExpectedCondition condition)
ExpectedConditions.numberOfElementsToBe(org.openqa.selenium.By locator, Integer number)
ExpectedConditions.numberOfElementsToBeLessThan(org.openqa.selenium.By locator, Integer number)
ExpectedConditions.numberOfElementsToBeMoreThan(org.openqa.selenium.By locator, Integer number)
ExpectedConditions.numberOfWindowsToBe(int expectedNumberOfWindows)
ExpectedConditions.or(ExpectedCondition… conditions)
ExpectedConditions.presenceOfAllElementsLocatedBy(org.openqa.selenium.By locator)
ExpectedConditions.presenceOfElementLocated(org.openqa.selenium.By locator)
ExpectedConditions.presenceOfNestedElementLocatedBy(org.openqa.selenium.By locator, org.openqa.selenium.By sub_locator)
ExpectedConditions.presenceOfNestedElementLocatedBy(org.openqa.selenium.WebElement element, org.openqa.selenium.By sub_locator)
ExpectedConditions.presenceOfNestedElementsLocatedBy(org.openqa.selenium.By locator, org.openqa.selenium.By sub_locator)
ExpectedConditions.refreshed(ExpectedCondition condition)
ExpectedConditions.stalenessOf(org.openqa.selenium.WebElement element)
ExpectedConditions.textMatches(org.openqa.selenium.By locator, Pattern pattern)
ExpectedConditions.textToBe(org.openqa.selenium.By locator, String value)
ExpectedConditions.textToBePresentInElementLocated(By, String)
ExpectedConditions.textToBePresentInElement(org.openqa.selenium.WebElement element, String text)
ExpectedConditions.textToBePresentInElementLocated(org.openqa.selenium.By locator, String text)
ExpectedConditions.textToBePresentInElementValue(org.openqa.selenium.By locator, String text)
ExpectedConditions.textToBePresentInElementValue(org.openqa.selenium.WebElement element, String text)
ExpectedConditions.titleContains(String title)
ExpectedConditions.titleIs(String title)
ExpectedConditions.urlContains(String fraction)
ExpectedConditions.urlMatches(String regex)
ExpectedConditions.urlToBe(String url)
ExpectedConditions.visibilityOf(org.openqa.selenium.WebElement element)
ExpectedConditions.visibilityOfAllElements(List elements)
ExpectedConditions.visibilityOfAllElementsLocatedBy(org.openqa.selenium.By locator)
ExpectedConditions.visibilityOfElementLocated(org.openqa.selenium.By locator)
ExpectedConditions.visibilityOfNestedElementsLocatedBy(org.openqa.selenium.By locator, org.openqa.selenium.By sub_locator)
ExpectedConditions.visibilityOfNestedElementsLocatedBy(org.openqa.selenium.WebElement element, org.openqa.selenium.By sub_locator)

0 comments

Leave a reply

© Copyright 2017 - Archi Develop by OceanThemes