CustomMatchers

public 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.

Methods

hasElement

public static Matcher<InBrowser> hasElement(Path el)

Successful if the browser has an element that corresponds to the given path. Example use:

assertThat( browser, hasElement(el));
Parameters:
  • el – the path to find
Returns:

a matcher for a browser that contains the element

hasElements

public static HasElements hasElements(Path path)

Successful if element is present in the browser or a W3C document. Useful especially when you have a reference count. This matcher is optimized. For example:

assertThat(browser, hasElements(path).present(5).times());
   assertThat(browser, hasElements(path).present(5).timesOrMore());
   assertThat(document, hasElements(path).present(5).timesOrLess());
Parameters:
  • path – The path of the elements to find
Returns:

a matcher for the number of times an element is present.

hasNoElement

public static Matcher<InBrowser> hasNoElement(Path el)

Successful if given browser has no elements that correspond to the given path. The implementation of this is optimized. For example:

assertThat( browser, hasNoElement(path));
Parameters:
  • el
    • the path that is expected not to exist in the browser
Returns:

a matcher that is successful if the element does not appear in the browser

hasText

public static HasText hasText(String text)

Successful if element has the text equal to the given parameter in the browser/document. Example use:

assertThat( path, hasText().in(browser));
Parameters:
  • text – the text to equal to (case insensitive)
Returns:

a custom Hamcrest matcher

isAbsentFrom

public static Matcher<Path> isAbsentFrom(InBrowser browser)

Successful if given browser has no elements that correspond to the given path. Equivalent to hasNoElement() matcher. 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, isAbsentFrom(browser));
Parameters:
  • browser – the browser instance to look in
Returns:

a matcher that is successful if the element does not appear in the browser

isAbsentFrom

public static Matcher<Path> isAbsentFrom(Document document)

Successful if given document has no elements that correspond to the given path. For example:

assertThat( path, isAbsentFrom(doc));
Parameters:
  • document
    • a W3C document
Returns:

a matcher that is successful if the element does not appear in the document

isDisplayedIn

public static Matcher<Path> isDisplayedIn(InBrowser browser)

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

assertThat( path, isDisplayedIn(browser));
Parameters:
  • browser – the browser instance to look in
Returns:

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

isEnabledIn

public static Matcher<Path> isEnabledIn(InBrowser browser)

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

assertThat( path, isEnabledIn(browser));
Parameters:
  • browser – the browser instance to look in
Returns:

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

isNotDisplayedIn

public static Matcher<Path> isNotDisplayedIn(InBrowser browser)

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, isNotDisplayedIn(browser));

Parameters:
  • browser – the browser instance to look in
Returns:

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

isPresent

public static IsPresentNTimes isPresent(int nTimes)

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

InBrowser browser = new InBrowser(driver);
  assertThat( myElement, ispresent(5).timesOrMoreIn(browser));
  assertThat( myElement, ispresent(5).timesIn(browser));
  assertThat( myElement, ispresent(5).timesOrLessIn(browser));

Same examples apply in case you have a Document (org.w3c.dom.Document).

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 IsPresent isPresent()

Successful if element is present in the browser/document. Example use:

assertThat( path, isPresent().in(browser));
Returns:a custom Hamcrest matcher

isPresentIn

public static Matcher<Path> isPresentIn(InBrowser browser)

Successful if given element is present in the browser. For example:

assertThat( path, isPresentIn(browser));
Parameters:
  • browser – the browser instance to look in
Returns:

a matcher that checks if an element is present in a browser

isPresentIn

public static Matcher<Path> isPresentIn(Document document)

Successful if given element is present in the document. For example:

assertThat( path, isPresentIn(document));
Parameters:
  • document
    • a W#C document
Returns:

a matcher that checks if an element is present in a document

isSelectedIn

public static Matcher<Path> isSelectedIn(InBrowser browser)

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

assertThat( path, isSelectedIn(browser));
Parameters:
  • browser – the browser instance to look in
Returns:

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