Skip to content

Latest commit

 

History

History
31 lines (26 loc) · 1.42 KB

MATCHERS.md

File metadata and controls

31 lines (26 loc) · 1.42 KB

Using Hamcrest matchers

Using hamcrest matchers is a preferred way to writing assertions. See Matchers class for an inspiration of how those can look like. Note the convenience (typesafe) Matcher superclass we use to avoid unnecessary verbosity:

public static Matcher<WebDriver> hasContent(final Pattern pattern) {
  return new Matcher<WebDriver>("Text matching %s", pattern) {
      @Override
      protected boolean matchesSafely(WebDriver item) {
          return pattern.matcher(pageText(item)).find();
      }

      @Override
      protected void describeMismatchSafely(WebDriver item, Description mismatchDescription) {
          mismatchDescription.appendText("was ")
                  .appendValue(item.getCurrentUrl())
                  .appendText("\n")
                  .appendValue(pageText(item));
      }

      private String pageText(WebDriver item) {
          return item.findElement(by.xpath("/html")).getText();
      }
  };
}

General purpose matchers should be available as static methods of the Matchers class. Plugin specific matchers should be defined in JUnit class or in dedicated *Matcher class.