ImplicitlyWaitingWebDriver extends DecoratedWebDriver { @Override public List<WebElement> findElements(By locator) { try { return wrapElements(trier().tryTo(getOriginal()::findElements, locator)); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (LimitExceededException e) { return new ArrayList<>(); } } ImplicitlyWaitingWebDriver(final WebDriver driver);  ImplicitlyWaitingWebDriver(final WebDriver driver, long timeoutInSeconds);  ImplicitlyWaitingWebDriver(final WebDriver driver, long timeoutInSeconds, long sleepTimeOut); protected  ImplicitlyWaitingWebDriver(final WebDriver driver, Clock clock, Sleeper sleeper, long timeoutInSeconds, long sleepTimeOut); @Override WebElement findElement(By locator); @Override List<WebElement> findElements(By locator);  }
@Test void findElementsChainShouldImplicitlyWaitForAnElementToBePresent() { final WebElement mockedElement1 = mock(WebElement.class); final WebElement mockedElement2 = mock(WebElement.class); final WebElement mockedElement3 = mock(WebElement.class); when(mockedDriver.findElements(By.name("foo"))) .thenReturn(new ArrayList<>()) .thenReturn(new ArrayList<>()) .thenReturn(Lists.newArrayList(mockedElement1)); when(mockedElement1.findElements(By.name("bar"))) .thenReturn(new ArrayList<>()) .thenReturn(new ArrayList<>()) .thenReturn(Lists.newArrayList(mockedElement2)); when(mockedElement2.findElements(By.name("baz"))) .thenReturn(new ArrayList<>()) .thenReturn(new ArrayList<>()) .thenReturn(Lists.newArrayList(mockedElement3)); List<WebElement> elements1 = driver.findElements(By.name("foo")); List<WebElement> elements2 = elements1.get(0).findElements(By.name("bar")); List<WebElement> elements3 = elements2.get(0).findElements(By.name("baz")); assertThat(clock.now(), is(600L)); verify(mockedDriver, times(3)).findElements(By.name("foo")); verify(mockedElement1, times(3)).findElements(By.name("bar")); verify(mockedElement2, times(3)).findElements(By.name("baz")); assertThat(elements3.get(0), equalTo(mockedElement3)); }
@Test void findElementsChainShouldThrowIfElementIsNotFound() { final WebElement mockedElement1 = mock(WebElement.class); final WebElement mockedElement2 = mock(WebElement.class); when(mockedDriver.findElements(By.name("foo"))) .thenReturn(new ArrayList<>()) .thenReturn(new ArrayList<>()) .thenReturn(Lists.newArrayList(mockedElement1)); when(mockedElement1.findElements(By.name("bar"))) .thenReturn(new ArrayList<>()) .thenReturn(new ArrayList<>()) .thenReturn(Lists.newArrayList(mockedElement2)); when(mockedElement2.findElements(By.name("baz"))) .thenReturn(new ArrayList<>()); List<WebElement> elements1 = driver.findElements(By.name("foo")); List<WebElement> elements2 = elements1.get(0).findElements(By.name("bar")); List<WebElement> elements3 = elements2.get(0).findElements(By.name("baz")); assertThat(clock.now(), is(1400L)); verify(mockedDriver, times(3)).findElements(By.name("foo")); verify(mockedElement1, times(3)).findElements(By.name("bar")); verify(mockedElement2, times(11)).findElements(By.name("baz")); assertThat(elements3.size(), is(0)); }
@Test void findElementsShouldImplicitlyWaitForAtLeastOneElementToBePresent() { final WebElement mockedElement1 = mock(WebElement.class); final WebElement mockedElement2 = mock(WebElement.class); final List<WebElement> mockedElements = Lists.newArrayList(mockedElement1, mockedElement2); when(mockedDriver.findElements(By.name("foo"))) .thenReturn(new ArrayList<>()) .thenReturn(new ArrayList<>()) .thenReturn(mockedElements); List<WebElement> elements = driver.findElements(By.name("foo")); assertThat(clock.now(), is(200L)); verify(mockedDriver, times(3)).findElements(By.name("foo")); assertThat(elements, equalTo(mockedElements)); }
@Test void findElementsShouldReturnEmptyListIfNoElementIsFound() { when(mockedDriver.findElements(By.name("foo"))) .thenReturn(new ArrayList<>()); List<WebElement> elements = driver.findElements(By.name("foo")); assertThat(clock.now(), is(1000L)); verify(mockedDriver, times(11)).findElements(By.name("foo")); assertThat(elements.size(), is(0)); }