JBoss Community Archive (Read Only)

Graphene 2

Graphene Interceptors

Interceptors are not officially part of 2.0 and thus they are not available for production use yet.

Graphene Interceptors concept is inspired by Java EE Interceptors feature. It is a way how to create interceptor classes and methods that interpose on method invocations.

Basic usage

An example of interceptor usage can be a need to take a screenshot of the browser window and its saving after the every method invocation on the injected WebDriver instance. Building an interceptor is simply matter of writing a Java class that implements the org.jboss.arquillian.graphene.proxy.Interceptor interface.

import org.jboss.arquillian.graphene.proxy.Interceptor;
import org.jboss.arquillian.graphene.proxy.InvocationContext;
import org.foo.bar.ScreenshotTaker;

public class ScreenshotTakerInterceptor implements Interceptor {

    @Override
    public Object intercept(final InvocationContext context) {
        ScreenshotTaker.takeScreenshot();
        return context.invoke();
    }
}

Each implementor must satisfy, that in the intercept method body will be called at least once method from current context: InvocationContext#invoke().

Secondly, the interceptor needs to be registered. Currently this can be achieved like in the following listing:

@Drone
WebDriver browser;

public void beforeSomething() {
    GrapheneProxyInstance proxy = (GrapheneProxyInstance) browser;
    proxy.registerInterceptor(new ScreenshotTakerInterceptor());
}

In the example above the interceptor was registered to the WebDriver instance, thus all method invocations on that object will be intercepted. Note that you can register the interceptor to any object which can be casted to GrapheneProxyInstance. For instance to Page Objects.

You can use GrapheneProxyInstance#unregisterInterceptor method to unregister previously registered interceptor.

Have an inspiration in StaleElementInterceptor (here), Graphene way of protecting the elements from StaleElementReferenceException.

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 12:14:40 UTC, last content change 2013-09-11 09:52:37 UTC.