@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 shouldUseFindAllByAnnotationsWherePossible() throws Exception { Field f = Page.class.getDeclaredField("listById"); final WebDriver driver = mock(WebDriver.class); final By by = By.id("foo"); final WebElement element1 = mock(WebElement.class, "webElement1"); final WebElement element2 = mock(WebElement.class, "webElement2"); final List<WebElement> list = Arrays.asList(element1, element2); when(driver.findElements(by)).thenReturn(list); ElementLocator locator = newLocator(driver, f); locator.findElements(); }
@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 shouldDelegateToDriverInstanceToFindElementList() throws Exception { Field f = Page.class.getDeclaredField("list"); final WebDriver driver = mock(WebDriver.class); final By by = new ByIdOrName("list"); final WebElement element1 = mock(WebElement.class, "webElement1"); final WebElement element2 = mock(WebElement.class, "webElement2"); final List<WebElement> list = Arrays.asList(element1, element2); when(driver.findElements(by)).thenReturn(list); ElementLocator locator = newLocator(driver, f); List<WebElement> returnedList = locator.findElements(); assertEquals(list, returnedList); }
@Test public void cachedElementListShouldBeCached() throws Exception { Field f = Page.class.getDeclaredField("cachedList"); final WebDriver driver = mock(WebDriver.class); final By by = new ByIdOrName("cachedList"); final WebElement element1 = mock(WebElement.class, "webElement1"); final WebElement element2 = mock(WebElement.class, "webElement2"); final List<WebElement> list = Arrays.asList(element1, element2); when(driver.findElements(by)).thenReturn(list); ElementLocator locator = newLocator(driver, f); locator.findElements(); locator.findElements(); verify(driver, times(1)).findElements(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 } }