下面列出了怎么用org.openqa.selenium.support.ByIdOrName的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void shouldContinueAttemptingToFindElement() 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))
.thenThrow(new NoSuchElementException("bar"))
.thenReturn(element);
ElementLocator locator = newLocator(driver, f);
WebElement returnedElement = locator.findElement();
assertThat(returnedElement).isEqualTo(element);
}
@Test
public void shouldContinueAttemptingToFindElements() 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);
final List<WebElement> elementList = new ArrayList<>();
elementList.add(element);
when(driver.findElements(by))
.thenThrow(new NoSuchElementException("bar"))
.thenReturn(elementList);
ElementLocator locator = newLocator(driver, f);
List<WebElement> returnedList = locator.findElements();
assertThat(returnedList.get(0)).isEqualTo(element);
}
@Test
public void shouldThrowNoSuchElementExceptionIfElementTakesTooLongToAppear() throws Exception {
Field f = Page.class.getDeclaredField("first");
final WebDriver driver = mock(WebDriver.class);
final By by = new ByIdOrName("first");
when(driver.findElement(by)).thenThrow(new NoSuchElementException("bar"));
ElementLocator locator = new MonkeyedAjaxElementLocator(clock, driver, f, 2);
assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(locator::findElement);
// Look ups:
// 1. In "isLoaded"
// 2. Immediately after call of load. (clock is 0)
// 3. First sleep, then third call. (clock is 1)
// 4. Main loop is now over. Final call as we exit to see if we've loaded.
// The last call guarantees we've called "isLoaded" at least once after a load.
verify(driver, times(4)).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();
assertThat(returnedList).isEqualTo(list);
}
@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 shouldNotCacheNormalElementList() 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);
locator.findElements();
locator.findElements();
verify(driver, times(2)).findElements(by);
}
@Test
public void testDecoratedElementsShouldBeUnwrapped() {
final RemoteWebElement element = new RemoteWebElement();
element.setId("foo");
WebDriver driver = mock(WebDriver.class);
when(driver.findElement(new ByIdOrName("element"))).thenReturn(element);
PublicPage page = new PublicPage();
PageFactory.initElements(driver, page);
Object seen = new WebElementToJsonConverter().apply(page.element);
Object expected = new WebElementToJsonConverter().apply(element);
assertThat(seen).isEqualTo(expected);
}
private static Map<Class<? extends By>, String> getLocatorToXPathTemplateMap() {
Map<Class<? extends By>, String> locatorToXPathTemplateMap = new HashMap<>();
locatorToXPathTemplateMap.put(ByClassName.class, "//*[contains(@class,'%s')]");
locatorToXPathTemplateMap.put(ByName.class, "//*[@name='%s']");
locatorToXPathTemplateMap.put(ById.class, "//*[@id='%s']");
locatorToXPathTemplateMap.put(ByIdOrName.class, "//*[@id='%1$s' or @name='%1$s']");
return locatorToXPathTemplateMap;
}
@Override
public Object decorate(ClassLoader loader, Field field) {
builder.setAnnotated(field);
By selector = builder.buildBy();
if (selector == null) {
selector = new Annotations(field).buildBy();
}
if (selector instanceof ByIdOrName) {
return decorateWithAppium(loader, field);
}
else if (SelenideElement.class.isAssignableFrom(field.getType())) {
return ElementFinder.wrap(driver, driver.getWebDriver(), selector, 0);
}
else if (ElementsCollection.class.isAssignableFrom(field.getType())) {
return new ElementsCollection(new BySelectorCollection(driver, selector));
}
else if (ElementsContainer.class.isAssignableFrom(field.getType())) {
return createElementsContainer(selector, field);
}
else if (isDecoratableList(field, ElementsContainer.class)) {
return createElementsContainerList(field);
}
else if (isDecoratableList(field, SelenideElement.class)) {
return SelenideElementListProxy.wrap(driver, factory.createLocator(field));
}
return super.decorate(loader, field);
}
@Test
public void shouldAlwaysDoAtLeastOneAttemptAtFindingTheElement() throws Exception {
Field f = Page.class.getDeclaredField("first");
final WebDriver driver = mock(WebDriver.class);
final By by = new ByIdOrName("first");
when(driver.findElement(by)).thenThrow(new NoSuchElementException("bar"));
ElementLocator locator = new MonkeyedAjaxElementLocator(clock, driver, f, 0);
assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(locator::findElement);
verify(driver, atLeast(2)).findElement(by);
}
@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();
assertThat(returnedElement).isEqualTo(element);
}
@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 shouldNotCacheNormalElement() 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);
locator.findElement();
locator.findElement();
verify(driver, times(2)).findElement(by);
}
protected By buildByFromDefault() {
return new ByIdOrName(field.getName());
}
@Test
public void testDefault() throws Exception {
assertThat(new Annotations(getClass().getField("default_field")).buildBy())
.isEqualTo(new ByIdOrName("default_field"));
}
@Test
public void testDefaultList() throws Exception {
assertThat(new Annotations(getClass().getField("defaultList_field")).buildBy())
.isEqualTo(new ByIdOrName("defaultList_field"));
}
@Test
public void testBuildByIdOrName(){
assertThat(How.ID_OR_NAME.buildBy(VALUE).toString())
.isEqualTo(new ByIdOrName(VALUE).toString());
}