public <T> T createPageFragment(Class<T> clazz, WebElement root) { return PageFragmentEnricher.createPageFragment(clazz, root); }
protected final void setupPageFragmentList(SearchContext searchContext, Object target, Field field) throws ClassNotFoundException { GrapheneContext grapheneContext = ((GrapheneProxyInstance) searchContext).getGrapheneContext(); // the by retrieved in this way is never null, by default it is ByIdOrName using field name By rootBy = FindByUtilities.getCorrectBy(field, configuration.get().getDefaultElementLocatingStrategy()); List<?> pageFragments = createPageFragmentList(getListType(field), searchContext, rootBy); setValue(field, target, pageFragments); }
private static Class<?> extractWebElementType(Class<?> clazz) { List<Class<?>> interfaces = Arrays.asList(clazz.getInterfaces()); if (interfaces.contains(GrapheneElement.class)) { return GrapheneElement.class; } else if (interfaces.contains(WebElement.class)) { return WebElement.class; } else { Class<?> superclass = clazz.getSuperclass(); if (!Object.class.equals(superclass)) { return extractWebElementType(superclass); } } return null; }
Class<?> webElementInterface = extractWebElementType(clazz); if (webElementInterface != null) { pageFragment = createProxyDelegatingToRoot(root, clazz, webElementInterface); } else { pageFragment = instantiate(clazz); setValue(roots.get(0), pageFragment, root); enrichRecursively(root, pageFragment); T proxy = GrapheneProxy.getProxyForHandler(GrapheneContextualHandler.forTarget(grapheneContext, pageFragment), clazz); enrichRecursively(root, proxy); // because of possibility of direct access to attributes from test class return proxy; } catch (NoSuchMethodException ex) {
protected final void setupPageFragment(SearchContext searchContext, Object target, Field field) { GrapheneContext grapheneContext = ((GrapheneProxyInstance) searchContext).getGrapheneContext(); // the by retrieved in this way is never null, by default it is ByIdOrName using field name By rootBy = FindByUtilities.getCorrectBy(field, configuration.get().getDefaultElementLocatingStrategy()); WebElement root = WebElementUtils.findElementLazily(rootBy, searchContext); Object pageFragment = createPageFragment(field.getType(), root); setValue(field, target, pageFragment); }
@Override public void enrich(final SearchContext searchContext, Object target) { List<Field> fields = FindByUtilities.getListOfFieldsAnnotatedWithFindBys(target); for (Field field : fields) { GrapheneContext grapheneContext = searchContext == null ? null : ((GrapheneProxyInstance) searchContext) .getGrapheneContext(); final SearchContext localSearchContext; if (grapheneContext == null) { grapheneContext = GrapheneContext.getContextFor(ReflectionHelper.getQualifier(field.getAnnotations())); localSearchContext = grapheneContext.getWebDriver(SearchContext.class); } else { localSearchContext = searchContext; } // Page fragment if (isPageFragmentClass(field.getType(), target)) { setupPageFragment(localSearchContext, target, field); // List<Page fragment> } else { try { if (field.getType().isAssignableFrom(List.class) && isPageFragmentClass(getListType(field), target)) { setupPageFragmentList(localSearchContext, target, field); } } catch (ClassNotFoundException e) { throw new PageFragmentInitializationException(e.getMessage(), e); } } } }
@Before public void prepareServiceLoader() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException { grapheneEnricher = new GrapheneEnricher(); Instance<GrapheneConfiguration> configuration = new Instance() { @Override public Object get() { return new GrapheneConfiguration(); } }; webElementEnricher = new WebElementEnricher(configuration); pageObjectEnricher = new PageObjectEnricher(); pageFragmentEnricher = new PageFragmentEnricher(configuration); when(serviceLoaderInstance.get()).thenReturn(serviceLoader); when(serviceLoader.all(TestEnricher.class)).thenReturn(Arrays.asList(grapheneEnricher)); when(serviceLoader.all(SearchContextTestEnricher.class)).thenReturn(Arrays.asList(webElementEnricher, pageObjectEnricher, pageFragmentEnricher)); for (Object o: Arrays.asList(grapheneEnricher, webElementEnricher, pageObjectEnricher, pageFragmentEnricher)) { Field serviceLoaderField; if (o instanceof SearchContextTestEnricher) { serviceLoaderField = AbstractSearchContextEnricher.class.getDeclaredField("serviceLoader"); } else { serviceLoaderField = o.getClass().getDeclaredField("serviceLoader"); } if (!serviceLoaderField.isAccessible()) { serviceLoaderField.setAccessible(true); } serviceLoaderField.set(o, serviceLoaderInstance); } GrapheneContext.setContextFor(new GrapheneConfiguration(), browser, Default.class); GrapheneRuntime.pushInstance(new DefaultGrapheneRuntime()); }
Class<?> webElementInterface = extractWebElementType(clazz); if (webElementInterface != null) { pageFragment = createProxyDelegatingToRoot(root, clazz, webElementInterface); } else { pageFragment = instantiate(clazz); setValue(roots.get(0), pageFragment, root); enrichRecursively(root, pageFragment); T proxy = GrapheneProxy.getProxyForHandler(GrapheneContextualHandler.forTarget(grapheneContext, pageFragment), clazz); enrichRecursively(root, proxy); // because of possibility of direct access to attributes from test class return proxy; } catch (NoSuchMethodException ex) {
protected final void setupPageFragment(SearchContext searchContext, Object target, Field field) { GrapheneContext grapheneContext = ((GrapheneProxyInstance) searchContext).getGrapheneContext(); // the by retrieved in this way is never null, by default it is ByIdOrName using field name By rootBy = FindByUtilities.getCorrectBy(field, configuration.get().getDefaultElementLocatingStrategy()); WebElement root = WebElementUtils.findElementLazily(rootBy, searchContext); Object pageFragment = createPageFragment(field.getType(), root); setValue(field, target, pageFragment); }
@Override public void enrich(final SearchContext searchContext, Object target) { List<Field> fields = FindByUtilities.getListOfFieldsAnnotatedWithFindBys(target); for (Field field : fields) { GrapheneContext grapheneContext = searchContext == null ? null : ((GrapheneProxyInstance) searchContext) .getGrapheneContext(); final SearchContext localSearchContext; if (grapheneContext == null) { grapheneContext = GrapheneContext.getContextFor(ReflectionHelper.getQualifier(field.getAnnotations())); localSearchContext = grapheneContext.getWebDriver(SearchContext.class); } else { localSearchContext = searchContext; } // Page fragment if (isPageFragmentClass(field.getType(), target)) { setupPageFragment(localSearchContext, target, field); // List<Page fragment> } else { try { if (field.getType().isAssignableFrom(List.class) && isPageFragmentClass(getListType(field), target)) { setupPageFragmentList(localSearchContext, target, field); } } catch (ClassNotFoundException e) { throw new PageFragmentInitializationException(e.getMessage(), e); } } } }
public <T> T createPageFragment(Class<T> clazz, WebElement root) { return PageFragmentEnricher.createPageFragment(clazz, root); }
protected final void setupPageFragmentList(SearchContext searchContext, Object target, Field field) throws ClassNotFoundException { GrapheneContext grapheneContext = ((GrapheneProxyInstance) searchContext).getGrapheneContext(); // the by retrieved in this way is never null, by default it is ByIdOrName using field name By rootBy = FindByUtilities.getCorrectBy(field, configuration.get().getDefaultElementLocatingStrategy()); List<?> pageFragments = createPageFragmentList(getListType(field), searchContext, rootBy); setValue(field, target, pageFragments); }
private static Class<?> extractWebElementType(Class<?> clazz) { List<Class<?>> interfaces = Arrays.asList(clazz.getInterfaces()); if (interfaces.contains(GrapheneElement.class)) { return GrapheneElement.class; } else if (interfaces.contains(WebElement.class)) { return WebElement.class; } else { Class<?> superclass = clazz.getSuperclass(); if (!Object.class.equals(superclass)) { return extractWebElementType(superclass); } } return null; }
public <T extends SelfAwareFragment> T getResource(Class<T> fragment) { return PageFragmentEnricher.createPageFragment(fragment, resource); } }
@Override public <T> T getContent(Class<T> clazz) { return PageFragmentEnricher.createPageFragment(clazz, root); }
@Override public Object getTarget() { List<WebElement> elements = searchContext.findElements(rootBy); List<T> fragments = new ArrayList<T>(); for (int i = 0; i < elements.size(); i++) { fragments.add(createPageFragment(clazz, WebElementUtils.findElementLazily(rootBy, searchContext, i))); } return fragments; } }, List.class);
@Override public Object getTarget() { List<WebElement> elements = searchContext.findElements(rootBy); List<T> fragments = new ArrayList<T>(); for (int i = 0; i < elements.size(); i++) { fragments.add(createPageFragment(clazz, WebElementUtils.findElementLazily(rootBy, searchContext, i))); } return fragments; } }, List.class);