@Override public void enrich(final SearchContext searchContext, Object target) { List<Field> fields = new LinkedList<Field>(); fields.addAll(ReflectionHelper.getFieldsWithAnnotation(target.getClass(), Page.class)); for (Field field : fields) { Object page = createPage(searchContext, field.getAnnotations(), target, field, null, null); setValue(field, target, page); } }
public static <P> P setupPage(GrapheneContext context, SearchContext searchContext, Class<?> pageClass) throws Exception{ P page = (P) instantiate(pageClass); P proxy = GrapheneProxy.getProxyForHandler(GrapheneContextualHandler.forTarget(context, page), pageClass); enrichRecursively(searchContext, page); enrichRecursively(searchContext, proxy); // because of possibility of direct access to attributes from test class return proxy; }
private Class<?> getDeclaredClass(Object target, Field field) { Type type = field.getGenericType(); // check whether it is type variable e.g. T if (type instanceof TypeVariable) { return getActualType(field, target); } else { // no it is normal type, e.g. TestPage return field.getType(); } }
Class<?> declaredClass; if (target != null) { declaredClass = getDeclaredClass(target, field); } else { declaredClass = param; appendErrorMsgBegin(errorMsgBegin, declaredClass, target, method); return setupPage(grapheneContext, localSearchContext, declaredClass);
public <T> T goTo(Class<T> pageObject, Class<?> browserQualifier) { T result = null; GrapheneContext grapheneContext = GrapheneContext.getContextFor(browserQualifier); WebDriver browser = grapheneContext.getWebDriver(); try { result = PageObjectEnricher.setupPage(grapheneContext, browser, pageObject); } catch (Exception e) { throw new GrapheneTestEnricherException("Error while initializing: " + pageObject, e); } handleLocationOf(pageObject, browser); return result; }
@Override public Object[] resolve(SearchContext searchContext, Method method, Object[] resolvedParams) { StringBuffer errorMsgBegin = new StringBuffer(""); List<Object[]> paramCouple = new LinkedList<Object[]>(); paramCouple.addAll(ReflectionHelper.getParametersWithAnnotation(method, Page.class)); for (int i = 0; i < resolvedParams.length; i++) { if (paramCouple.get(i) != null) { Class<?> param = (Class<?>) paramCouple.get(i)[0]; Annotation[] parameterAnnotations = (Annotation[]) paramCouple.get(i)[1]; Object page = createPage(searchContext, parameterAnnotations, null, null, method, param); resolvedParams[i] = page; } } return resolvedParams; }
@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<?> declaredClass; if (target != null) { declaredClass = getDeclaredClass(target, field); } else { declaredClass = param; appendErrorMsgBegin(errorMsgBegin, declaredClass, target, method); return setupPage(grapheneContext, localSearchContext, declaredClass);
public <T> T goTo(Class<T> pageObject, Class<?> browserQualifier) { T result = null; GrapheneContext grapheneContext = GrapheneContext.getContextFor(browserQualifier); WebDriver browser = grapheneContext.getWebDriver(); try { result = PageObjectEnricher.setupPage(grapheneContext, browser, pageObject); } catch (Exception e) { throw new GrapheneTestEnricherException("Error while initializing: " + pageObject, e); } handleLocationOf(pageObject, browser); return result; }
@Override public Object[] resolve(SearchContext searchContext, Method method, Object[] resolvedParams) { StringBuffer errorMsgBegin = new StringBuffer(""); List<Object[]> paramCouple = new LinkedList<Object[]>(); paramCouple.addAll(ReflectionHelper.getParametersWithAnnotation(method, Page.class)); for (int i = 0; i < resolvedParams.length; i++) { if (paramCouple.get(i) != null) { Class<?> param = (Class<?>) paramCouple.get(i)[0]; Annotation[] parameterAnnotations = (Annotation[]) paramCouple.get(i)[1]; Object page = createPage(searchContext, parameterAnnotations, null, null, method, param); resolvedParams[i] = page; } } return resolvedParams; }
public static <P> P setupPage(GrapheneContext context, SearchContext searchContext, Class<?> pageClass) throws Exception{ P page = (P) instantiate(pageClass); P proxy = GrapheneProxy.getProxyForHandler(GrapheneContextualHandler.forTarget(context, page), pageClass); enrichRecursively(searchContext, page); enrichRecursively(searchContext, proxy); // because of possibility of direct access to attributes from test class return proxy; }
@Override public void enrich(final SearchContext searchContext, Object target) { List<Field> fields = new LinkedList<Field>(); fields.addAll(ReflectionHelper.getFieldsWithAnnotation(target.getClass(), Page.class)); for (Field field : fields) { Object page = createPage(searchContext, field.getAnnotations(), target, field, null, null); setValue(field, target, page); } }
private Class<?> getDeclaredClass(Object target, Field field) { Type type = field.getGenericType(); // check whether it is type variable e.g. T if (type instanceof TypeVariable) { return getActualType(field, target); } else { // no it is normal type, e.g. TestPage return field.getType(); } }