CustomMatchers { public static HasElements hasElements(Path path) { return new HasElements(path); }  static Matcher<InBrowser> hasElement(final Path el); static IsPresentNTimes isPresent(int nTimes); static IsPresent isPresent(); static HasText hasText(String text); static HasElements hasElements(Path path); static Matcher<Path> isPresentIn(final InBrowser browser); static Matcher<Path> isPresentIn(final Document document); static Matcher<Path> isDisplayedIn(final InBrowser browser); static Matcher<Path> isNotDisplayedIn(final InBrowser browser); static Matcher<Path> isSelectedIn(final InBrowser browser); static Matcher<Path> isEnabledIn(final InBrowser browser); static Matcher<InBrowser> hasNoElement(final Path el); static Matcher<Path> isAbsentFrom(final InBrowser browser); static Matcher<Path> isAbsentFrom(final Document document);  }
@Test public void hasElementNTimesFailed() { when(browser.numberOfAppearances(div)).thenReturn(2); when(browser.findPageWithNumberOfOccurrences(eq(div), eq(5), eq(exactly))).thenThrow(new NoSuchElementException("")); try { assertThat(browser, CustomMatchers.hasElements(div).present(5).times()); fail("should fail"); } catch (AssertionError e) { assertThat(e.getMessage(), is(equalTo("\nExpected: browser page contains div 5 times\n but: div appears 2 times"))); } }
@Test public void hasElementNTimesSuccess() { when(browser.findPageWithNumberOfOccurrences(eq(div), eq(2), eq(exactly))).thenReturn(mock(WebElement.class)); assertThat(browser, CustomMatchers.hasElements(div).present(2).times()); }
@Test public void hasElementNTimesOrMoreFailed() { when(browser.numberOfAppearances(div)).thenReturn(2); when(browser.findPageWithNumberOfOccurrences(eq(div), eq(5), eq(orMore))).thenThrow(new NoSuchElementException("")); try { assertThat(browser, CustomMatchers.hasElements(div).present(5).timesOrMore()); fail("should fail"); } catch (AssertionError e) { assertThat(e.getMessage(), is(equalTo("\nExpected: browser page contains at least div 5 times\n but: div appears 2 times"))); } }
@Test public void hasElementNTimesOrMoreSuccess() { when(browser.findPageWithNumberOfOccurrences(eq(div), eq(2), eq(orMore))).thenReturn(mock(WebElement.class)); assertThat(browser, CustomMatchers.hasElements(div).present(2).timesOrMore()); }
@Test public void hasElementNTimesOrLessFailed() { when(browser.numberOfAppearances(div)).thenReturn(2); when(browser.findPageWithNumberOfOccurrences(eq(div), eq(5), eq(orLess))).thenThrow(new NoSuchElementException("")); try { assertThat(browser, CustomMatchers.hasElements(div).present(5).timesOrLess()); fail("should fail"); } catch (AssertionError e) { assertThat(e.getMessage(), is(equalTo("\nExpected: browser page contains at most div 5 times\n but: div appears 2 times"))); } }
@Test public void hasElementNTimesOrLessSuccess() { when(browser.findPageWithNumberOfOccurrences(eq(div), eq(2), eq(orLess))).thenReturn(mock(WebElement.class)); assertThat(browser, CustomMatchers.hasElements(div).present(2).timesOrLess()); }