CustomMatchers

public final class CustomMatchers

A collection of Hamcrest custom matchers, that are optimized to be as atomic as possible when interacting with the browser or a W3C document, and return useful error messages in case of a failure. This is a simplified API, relevant when there is a singleton browser.

Methods

hasText

public static Matcher<Path> hasText(String text)

Successful if element has the text equal to the given parameter in the browser/document. Note that internally it creates a new path that includes the “hasText” constraint, and then searches for it, so it is atomic. Example use:

assertThat( path, hasText("John"));
Parameters:
  • text – the text to equal to (case insensitive)
Returns:

a custom Hamcrest matcher

isAbsent

public static Matcher<Path> isAbsent()

Successful if the browser has no elements that correspond to the given path. The implementation of this is optimized. This is much better than doing not(isPresent()), because in case of success (i.e. the element is not there), it will return immidiately, while the isPresent() will block until timeout is reached. For example: assertThat( path, isAbsent());

Returns:a matcher that is successful if an element does not appear in the browser.

isDisplayed

public static Matcher<Path> isDisplayed()

Successful if given element is present and displayed in the browser. Relies on WebElement.isDisplayed(), thus non-atomic. For example: assertThat( path, isDisplayed());

Returns:a matcher that checks if an element is displayed in the browser

isEnabled

public static Matcher<Path> isEnabled()

Successful if given element is present and enabled in the browser. Relies on WebElement.isEnabled(), thus non-atomic. For example: assertThat( path, isEnabled());

Returns:a matcher that checks if an element is enabled in the browser

isNotDisplayed

public static Matcher<Path> isNotDisplayed()

Successful if given element is either not present, or present and not displayed in the browser. Relies on WebElement.isDisplayed(), thus non-atomic. For example: assertThat( path, isNotDisplayed());

Returns:a matcher that checks if an element is displayed in the browser

isNotSelected

public static Matcher<Path> isNotSelected()

Successful if given element is present and is not selected in the browser. Relies on WebElement.isSelected(), thus non-atomic. For example: assertThat( path, isSelected());

Returns:a matcher that checks if an element is selected in the browser

isPresent

public static IsPresentNTimes isPresent(int nTimes)

Successful if the the element appears the expected number of times in the browser. This matcher is optimized. Example use for browser interaction:

assertThat( path, ispresent(5).timesOrMore());
assertThat( path, ispresent(5).times());
assertThat( path, ispresent(5).timesOrLess());
Parameters:
  • nTimes
    • the reference number of times to be matched against. See examples.
Returns:

a matcher that matches the number of times an element is present. See examples in the description.

isPresent

public static Matcher<Path> isPresent()

Successful if the the element is present in the browser. Example: assertThat( path, ispresent());

Returns:a matcher that checks if an element is present in the browser

isSelected

public static Matcher<Path> isSelected()

Successful if given element is present and selected in the browser. Relies on WebElement.isSelected(), thus non-atomic. For example: assertThat( path, isSelected());

Returns:a matcher that checks if an element is selected in the browser