thewaiter: wait for WebElement text. To equal, contain a String with variations.

Element text is something you will often check for when writing Selenium tests. Whether it equals a given String, or contains a given String. But you can extend your checks to whether: the element text equals/contains a String ignoring the case of the two, or whether the element text equals/contains a String ignoring any whitespace the two might contain.

When would you ignore the case: usually when you need to check for a very long text, and you are not interested in whether some words are written in camel case or upper case. You are more interested that the text displayed to the user conveys the message it’s supposed to, not how the upper and lower case letters appear in the text.

When would you ignore the whitespaces: well, when you need to check for a very long text that is styled on the front-end side, to be easy to read. Usually when the text is grouped into paragraphs or intense spacing is used (many spaces between groups of words). Again, in this situation you are more interested in what the text is, and not how it is structured on the page.

Below you can find the wait methods that are currently implemented in ‘thewaiter’ library. Note that when using them, you don’t need to use the ‘waitForElementToBeDisplayed’ method also. These methods suffice. Also, the WebElements need to be created in a PageObject class and passed to the wait method as such (not by using driver.findElement).

The wait methods: waitForElementTextEqualsString

This is the method that waits for the text on a WebElement to equal an expected String. This variant is the one to which you need to pass the timeout value, as an int, which represents the number of seconds the method will wait for. The other parameters the method requires are: the WebElement whose text you are checking, and the String representing the expected text on the WebElement. Also, the WebDriver instance needs to be passed to the method.

public void waitForElementTextEqualsString(WebElement element, String expectedString, WebDriver driver, int
specifiedTimeout) {
WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout);
ExpectedCondition<Boolean> elementTextEqualsString = arg0 -> element.getText().equals(expectedString);
wait.until(elementTextEqualsString);
}

The variant of the same method that uses the default timeout value set in ‘thewaiter’ library (30 seconds), based on the above method, is:

public void waitForElementTextEqualsString(WebElement element, String expectedString, WebDriver driver) {
waitForElementTextEqualsString(element, expectedString, driver, TIMEOUT);
}
The wait methods: waitForElementTextEqualsString_IgnoreCase

The method which needs to specify the timeout, that waits for the text on a WebElement to equal a String, but ignoring the case of both, can be found below. This method checks for the actual letters of both strings, not taking into account that one is upper case and the other is lower case. Therefore, for example, the String ‘String’ and the String ‘string’ will be seen as identical by this method.

public void waitForElementTextEqualsString_IgnoreCase(WebElement element, String expectedString, WebDriver driver,
int
specifiedTimeout) {
WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout);
ExpectedCondition<Boolean> elementTextEqualsStringIgnoreCase = arg0 -> element.getText().equalsIgnoreCase
(expectedString);
wait.until(elementTextEqualsStringIgnoreCase);
}

The same method, with a default timeout, is below:

public void waitForElementTextEqualsString_IgnoreCase(WebElement element, String expectedString, WebDriver driver) {
waitForElementTextEqualsString_IgnoreCase(element, expectedString, driver, TIMEOUT);
}
The wait methods: waitForElementTextEqualsString_IgnoreWhitespaces

The method that waits for the text on a WebElement to equal another String, during a specified timeout period, but ignoring the whitespaces of both, can be found below. Whitespaces include spaces, tabs or new lines. From this method’s perspective, the String ‘St r   ing’ equals the String ‘String’.

public void waitForElementTextEqualsString_IgnoreWhitespaces(WebElement element, String expectedString, WebDriver
driver, int specifiedTimeout) {
WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout);
ExpectedCondition<Boolean> elementTextEqualsString = arg0 -> element.getText().replaceAll("\\s", "").equals
(expectedString.replaceAll("\\s", ""));
wait.until(elementTextEqualsString);
}

The variant of the method that uses a default timeout is:

public void waitForElementTextEqualsString_IgnoreWhitespaces(WebElement element, String expectedString, WebDriver
driver) {
waitForElementTextEqualsString_IgnoreWhitespaces(element, expectedString, driver, TIMEOUT);
}
The wait methods: waitForElementTextContainsString

Similarly to the methods above, here are the methods that wait for the text on a WebElement to contain an expected String, with specified timeout:

public void waitForElementTextContainsString(WebElement element, String expectedString, WebDriver driver, int
specifiedTimeout) {
WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout);
ExpectedCondition<Boolean> elementTextContainsString = arg0 -> element.getText().contains(expectedString);
wait.until(elementTextContainsString);
}

The default timeout variant:

