ImplicitlyWaitingWebDriver extends DecoratedWebDriver { @Override public WebElement findElement(By locator) { try { return new ImplicitlyWaitingWebElement(trier().tryTo(getOriginal()::findElement, locator), this).getActivated(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (LimitExceededException e) { throw propagate(e.getCause()); } } 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 findElementShouldImplicitlyWaitForAnElementToBePresent() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenThrow(NoSuchElementException.class) .thenThrow(NoSuchElementException.class) .thenReturn(mockedElement); WebElement element = driver.findElement(By.name("foo")); assertThat(clock.now(), is(200L)); verify(mockedDriver, times(3)).findElement(By.name("foo")); assertThat(element, equalTo(mockedElement)); }
@Test void clearShouldImplicitlyWaitForTheElementToBeVisible() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); doThrow(ElementNotVisibleException.class) .doThrow(ElementNotVisibleException.class) .doNothing() .when(mockedElement).clear(); driver.findElement(By.name("foo")).clear(); assertThat(clock.now(), is(200L)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(3)).clear(); }
@Test void clearShouldThrowIfTheElementIsNotVisible() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); doThrow(new ElementNotVisibleException("")) .when(mockedElement).clear(); assertThrows(ElementNotVisibleException.class, () -> driver.findElement(By.name("foo")).clear()); assertThat(clock.now(), is(1000L)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(11)).clear(); }
@Test void isSelectedShouldImplicitlyWaitForTheElementToBeVisible() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); when(mockedElement.isSelected()) .thenThrow(ElementNotVisibleException.class) .thenThrow(ElementNotVisibleException.class) .thenReturn(true); boolean selected = driver.findElement(By.name("foo")).isSelected(); assertThat(clock.now(), is(200L)); assertThat(selected, is(true)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(3)).isSelected(); }
@Test void isSelectedShouldImplicitlyWaitForTheElementToBeVisibleEvenIfTheElementIsNotSelected() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); when(mockedElement.isSelected()) .thenThrow(new ElementNotVisibleException("")) .thenThrow(new ElementNotVisibleException("")) .thenReturn(false); boolean selected = driver.findElement(By.name("foo")).isSelected(); assertThat(clock.now(), is(200L)); assertThat(selected, is(false)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(3)).isSelected(); }
@Test void isSelectedShouldThrowIfTheElementIsNotVisible() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); when(mockedElement.isSelected()) .thenThrow(ElementNotVisibleException.class); assertThrows(ElementNotVisibleException.class, () -> driver.findElement(By.name("foo")).isSelected()); assertThat(clock.now(), is(1000L)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(11)).isSelected(); }
@Test void isEnabledShouldImplicitlyWaitForTheElementToBeVisible() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); when(mockedElement.isEnabled()) .thenThrow(ElementNotVisibleException.class) .thenThrow(ElementNotVisibleException.class) .thenReturn(true); boolean selected = driver.findElement(By.name("foo")).isEnabled(); assertThat(clock.now(), is(200L)); assertThat(selected, is(true)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(3)).isEnabled(); }
@Test void isEnabledShouldImplicitlyWaitForTheElementToBeVisibleEvenIfTheElementIsNotEnabled() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); when(mockedElement.isEnabled()) .thenThrow(new ElementNotVisibleException("")) .thenThrow(new ElementNotVisibleException("")) .thenReturn(false); boolean selected = driver.findElement(By.name("foo")).isEnabled(); assertThat(clock.now(), is(200L)); assertThat(selected, is(false)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(3)).isEnabled(); }
@Test void isEnabledShouldThrowIfTheElementIsNotVisible() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); when(mockedElement.isEnabled()) .thenThrow(ElementNotVisibleException.class); assertThrows(ElementNotVisibleException.class, () -> driver.findElement(By.name("foo")).isEnabled()); assertThat(clock.now(), is(1000L)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(11)).isEnabled(); }
@Test void findElementInAnotherElementShouldImplicitlyWaitForAnElementToBePresent() { final WebElement mockedElement = mock(WebElement.class); final WebElement mockedElement2 = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenThrow(NoSuchElementException.class) .thenThrow(NoSuchElementException.class) .thenReturn(mockedElement); when(mockedElement.findElement(By.name("bar"))) .thenThrow(NoSuchElementException.class) .thenThrow(NoSuchElementException.class) .thenReturn(mockedElement2); WebElement element = driver.findElement(By.name("foo")); WebElement element2 = element.findElement(By.name("bar")); assertThat(clock.now(), is(400L)); verify(mockedDriver, times(3)).findElement(By.name("foo")); verify(mockedElement, times(3)).findElement(By.name("bar")); assertThat(element2, equalTo(mockedElement2)); }
@Test void findElementInAnotherElementShouldThrowIfElementIsNotFound() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenThrow(NoSuchElementException.class) .thenThrow(NoSuchElementException.class) .thenReturn(mockedElement); when(mockedElement.findElement(By.name("bar"))) .thenThrow(NoSuchElementException.class); WebElement element = driver.findElement(By.name("foo")); assertThrows(NoSuchElementException.class, () -> element.findElement(By.name("bar"))); assertThat(clock.now(), is(1200L)); verify(mockedDriver, times(3)).findElement(By.name("foo")); verify(mockedElement, times(11)).findElement(By.name("bar")); }
@Test void findElementShouldThrowIfElementIsNotFound() { when(mockedDriver.findElement(By.name("foo"))) .thenThrow(NoSuchElementException.class); assertThrows(NoSuchElementException.class, () -> driver.findElement(By.name("foo"))); assertThat(clock.now(), is(1000L)); verify(mockedDriver, times(11)).findElement(By.name("foo")); }
@Test void findElementChainShouldImplicitlyWaitForAnElementToBePresent() { final WebElement mockedElement1 = mock(WebElement.class); final WebElement mockedElement2 = mock(WebElement.class); final WebElement mockedElement3 = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenThrow(NoSuchElementException.class) .thenThrow(NoSuchElementException.class) .thenReturn(mockedElement1); when(mockedElement1.findElement(By.name("bar"))) .thenThrow(NoSuchElementException.class) .thenThrow(NoSuchElementException.class) .thenReturn(mockedElement2); when(mockedElement2.findElement(By.name("baz"))) .thenThrow(NoSuchElementException.class) .thenThrow(NoSuchElementException.class) .thenReturn(mockedElement3); WebElement element1 = driver.findElement(By.name("foo")); WebElement element2 = element1.findElement(By.name("bar")); WebElement element3 = element2.findElement(By.name("baz")); assertThat(clock.now(), is(600L)); verify(mockedDriver, times(3)).findElement(By.name("foo")); verify(mockedElement1, times(3)).findElement(By.name("bar")); verify(mockedElement2, times(3)).findElement(By.name("baz")); assertThat(element3, equalTo(mockedElement3)); }
@Test void findElementChainShouldThrowIfElementIsNotFound() { final WebElement mockedElement1 = mock(WebElement.class); final WebElement mockedElement2 = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenThrow(NoSuchElementException.class) .thenThrow(NoSuchElementException.class) .thenReturn(mockedElement1); when(mockedElement1.findElement(By.name("bar"))) .thenThrow(NoSuchElementException.class) .thenThrow(NoSuchElementException.class) .thenReturn(mockedElement2); when(mockedElement2.findElement(By.name("baz"))) .thenThrow(NoSuchElementException.class); WebElement element1 = driver.findElement(By.name("foo")); WebElement element2 = element1.findElement(By.name("bar")); assertThrows(NoSuchElementException.class, () -> element2.findElement(By.name("baz"))); assertThat(clock.now(), is(1400L)); verify(mockedDriver, times(3)).findElement(By.name("foo")); verify(mockedElement1, times(3)).findElement(By.name("bar")); verify(mockedElement2, times(11)).findElement(By.name("baz")); }
@Test void clickShouldImplicitlyWaitForTheElementToBeVisible() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); doThrow(ElementNotVisibleException.class) .doThrow(ElementNotVisibleException.class) .doNothing() .when(mockedElement).click(); driver.findElement(By.name("foo")).click(); assertThat(clock.now(), is(200L)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(3)).click(); }
@Test void clickShouldThrowIfTheElementIsNotVisible() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); doThrow(ElementNotVisibleException.class) .when(mockedElement).click(); assertThrows(ElementNotVisibleException.class, () -> driver.findElement(By.name("foo")).click()); assertThat(clock.now(), is(1000L)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(11)).click(); }
@Test void submitShouldImplicitlyWaitForTheElementToBeVisible() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); doThrow(ElementNotVisibleException.class) .doThrow(ElementNotVisibleException.class) .doNothing() .when(mockedElement).submit(); driver.findElement(By.name("foo")).submit(); assertThat(clock.now(), is(200L)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(3)).submit(); }
@Test void submitShouldThrowIfTheElementIsNotVisible() { final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); doThrow(ElementNotVisibleException.class) .when(mockedElement).submit(); assertThrows(ElementNotVisibleException.class, () -> driver.findElement(By.name("foo")).submit()); assertThat(clock.now(), is(1000L)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(11)).submit(); }
@Test void sendKeysShouldImplicitlyWaitForTheElementToBeVisible() { final String text = "To be or not to be"; final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); doThrow(ElementNotVisibleException.class) .doThrow(ElementNotVisibleException.class) .doNothing() .when(mockedElement).sendKeys(text); driver.findElement(By.name("foo")).sendKeys(text); assertThat(clock.now(), is(200L)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(3)).sendKeys(text); }
@Test void sendKeysShouldThrowIfTheElementIsNotVisible() { final String text = "To be or not to be"; final WebElement mockedElement = mock(WebElement.class); when(mockedDriver.findElement(By.name("foo"))) .thenReturn(mockedElement); doThrow(ElementNotVisibleException.class) .when(mockedElement).sendKeys(text); assertThrows(ElementNotVisibleException.class, () -> driver.findElement(By.name("foo")).sendKeys(text)); assertThat(clock.now(), is(1000L)); verify(mockedDriver, times(1)).findElement(By.name("foo")); verify(mockedElement, times(11)).sendKeys(text); }