Use EventFiringWebDriver with Arquillian Drone


#1

Hello guys. I’m using Arquillian Graphene to automate web application. My issue is I cannot capture correct image at the correct time when test failed so I think I should use EventFiringWebDriver to take a screen shot when I meet exception. Things like this:

   EventFiringWebDriver driver = new EventFiringWebDriver(new InternetExplorerDriver());
   WebDriverEventListener errorListener = new AbstractWebDriverEventListener() {
       @Override
       public void onException(Throwable throwable, WebDriver driver) {
           takeScreenshot("some name");
       }
   };
   driver.register(errorListener);

Could you guys help me to inject those coded to Drone? Or how can I do similar things with drone?


#2

Which version of Drone are you using? In our project we are still on 1.3.1.Final, but I believe with lately released 2.0.0 this hasn’t changed a lot.

In case of Drone it should be done through SPI. You should create tiny little Arquillian Extension which will plumb your logic. I personally would do it as a separated jar.

I would like to share this knowledge here instead of simply pointing you to already existing extension, as it might help you do “similar things” you asked about :wink:

WebDriver Listener (same same)

public class TakeScreenshotOnError implements AbstractWebDriverEventListener {
       @Override
       public void onException(Throwable throwable, WebDriver driver) {
           takeScreenshot("some name");
       }
}

Drone Listener

public class ScreenshotOnErrorListener implements DroneInstanceEnhancer<WebDriver> {

    protected WebDriverEventListener listener;

    @Override
    protected WebDriverEventListener createListenerInstance() {
        return new TakeScreenshotOnError();
    }

    @Override
    public int getPrecedence() {
        return 1;
    }

    @Override
    public boolean canEnhance(InstanceOrCallableInstance instance, Class<?> droneType, Class<? extends Annotation> qualifier) {
        return droneType.isAssignableFrom(EventFiringWebDriver.class);
    }

    @Override
    public WebDriver enhance(WebDriver instance, Class<? extends Annotation> qualifier) {
        final EventFiringWebDriver driver = new EventFiringWebDriver(instance);
        listener = createListenerInstance();
        driver.register(listener);
        return driver;
    }

    @Override
    public WebDriver deenhance(final WebDriver enhancedInstance, final Class<? extends Annotation> qualifier) {
        if (EventFiringWebDriver.class.isInstance(enhancedInstance)) {
            final EventFiringWebDriver driver = (EventFiringWebDriver) enhancedInstance;
            driver.unregister(listener);
            listener = null;
            return driver.getWrappedDriver();
        }
        return enhancedInstance;
    }

}

Wiring Drone listener through Arquillian Extension mechanism

public class DroneExtensions implements LoadableExtension {

    @Override
    public void register(final ExtensionBuilder extensionBuilder) {
        extensionBuilder
                .service(DroneInstanceEnhancer.class, ScreenshotOnErrorListener.class);
    }

}

Registering extension

Last but not least you have to register your Arquillian Extension through standard Java SPI mechanism. In src/main/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension put your fully qualified class name

com.acme.DroneExtensions

Hope that helps :slight_smile:


#3

Hi bartoszmajsak,

Thanks a lot :slight_smile: