Lately I have been working on developing UI Integration tests for my JEE6 DiveLog project and was having some trouble getting Arquillian Persistence and Arquillian Drone to work together. After spending a few days trying to sort it out, I created a JIRA and with the help of the Arquillian team, I was finally able to get it working.

The Use Case

The immediate use case I was testing was whether or not a user could sign in via the login page. I also needed to test whether or not the correct page elements were displayed and/or hidden after the user logged in. This required that a user already exist in the database.

The Problem

The problem I was having was that the Persistence extension was not executing when the Drone tests ran. This resulted in the user not being created in the database and the test ultimately failing as a result of the user not being found. Behind the scenes, the Persistence extension is not configured to work with tests that are baing run in Client Mode, so when the deployment was set to testable=false the persistence extension never kicked in.

The Solution

The solution is to "trick" the persistence extension into running prior to the client tests running. This is done with the @InSequence(X) annotation on your tests and by marking the deployment as testable=true. Once this is done you will need to create an empty method that does nothing and set it as the first test in the sequence. This test will be run in In Container mode and will cause the persistence extension to execute and setup your database. All of your remaining client tests should be annotated with @RunAsClient and set to come after the database setup method using @InSequence(X). Below is the working code sample:

@RunWith(Arquillian.class)
@DataSource("java:jboss/datasources/ExampleDS")
@UsingDataSet("addUserDataset.yml")
public class DroneTestIT {
    @Deployment(testable = true)
    public static WebArchive createDeployment() {
        // Create Archive
    }
    @Test
    @InSequence(1)
    public void setupDatabase(){
        // Do nothing, so Persistence will setup database
    }
    @Test
    @InSequence(2)
    @RunAsClient
    public void someUiTest(@ArquillianResource URL applicationPath, @Drone GrapheneSelenium browser) {
        // .. Perform Test
    }
}

The down side to this is that each client test has to be annotated with @RunAsClient so Drone will kick off a new Selenium session for each test, rather than using the same session for all the tests like it would if you were not running with mixed container modes. This results in the tests taking slightly longer to run. Hopefully in future versions of Arquillian this behavior can be cleaned up so that the process is a little more streamlined and these two extensions work together better.



Published

23 August 2012

Tags