/** * This method produces value for the field. It constructs the context for the creation out of * paren't context and the field's own frame info. */ @Override public Optional<Object> provideValue(Object pageObject, Field field, PageObjectContext context) { By selector = PageObjectProviderHelper.getSelectorFromPageObject(field); ElementLocatorFactory elementLocatorFactory = new NestedSelectorScopedLocatorFactory(webDriver, selector, context.getElementLocatorFactory(), AnnotationsHelper.isGlobal(field)); final FramePath framePath = frameMap.get(pageObject); contextStack.push(new PageObjectContext(elementLocatorFactory, framePath)); Object scopedPageObject = null; try { scopedPageObject = injector.getInstance(field.getType()); } catch (Exception e) { if (e instanceof ConfigurationException) { ConfigurationException ce = (ConfigurationException) e; throw new BobcatRuntimeException( "Configuration exception: " + ce.getErrorMessages().toString(), e); } throw new BobcatRuntimeException(e.getMessage(), e); } finally { contextStack.pop(); } return Optional.ofNullable(scopedPageObject); }
/** * This method produces value for the field. It constructs the context for the creation out of * parent's context and the field's own frame info. */ @Override public Optional<Object> provideValue(Object pageObject, Field field, PageObjectContext context) { final ElementLocatorFactory elementLocatorFactory = new ScopedElementLocatorFactory(webDriver, context.getElementLocatorFactory(), field); final FramePath framePath = frameMap.get(pageObject); contextStack.push(new PageObjectContext(elementLocatorFactory, framePath)); Object scopedPageObject = null; try { scopedPageObject = injector.getInstance(field.getType()); } catch (Exception e) { if (e instanceof ConfigurationException) { ConfigurationException ce = (ConfigurationException) e; throw new BobcatRuntimeException( "Configuration exception: " + ce.getErrorMessages().toString(), e); } throw new BobcatRuntimeException(e.getMessage(), e); } finally { contextStack.pop(); } return Optional.ofNullable(scopedPageObject); }
/** * 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 DefaultElementLocatorFactory(webDriver); stack.push(new PageObjectContext(elementLocatorFactory, framePath)); try { return injector.getInstance(clazz); } finally { stack.pop(); } }
/** * 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); stack.push(new PageObjectContext(elementLocatorFactory, framePath)); try { return injector.getInstance(clazz); } finally { stack.pop(); } }
@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); }
/** * Creates a selector-scoped locator factory with scope within parent scope. * * @param searchContext Initial search context * @param selector Scope of the created locator * @param parentFactory Factory that represents scope for elements */ public NestedSelectorScopedLocatorFactory(SearchContext searchContext, By selector, ElementLocatorFactory parentFactory, boolean globalCurrenScope) { this.searchContext = searchContext; this.selector = selector; this.parentFactory = parentFactory; this.globalCurrenScope = globalCurrenScope; }
private SearchContext getSearchContext(PageObjectContext context, Field field) { SearchContext searchContext = webDriver; ElementLocatorFactory elementLocatorFactory = context.getElementLocatorFactory(); if (elementLocatorFactory instanceof ParentElementLocatorProvider && !AnnotationsHelper.isGlobal(field)) { searchContext = acquireSearchContext(elementLocatorFactory); } return searchContext; }
private SearchContext acquireSearchContext(ElementLocatorFactory elementLocatorFactory) { SearchContext searchContext; ElementLocator parentElementLocator = ((ParentElementLocatorProvider) elementLocatorFactory). getCurrentScope(); if (parentElementLocator instanceof SearchContextAwareLocator) { searchContext = ((SearchContextAwareLocator) parentElementLocator).getSearchContext(); } else { searchContext = parentElementLocator.findElement(); } return searchContext; }
@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(); }
/** * Searches for the field of a given type, identified by the selector class. * * @param selector Locator by which to locate the dialog field. * @param dialogFieldType Class that represents dialog field. * @param <T> dialog field class * @return A dialogFieldType's instance that represents dialog field. */ public <T> T getFieldBySelector(By selector, Class<T> dialogFieldType) { ElementLocatorFactory fieldScope = new SelectorScopedLocatorFactory(webDriver, selector); elementLocatorStack.push(new PageObjectContext(fieldScope, FramePath.parsePath("$cq"))); try { return injector.getInstance(dialogFieldType); } finally { elementLocatorStack.pop(); } }
/** * Constructor. * * @param factory The factory of the elements locator. */ public WiseDecorator(ElementLocatorFactory factory) { ElementDecoratorChain wiseComponentDecorator = new WiseComponentDecorator(factory); ElementDecoratorChain wiseFrameDecorator = new WiseFrameDecorator(factory); ElementDecoratorChain webElementDecorator = new WebElementDecorator(factory); ElementDecoratorChain nullDecorator = new NullDecorator(factory); wiseComponentDecorator.setNext(wiseFrameDecorator); wiseFrameDecorator.setNext(webElementDecorator); webElementDecorator.setNext(nullDecorator); this.elementDecorator = wiseComponentDecorator; }
@SuppressWarnings("unchecked") public static <E> List<E> getInstance(Class<E> clazz, List<WebElement> webElements, ElementLocatorFactory factory) { Enhancer e = new Enhancer(); e.setInterfaces(new Class[] { List.class }); e.setCallback(new WiseElementListProxy<E>(clazz, webElements, factory)); return (List<E>) e.create(); }
/** * @return Element locator factory that is stored in the context. */ public ElementLocatorFactory getElementLocatorFactory() { return elementLocatorFactory; }
private List<WebElement> getScopedList() { ElementLocatorFactory factory = stack.peek().getElementLocatorFactory(); return factory instanceof ScopedElementLocatorFactory ? retrieveListFromFactory(factory) : Collections.emptyList(); }
private WebElement getScopedWebElement() { ElementLocatorFactory factory = stack.peek().getElementLocatorFactory(); return (factory instanceof ParentElementLocatorProvider) ? getWebElementFromFactory(factory) : getTopElement(); }
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); }
private PageObjectContext getDefaultPageObjectContext(WebDriver webDriver) { ElementLocatorFactory elementLocatorFactory = new DefaultElementLocatorFactory(webDriver); FramePath framePath = new FramePath(); return new PageObjectContext(elementLocatorFactory, framePath); }
public JPageFactoryFieldDecorator(ElementLocatorFactory factory) { super(factory); }
public NullDecorator(ElementLocatorFactory factory) { super(factory); }
public WiseFrameDecorator(ElementLocatorFactory factory) { super(factory); }
private WiseElementListProxy(Class<E> clazz, List<WebElement> webElements, ElementLocatorFactory factory) { this.clazz = clazz; this.webElements = webElements; this.locatorFactory = factory; }
public ElementDecoratorChainTemplate(ElementLocatorFactory factory) { super(factory); }
public WiseComponentDecorator(ElementLocatorFactory factory) { super(factory); }
public WebElementDecorator(ElementLocatorFactory factory) { super(factory); }
public ExtendedFieldDecorator(ElementLocatorFactory factory, WebDriver webDriver) { this.factory = factory; this.webDriver = webDriver; }
/** * Constructs NestedSelectorScopedElementLocator. * * @param searchContext instance of SearchContext * @param scopeFactory instance of ElementLocatorFactory * @param selector selector * @param globalCurrentScope indicates if field is in global scope */ public NestedSelectorScopedElementLocator(SearchContext searchContext, ElementLocatorFactory scopeFactory, By selector, boolean globalCurrentScope) { this.scopeFactory = scopeFactory; this.selector = selector; this.searchContext = searchContext; this.globalCurrentScope = globalCurrentScope; }
/** * 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) { this.scopeFactory = scopeFactory; this.scopeField = scopeField; this.searchField = searchField; }
/** * 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) { this.parentFactory = parentFactory; this.parentField = parentField; this.webDriver = webDriver; }
/** * 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; }
/** * 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; }