org.openqa.selenium.support.pagefactory.ElementLocatorFactory Java Examples

The following examples show how to use org.openqa.selenium.support.pagefactory.ElementLocatorFactory. 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: PageObjectInjector.java    From bobcat with Apache License 2.0 5 votes vote down vote up
/**
 * This method creates the object of type clazz within context defined by the top web element and
 * the frame path provided as the parameter.
 *
 * @param clazz PageObject class.
 * @param framePath instance of FramePath.
 * @param <T> type of PageObject class that will be returned.
 * @return instance of Injector.
 */
public <T> T inject(Class<T> clazz, FramePath framePath) {
  final ElementLocatorFactory elementLocatorFactory = new BobcatDefaultElementLocatorFactory(
      webDriver, injector);
  stack.push(new PageObjectContext(elementLocatorFactory, framePath));
  try {
    return injector.getInstance(clazz);
  } finally {
    stack.pop();
  }
}
 
Example #2
Source File: PageObjectInjector.java    From bobcat with Apache License 2.0 5 votes vote down vote up
/**
 * This method creates the object of type clazz within context defined by the "scope" WebElement
 * parameter and frame path provided as the parameter.
 *
 * @param clazz PageObject class.
 * @param scope WebElement for scope definition.
 * @param framePath instance of FramePath.
 * @param <T> type of PageObject class that will be returned.
 * @return instance of Injector.
 */
public <T> T inject(Class<T> clazz, WebElement scope, FramePath framePath) {
  final ElementLocatorFactory elementLocatorFactory =
      new WebElementScopedLocatorFactory(webDriver, scope, injector);
  stack.push(new PageObjectContext(elementLocatorFactory, framePath));
  try {
    return injector.getInstance(clazz);
  } finally {
    stack.pop();
  }
}
 
Example #3
Source File: CurrentScopeListProvider.java    From bobcat with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private List<WebElement> retrieveListFromFactory(ElementLocatorFactory factory) {
  InvocationHandler handler = new LocatingElementListHandler(
      ((ScopedElementLocatorFactory) factory).getCurrentScope());
  return (List<WebElement>) Proxy
      .newProxyInstance(WebElement.class.getClassLoader(), new Class[] {List.class}, handler);
}
 
Example #4
Source File: ScopedElementLocator.java    From bobcat with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs ScopedElementLocator.
 *
 * @param scopeFactory instance of ElementLocatorFactory
 * @param scopeField field for scope definition
 * @param searchField class field
 */
public ScopedElementLocator(ElementLocatorFactory scopeFactory,
    Field scopeField, Field searchField, Injector injector) {
  this.scopeFactory = scopeFactory;
  this.scopeField = scopeField;
  this.searchField = searchField;
  this.injector = injector;
}
 
Example #5
Source File: CurrentScopeListProviderTest.java    From bobcat with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnEmptyListWhenFactoryInStackIsNotScoped() {
  //given
  ElementLocatorFactory elementLocatorFactory = mock(ElementLocatorFactory.class);
  when(contextStack.peek()).thenReturn(pageObjectContext);
  when(contextStack.peek().getElementLocatorFactory()).thenReturn(elementLocatorFactory);

  //when
  List<WebElement> actual = testedObject.get();

  //then
  assertThat(actual).isEmpty();
}
 
Example #6
Source File: PageObjectContext.java    From bobcat with Apache License 2.0 4 votes vote down vote up
/**
 * @return Element locator factory that is stored in the context.
 */
public ElementLocatorFactory getElementLocatorFactory() {
  return elementLocatorFactory;
}
 
Example #7
Source File: CurrentScopeListProvider.java    From bobcat with Apache License 2.0 4 votes vote down vote up
private List<WebElement> getScopedList() {
  ElementLocatorFactory factory = stack.peek().getElementLocatorFactory();
  return factory instanceof ScopedElementLocatorFactory ?
      retrieveListFromFactory(factory) :
      Collections.emptyList();
}
 
