Integration Testing in a Maven Environment using Selenium

The following is a guide to get Selenium set up and running in a Maven environment. This is assuming that you are running a project that deploys to a web container (otherwise, what are you using Selenium for?)


Since Selenium has not yet been published to mirrors, we need to add the following repository information to <repositories> in pom.xml:

    <name>OpenQA Repository</name>

We also need to add the following dependency to pom.xml in the <dependencies> tag:


Test Code

The following is a snippet of code from the project that spawned this Maven/Selenium integration. We are making an AJAX call on page load, and need to wait for a certain element to appear on the page. The stripped down version of the test is:

package ca.intelliware.ontrace.pages;

import ca.intelliware.ontrace.AbstractBaseIntegrationTest;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class PremisesMapIntegrationTest extends AbstractBaseIntegrationTest {

    private static final String DEFAULT_WAIT_PERIOD = "12000";
    private static final String HOME_URL = "http://localhost:8080/web/app";
    private Selenium selenium;

    protected void setUp() throws Exception {
        // insert data in database

        // configure the selenium client
        selenium = new DefaultSelenium("localhost", 4444, "*firefox", HOME_URL);
        // launch the browser window

    protected void tearDown() throws Exception {
        // close the browser window

    public void testShouldPass() throws Exception {;
        // the waitforcondition polls for the DEFAULT_WAIT_PERIOD time (in millis), looking for a div with the id of
        // 'SOME_DIV_ID' - this is how we know when the AJAX call is complete
        selenium.waitForCondition("selenium.isElementPresent(\"xpath=//div[@id='SOME_DIV_ID']\");", DEFAULT_WAIT_PERIOD);
        // mouse over the div
        // get the text from the div and assert on the value
        assertEquals("SOME_DIV_ID_TEXT_CONTENT", selenium.getText(xpath=//div[@id='SOME_DIV_ID']));

Getting the Selenium RC to Start Automatically when Running Tests

We are already using cargo to fire up our container during the pre-integration-test phase, and shut it down during the post-integration-test phase, but we need the Selenium RC to fire up during the pre-integration-test phase as well. Directly after the entry for the cargo-maven1-plugin, we add the following:


The reason for using this SNAPSHOT version is that the 1.0-beta-2 version uses an older version of the selenium-server, which is lacking several useful commands. You may need to add the following to your <pluginRepositories> pom.xml entry (as in Dependencies above):

        <name>codehause mojo snapshots</name>

Running the Integration Tests

You can now run the following command

mvn clean package verify

which will do the following:

  • clean the target directory
  • run the unit tests for your project
  • package up the war
  • start the container (see note above about the cargo-maven2-plugin)
  • start the Selenium RC
  • run your integration tests
  • stop the container

It's only fair to share...
Share on Facebook
Tweet about this on Twitter
Share on LinkedIn

Leave a Reply