A quick primer on testing Selenium in Groovy

Selenium is a widely-used and very useful tool for automated web testing. This article is a very short primer designed to get you up and running Selenium Tests in Groovy in your Maven projects.

The examples assume you are running your Selenium tests against an existing web server (say an integration or staging server). In this case, I typically set up a dedicated Maven project to do the job. This project will start up a Selenium client, run some Selenium integration tests, and then shut down the Selenium client. An alternative approach is to start your application up in Jetty, which works fine as well, but involves a bit more configuration in the pom.xml file.

The test cases are written in Groovy, primarily because it is a real pleasure to write anything in Groovy :-). Easyb is also an excellent alternative that works well with Selenium. The Groovy tests by default go in the test/src/groovy directory. You can set up Groovy in your Maven project by adding the gmaven plugin, as shown here:

Note that we have set up the Selenium client to start just before the integration tests, and to close down once the integration tests are finished. We also need to configure the Surefire plugin to run the Selenium tests during the integration-test phase. You can use whatever convention you want - in this case, the Selenium tests all go in a package called 'selenium', placed somewhere in the project class structure. In the following configuration, we ensure that these tests are only run during the integration-test phase:

To make things a bit more flexible, we will be passing in the target URLs to the tests from the command line or via a Maven profile. Now, due to a bug/quirk in the current Maven surefire plugin, you need to set the forkMode option to never for this to work properly.

We will also need a dependency on the Selenium Java client classes for our test code later on:

The Groovy classes themselves are fairly simple. We start off with a base class, which will set up the Selenium client, and shut it down cleanly after the tests. We use a property called base.url to ensure that we can run the tests against different servers as required:

That should be enough to get you started with Selenium tests written in Groovy for your Maven projects - have fun!

"Probably the best training course I've been on."..."Not just how to write Java code but the 'business end' - how to build, test, deploy, manage and monitor"..."One of the best and most useful courses I have attended. And they didn't even try to sell me anything!"- Coming soon to London, Wellington, Auckland, Canberra and Sydney[2] - Get up to speed with the latest and coolest in Java tools and best practices! Sign up on the 2009 season of the Java Power Tools Bootcamps.

Your use of this web site or any of its content or software indicates your agreement to be bound by these Terms of Participation.