@Override public void enrich(final SearchContext searchContext, Object target) { try { 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; } //by should never by null, by default it is ByIdOrName using field name By by = FindByUtilities.getCorrectBy(field, configuration.get().getDefaultElementLocatingStrategy()); // WebElement if (field.getType().isAssignableFrom(WebElement.class)) { WebElement element = WebElementUtils.findElementLazily(by, localSearchContext); setValue(field, target, element); // List<WebElement> } else if (field.getType().isAssignableFrom(List.class) && getListType(field).isAssignableFrom(WebElement.class)) { List<WebElement> elements = WebElementUtils.findElementsLazily(by, localSearchContext); setValue(field, target, elements); } } } catch (ClassNotFoundException e) { throw new IllegalStateException(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()); }
@Override public void enrich(final SearchContext searchContext, Object target) { try { 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; } //by should never by null, by default it is ByIdOrName using field name By by = FindByUtilities.getCorrectBy(field, configuration.get().getDefaultElementLocatingStrategy()); // WebElement if (field.getType().isAssignableFrom(WebElement.class)) { WebElement element = WebElementUtils.findElementLazily(by, localSearchContext); setValue(field, target, element); // List<WebElement> } else if (field.getType().isAssignableFrom(List.class) && getListType(field).isAssignableFrom(WebElement.class)) { List<WebElement> elements = WebElementUtils.findElementsLazily(by, localSearchContext); setValue(field, target, elements); } } } catch (ClassNotFoundException e) { throw new IllegalStateException(e.getMessage(), e); } }