CustomMatchers { public static IsPresentNTimes isPresent(int nTimes) { return new IsPresentNTimes(nTimes); }  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(expected = IllegalArgumentException.class) public void isPresentNTimesInvalidInput() { assertThat(span, CustomMatchers.isPresent(0).timesIn(browser)); }
@Test public void isPresentNTimesOrMoreFailed() { when(browser.findPageWithNumberOfOccurrences(any(), eq(5), eq(orMore))).thenThrow(new NoSuchElementException("")); when(browser.numberOfAppearances(any())).thenReturn(1); try { assertThat(span.inside(div), CustomMatchers.isPresent(5).timesOrMoreIn(browser)); fail("should fail"); } catch (AssertionError e) { assertThat(e.getMessage(), is(equalTo("\nExpected: browser page contains (span, inside div) at least 5 times\n but: (span, inside div) appears 1 time"))); } }
@Test public void isPresentNTimesOrMoreSuccess() { when(browser.findPageWithNumberOfOccurrences(any(), eq(5), eq(orMore))).thenReturn(mock(WebElement.class)); assertThat(span.inside(div), CustomMatchers.isPresent(5).timesOrMoreIn(browser)); }
@Test public void isPresentNTimesOrLessFailed() { when(browser.findPageWithNumberOfOccurrences(any(), eq(5), eq(orLess))).thenThrow(new NoSuchElementException("")); when(browser.numberOfAppearances(any())).thenReturn(1); try { assertThat(span.inside(div), CustomMatchers.isPresent(5).timesOrLessIn(browser)); fail("should fail"); } catch (AssertionError e) { assertThat(e.getMessage(), is(equalTo("\nExpected: browser page contains (span, inside div) at most 5 times\n but: (span, inside div) appears 1 time"))); } }
@Test public void isPresentNTimesOrLessSuccess() { when(browser.findPageWithNumberOfOccurrences(any(), eq(5), eq(orLess))).thenReturn(mock(WebElement.class)); assertThat(span.inside(div), CustomMatchers.isPresent(5).timesOrLessIn(browser)); }
@Test public void isPresentVariationFailed() { when(browser.isPresent(any())).thenReturn(false); try { assertThat(div, CustomMatchers.isPresent().in(browser)); fail("should fail"); } catch (AssertionError e) { assertThat(e.getMessage(), is(equalTo("\nExpected: browser page contains div\n but: div is absent"))); } }
@Test public void isPresentNTimesVariationFailed() { when(browser.findPageWithNumberOfOccurrences(any(), eq(5), eq(exactly))).thenThrow(new NoSuchElementException("")); when(browser.numberOfAppearances(any())).thenReturn(1); try { assertThat(span.inside(div), CustomMatchers.isPresent(5).timesIn(browser)); fail("should fail"); } catch (AssertionError e) { assertThat(e.getMessage(), is(equalTo("\nExpected: browser page contains (span, inside div) 5 times\n but: (span, inside div) appears 1 time"))); } }
@Test public void isPresentNTimesVariationSuccess() { when(browser.findPageWithNumberOfOccurrences(any(), eq(5), eq(exactly))).thenReturn(mock(WebElement.class)); when(browser.numberOfAppearances(any())).thenReturn(5); assertThat(span.inside(div), CustomMatchers.isPresent(5).timesIn(browser)); }