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());
}