Example #8
Source File: CurrentWebElementProvider.java    From bobcat with Apache License 2.0 4 votes vote down vote up
private WebElement getScopedWebElement() {
  ElementLocatorFactory factory = stack.peek().getElementLocatorFactory();
  return (factory instanceof ParentElementLocatorProvider) ?
      getWebElementFromFactory(factory) :
      getTopElement();
}
 
Example #9
Source File: CurrentWebElementProvider.java    From bobcat with Apache License 2.0 4 votes vote down vote up
private WebElement getWebElementFromFactory(ElementLocatorFactory factory) {
  InvocationHandler handler = new LocatingElementHandler(
      ((ParentElementLocatorProvider) factory).getCurrentScope());
  return (WebElement) Proxy.newProxyInstance(WebElement.class.getClassLoader(),
      new Class[] {WebElement.class, WrapsElement.class, Locatable.class}, handler);
}
 
Example #10
Source File: ContextStack.java    From bobcat with Apache License 2.0 4 votes vote down vote up
private PageObjectContext getDefaultPageObjectContext(WebDriver webDriver) {
  ElementLocatorFactory elementLocatorFactory = new BobcatDefaultElementLocatorFactory(webDriver,
      injector);
  FramePath framePath = new FramePath();
  return new PageObjectContext(elementLocatorFactory, framePath);
}
 
Example #11
Source File: ExtendedFieldDecorator.java    From carina with Apache License 2.0 4 votes vote down vote up
public ExtendedFieldDecorator(ElementLocatorFactory factory, WebDriver webDriver) {
    this.factory = factory;
    this.webDriver = webDriver;
}
 
Example #12
Source File: ScopedElementLocatorFactory.java    From bobcat with Apache License 2.0 3 votes vote down vote up
/**
 * Creates an element-scoped locator factory.
 *
 * @param webDriver     WebDriver instance that will serve as a global scope for Global-annotated
 *                      fields
 * @param parentFactory Factory that represents scope for elements without Global annotation
 * @param parentField   Field that contains current field, reducing the scope indicated by
 *                      parentFactory
 */
public ScopedElementLocatorFactory(WebDriver webDriver,
    ElementLocatorFactory parentFactory, Field parentField, Injector injector) {
  this.parentFactory = parentFactory;
  this.parentField = parentField;
  this.webDriver = webDriver;
  this.injector = injector;
}
 
Example #13
Source File: PageObjectContext.java    From bobcat with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs PageObjectContext. Initializes its fields.
 *
 * @param elementLocatorFactory ElementLocatorFactory instance.
 * @param framePath             FramePath instance.
 */
public PageObjectContext(ElementLocatorFactory elementLocatorFactory, FramePath framePath) {
  this.elementLocatorFactory = elementLocatorFactory;
  this.framePath = framePath;
}
 
Example #14
Source File: GuiceAwareFieldDecorator.java    From bobcat with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor. Initializes decorator with the element locator factory that will be used for
 * producing
 * values for decorated fields.
 *
 * @param factory                 represents ElementLocatorFactory
 * @param bobcatWebElementFactory instance of BobcatWebElementFactory
 */
public GuiceAwareFieldDecorator(ElementLocatorFactory factory,
    BobcatWebElementFactory bobcatWebElementFactory) {
  super(factory);
  this.bobcatWebElementFactory = bobcatWebElementFactory;
}
 
Example #15
Source File: PageFactory.java    From selenium with Apache License 2.0 2 votes vote down vote up
/**
 * Similar to the other "initElements" methods, but takes an {@link ElementLocatorFactory} which
 * is used for providing the mechanism for finding elements. If the ElementLocatorFactory returns
 * null then the field won't be decorated.
 *
 * @param factory The factory to use
 * @param page    The object to decorate the fields of
 */
public static void initElements(ElementLocatorFactory factory, Object page) {
  initElements(new DefaultFieldDecorator(factory), page);
}