@Test public void shouldUseFindByAnnotationsWherePossible() throws Exception { Field f = Page.class.getDeclaredField("byId"); final WebDriver driver = mock(WebDriver.class); final By by = By.id("foo"); final WebElement element = mock(WebElement.class); when(driver.findElement(by)).thenReturn(element); ElementLocator locator = newLocator(driver, f); locator.findElement(); }
@Test public void shouldDelegateToDriverInstanceToFindElement() throws Exception { Field f = Page.class.getDeclaredField("first"); final WebDriver driver = mock(WebDriver.class); final By by = new ByIdOrName("first"); final WebElement element = mock(WebElement.class); when(driver.findElement(by)).thenReturn(element); ElementLocator locator = newLocator(driver, f); WebElement returnedElement = locator.findElement(); assertEquals(element, returnedElement); }
@Test public void cachedElementShouldBeCached() throws Exception { Field f = Page.class.getDeclaredField("cached"); final WebDriver driver = mock(WebDriver.class); final By by = new ByIdOrName("cached"); final WebElement element = mock(WebElement.class); when(driver.findElement(by)).thenReturn(element); ElementLocator locator = newLocator(driver, f); locator.findElement(); locator.findElement(); verify(driver, times(1)).findElement(by); }
@Test public void shouldNotMaskNoSuchElementExceptionIfThrown() throws Exception { Field f = Page.class.getDeclaredField("byId"); final WebDriver driver = mock(WebDriver.class); final By by = By.id("foo"); when(driver.findElement(by)).thenThrow(new NoSuchElementException("Foo")); ElementLocator locator = newLocator(driver, f); try { locator.findElement(); fail("Should have allowed the exception to bubble up"); } catch (NoSuchElementException e) { // This is expected } }