org.openqa.selenium.support.ByIdOrName Java Examples

The following examples show how to use org.openqa.selenium.support.ByIdOrName. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AjaxElementLocatorTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #2
Source File: AjaxElementLocatorTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: AjaxElementLocatorTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: UsingPageFactoryTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #5
Source File: DefaultElementLocatorTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: DefaultElementLocatorTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #7
Source File: DefaultElementLocatorTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #8
Source File: SelenideAppiumFieldDecorator.java    From selenide-appium with MIT License 5 votes vote down vote up
@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);
}
 
Example #9
Source File: ElementFactory.java    From aquality-selenium-java with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #10
Source File: AjaxElementLocatorTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #11
Source File: DefaultElementLocatorTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #12
Source File: DefaultElementLocatorTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #13
Source File: DefaultElementLocatorTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #14
Source File: HowTest.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Test
public void testBuildByIdOrName(){
  assertThat(How.ID_OR_NAME.buildBy(VALUE).toString())
      .isEqualTo(new ByIdOrName(VALUE).toString());
}
 
Example #15
Source File: AnnotationsTest.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Test
public void testDefaultList() throws Exception {
  assertThat(new Annotations(getClass().getField("defaultList_field")).buildBy())
      .isEqualTo(new ByIdOrName("defaultList_field"));
}
 
Example #16
Source File: AnnotationsTest.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Test
public void testDefault() throws Exception {
  assertThat(new Annotations(getClass().getField("default_field")).buildBy())
      .isEqualTo(new ByIdOrName("default_field"));
}
 
Example #17
Source File: Annotations.java    From selenium with Apache License 2.0 4 votes vote down vote up
protected By buildByFromDefault() {
  return new ByIdOrName(field.getName());
}