/** * <p> * Wraps the given target instance in the proxy. * </p> * * <p> * The list of interfaces which should be implemented by the proxy needs to be provided. * </p> * * * @param target the target instance to be wrapped * @param interfaces the list of interfaces which should be implemented by created proxy * @return the proxy wrapping the target */ @SuppressWarnings("unchecked") public static <T> T getProxyForTargetWithInterfaces(GrapheneContext context, T target, Class<?>... interfaces) { GrapheneProxyHandler handler = GrapheneContextualHandler.forTarget(context, target); return (T) createProxy(handler, null, interfaces); }
/** * <p> * Wraps the given target instance in the proxy. * </p> * * <p> * The list of interfaces which should be implemented by the proxy needs to be provided. * </p> * * * @param target the target instance to be wrapped * @param interfaces the list of interfaces which should be implemented by created proxy * @return the proxy wrapping the target */ @SuppressWarnings("unchecked") public static <T> T getProxyForTargetWithInterfaces(GrapheneContext context, T target, Class<?>... interfaces) { GrapheneProxyHandler handler = GrapheneContextualHandler.forTarget(context, target); return (T) createProxy(handler, null, interfaces); }
/** * <p> * Wraps the given target instance in the proxy. * </p> * * <p> * The list of interfaces which should be implemented by the proxy is automatically computer from provided instance. * </p> * * * @param target the target instance to be wrapped * @return the proxy wrapping the target */ @SuppressWarnings("unchecked") public static <T> T getProxyForTarget(GrapheneContext context, T target) { if (Modifier.isFinal(target.getClass().getModifiers())) { if (target.getClass().getInterfaces().length > 0) { return GrapheneProxy.getProxyForTargetWithInterfaces(context, target, target.getClass().getInterfaces()); } else { throw new IllegalStateException("Can't create a proxy for " + target.getClass() + ", it's final and id doesn't implement any interface."); } } GrapheneProxyHandler handler = GrapheneContextualHandler.forTarget(context, target); return (T) createProxy(handler, target.getClass()); }
/** * <p> * Wraps the given target instance in the proxy. * </p> * * <p> * The list of interfaces which should be implemented by the proxy is automatically computer from provided instance. * </p> * * * @param target the target instance to be wrapped * @return the proxy wrapping the target */ @SuppressWarnings("unchecked") public static <T> T getProxyForTarget(GrapheneContext context, T target) { if (Modifier.isFinal(target.getClass().getModifiers())) { if (target.getClass().getInterfaces().length > 0) { return GrapheneProxy.getProxyForTargetWithInterfaces(context, target, target.getClass().getInterfaces()); } else { throw new IllegalStateException("Can't create a proxy for " + target.getClass() + ", it's final and id doesn't implement any interface."); } } GrapheneProxyHandler handler = GrapheneContextualHandler.forTarget(context, target); return (T) createProxy(handler, target.getClass()); }
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; }
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; }
@Before public void prepare() { handler = GrapheneContextualHandler.forTarget(GrapheneContext.getContextFor(Default.class), null); }
private WebElement getProxified() { // right now browser.switchTo().activeElement() is an example of a proxified but not wrapped element // we won't rely on that though, because it could easily change, so instead we'll use the proxifier directly (and in // such a way so as to benefit from the fix relevant to the test) GrapheneContextualHandler handler = GrapheneContextualHandler.forTarget(GrapheneContext.lastContext(), browser); WebElement proxy; try { Method findElement = WebDriver.class.getMethod("findElement", By.class); proxy = (WebElement) handler.invoke(browser, findElement, new Object[] { By.id(TEST_ELEMENT_ID) }); } catch (Throwable e) { throw new RuntimeException(e); } // in case you need to reproduce the problem, create the proxy manually: // Class<?>[] interfaces = GrapheneProxyUtil.getInterfaces(getUnwrapped().getClass()); // WebElement proxy = GrapheneProxy.getProxyForTargetWithInterfaces(GrapheneContext.lastContext(), getUnwrapped(), // interfaces); if (!(proxy instanceof GrapheneProxyInstance) || proxy instanceof GrapheneElement) { // it's not right to use asserts here - not the point of the test // also a caller might use @Test(expected = AssertionError.class) throw new RuntimeException( "Not proxified correctly: " + Arrays.toString(GrapheneProxyUtil.getInterfaces(proxy.getClass()))); } return proxy; }
T proxy = GrapheneProxy.getProxyForHandler(GrapheneContextualHandler.forTarget(grapheneContext, pageFragment), clazz);
T proxy = GrapheneProxy.getProxyForHandler(GrapheneContextualHandler.forTarget(grapheneContext, pageFragment), clazz);