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?)
Dependencies
Since Selenium has not yet been published to mirrors, we need to add the following repository information to <repositories> in pom.xml:
<repository> <id>openqa</id> <name>OpenQA Repository</name> <url>http://maven.openqa.org</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository>
We also need to add the following dependency to pom.xml in the <dependencies> tag:
<dependency> <groupId>org.openqa.selenium.client-drivers</groupId> <artifactId>selenium-java-client-driver</artifactId> <version>0.9.0</version> <scope>test</scope> </dependency>
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; @Override protected void setUp() throws Exception { super.setUp(); // insert data in database // configure the selenium client selenium = new DefaultSelenium("localhost", 4444, "*firefox", HOME_URL); // launch the browser window selenium.start(); } @Override protected void tearDown() throws Exception { // close the browser window selenium.stop(); super.tearDown(); } public void testShouldPass() throws Exception { selenium.open(HOME_URL); // 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 selenium.mouseOver("SOME_DIV_ID"); // 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:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>selenium-maven-plugin</artifactId> <version>1.0-beta-2-SNAPSHOT</version> <executions> <execution> <id>start-selenium</id> <phase>pre-integration-test</phase> <goals> <goal>start-server</goal> </goals> <configuration> <background>true</background> </configuration> </execution> </executions> </plugin>
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):
<pluginRepositories> <pluginRepository> <id>mojo-snapshots</id> <name>codehause mojo snapshots</name> <layout>default</layout> <url>http://snapshots.repository.codehaus.org</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories>
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)
- see also Deploy to JBoss using Cargo
- see also Deploy to Tomcat using Cargo
- start the Selenium RC
- run your integration tests
- stop the container
About The Author: Andrea Ramirez
More posts by Andrea Ramirez