类org.openqa.selenium.support.ByIdOrName源码实例Demo

下面列出了怎么用org.openqa.selenium.support.ByIdOrName的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: selenium   文件: AjaxElementLocatorTest.java
@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);
}
 
源代码2 项目: selenium   文件: AjaxElementLocatorTest.java
@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);
}
 
源代码3 项目: selenium   文件: AjaxElementLocatorTest.java
@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);
}
 
源代码4 项目: selenium   文件: DefaultElementLocatorTest.java
@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);
}
 
源代码5 项目: selenium   文件: DefaultElementLocatorTest.java
@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);
}
 
源代码6 项目: selenium   文件: DefaultElementLocatorTest.java
@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);
}
 
源代码7 项目: selenium   文件: UsingPageFactoryTest.java
@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);
}
 
源代码8 项目: aquality-selenium-java   文件: ElementFactory.java
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);
}
 
源代码10 项目: selenium   文件: AjaxElementLocatorTest.java
@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);
}
 
源代码11 项目: selenium   文件: DefaultElementLocatorTest.java
@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);
}
 
源代码12 项目: selenium   文件: DefaultElementLocatorTest.java
@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);
}
 
源代码13 项目: selenium   文件: DefaultElementLocatorTest.java
@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);
}
 
源代码14 项目: selenium   文件: Annotations.java
protected By buildByFromDefault() {
  return new ByIdOrName(field.getName());
}
 
源代码15 项目: selenium   文件: AnnotationsTest.java
@Test
public void testDefault() throws Exception {
  assertThat(new Annotations(getClass().getField("default_field")).buildBy())
      .isEqualTo(new ByIdOrName("default_field"));
}
 
源代码16 项目: selenium   文件: AnnotationsTest.java
@Test
public void testDefaultList() throws Exception {
  assertThat(new Annotations(getClass().getField("defaultList_field")).buildBy())
      .isEqualTo(new ByIdOrName("defaultList_field"));
}
 
源代码17 项目: selenium   文件: HowTest.java
@Test
public void testBuildByIdOrName(){
  assertThat(How.ID_OR_NAME.buildBy(VALUE).toString())
      .isEqualTo(new ByIdOrName(VALUE).toString());
}
 
 类所在包
 同包方法