public void waitForElementTextContainsString(WebElement element, String expectedString, WebDriver driver) {
waitForElementTextContainsString(element, expectedString, driver, TIMEOUT);
}
The wait methods: waitForElementTextContainsString_IgnoreCase

The specified timeout method for waiting for an element’s text to contain an expected String, ignoring the case of both texts:

public void waitForElementTextContainsString_IgnoreCase(WebElement element, String expectedString, WebDriver driver,
int specifiedTimeout) {
WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout);
ExpectedCondition<Boolean> elementTextContainsString = arg0 -> element.getText().toLowerCase().contains
(expectedString.toLowerCase());
wait.until(elementTextContainsString);
}

The default timeout variant:

public void waitForElementTextContainsString_IgnoreCase(WebElement element, String expectedString, WebDriver
driver) {
waitForElementTextContainsString_IgnoreCase(element, expectedString, driver, TIMEOUT);
}
The wait methods: waitForElementTextContainsString_IgnoreWhitespaces

The wait method with specified timeout that waits for an element’s text to contain an expected String, ignore any whitespace in the two Strings is:

public void waitForElementTextContainsString_IgnoreWhitespaces(WebElement element, String expectedString, WebDriver
driver, int specifiedTimeout) {
WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout);
ExpectedCondition<Boolean> elementTextContainsString = arg0 -> element.getText().replaceAll("\\s", "").contains
(expectedString.replaceAll("\\s", ""));
wait.until(elementTextContainsString);
}

The variant with default timeout:

public void waitForElementTextContainsString_IgnoreWhitespaces(WebElement element, String expectedString, WebDriver
driver) {
waitForElementTextContainsString_IgnoreWhitespaces(element, expectedString, driver, TIMEOUT);
}
Examples

In order to exemplify these methods, i created a few tests. They all open a page, on which you can find a text field. From it you can add a new item to the page. The first tests check that after you add a new item, the text of the only entry in the list where the added items appear, equals/contains the expected String. The following tests also add an item, then check that its’ text equals/contains a String, ignoring the case, then the whitespaces.

Additionally, right after you add an item, a success message is displayed. The wait for the text equality method is also used to check that the message text is ‘Success’, ignoring whitespaces. I extracted a separate method for adding the item to the list, since this process is done in each test. This method looks as follows:

private void addNewItemWithLabel(String itemLabel) {
page.addMeField.sendKeys(itemLabel);
page.addMeButton.click();
waiter.waitForElementTextEqualsString_IgnoreWhitespaces(page.successMessage, "Success!", driver);
}

Below are the tests. I will only show them in the ‘default timeout’ variant. You can find all of them, together with the tests for the ‘specified timeout’ variants in my Git examples project.

All of the below tests pass, because they are waiting for a condition that really takes place.

  • waiting for the element text to equal the expected String
addNewItemWithLabel("espresso");
waiter.waitForElementTextEqualsString(page.addedElement, "espresso", driver);
  • waiting for the element text to contain the expected String
addNewItemWithLabel("espresso with milk");
waiter.waitForElementTextContainsString(page.addedElement, "espresso", driver);
  • waiting for the element text to equal the expected String, ignoring cases
addNewItemWithLabel("EsPreSSo");
waiter.waitForElementTextEqualsString_IgnoreCase(page.addedElement, "espresso", driver);
  • waiting for the element text to contain the expected String, ignoring cases
addNewItemWithLabel("EsPreSSo With Milk");
waiter.waitForElementTextContainsString_IgnoreCase(page.addedElement, "espresso", driver);
  • waiting for the element text to equal the expected String, ignoring whitespaces
addNewItemWithLabel("es pre sso");
waiter.waitForElementTextEqualsString_IgnoreWhitespaces(page.addedElement, "espresso", driver);
  • waiting for the element text to contain the expected String, ignoring whitespaces
addNewItemWithLabel("es pre sso wi th mi lk");
waiter.waitForElementTextContainsString_IgnoreWhitespaces(page.addedElement, "espresso", driver);

Where to find the code and  examples

The GitHub repo of thewaiter library, with a “how to use” documentation:

https://github.com/iamalittletester/thewaiter

The downloadable release version of thewaiter:

https://mvnrepository.com/artifact/com.imalittletester/thewaiter

The library’s javadoc:

http://javadoc.io/doc/com.imalittletester/thewaiter/1.0

The tests that use the methods from this post:

https://github.com/iamalittletester/learning-project/blob/master/src/test/java/waitertests/WaitForElementTextTest.java

https://github.com/iamalittletester/learning-project/blob/master/src/main/java/pageobjects/waiter/WaiterPage.java

One thought on “thewaiter: wait for WebElement text. To equal, contain a String with variations.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.