public void enrich(final SearchContext searchContext, Object target) {
List<Field> fields = FindByUtilities.getListOfFieldsAnnotatedWithFindBys(target);
for (Field field : fields) {
try {
if (isValidClass(field.getType())) {
final SearchContext localSearchContext;
GrapheneContext grapheneContext = searchContext == null ? null : ((GrapheneProxyInstance) searchContext)
.getContext();
if (grapheneContext == null) {
grapheneContext = GrapheneContext.getContextFor(ReflectionHelper.getQualifier(field.getAnnotations()));
localSearchContext = grapheneContext.getWebDriver(SearchContext.class);
} else {
localSearchContext = searchContext;
}
final By rootBy = FindByUtilities.getCorrectBy(field, configuration.get()
.getDefaultElementLocatingStrategy());
Object wrapper;
try {
Class<?> type = field.getType();
wrapper = createWrapper(grapheneContext, type,
WebElementUtils.findElementLazily(rootBy, localSearchContext));
} catch (Exception e) {
throw new GrapheneTestEnricherException("Can't instantiate element wrapper " + target.getClass() + "."
+ field.getName() + " of type " + field.getType(), e);
}
try {
setValue(field, target, wrapper);
} catch (Exception e) {
throw new GrapheneTestEnricherException("Can't set a value to the " + target.getClass() + "."
+ field.getName() + ".", e);
}
} else if (field.getType().isAssignableFrom(List.class) && isValidClass(getListType(field))) {
final SearchContext localSearchContext;
GrapheneContext grapheneContext = searchContext == null ? null : ((GrapheneProxyInstance) searchContext)
.getContext();
if (grapheneContext == null) {
grapheneContext = GrapheneContext.getContextFor(ReflectionHelper.getQualifier(field.getAnnotations()));
localSearchContext = grapheneContext.getWebDriver(SearchContext.class);