Skip to content

Wakaleo Consulting

  Home Blog A quick primer on testing Selenium in Groovy
  • Training and Mentoring in Test Driven Development
  • Training and Mentoring in Test Driven Development
  • Expertise in Continuous Integration and Continuous Deployment
  • Training and Mentoring in Test Driven Development
  • Expert Jenkins/Hudson training and mentoring
  • Expertise in Automated Acceptance Tests and ATDD
  • Expertise in Continuous Integration and Continuous Delivery
  • Training and Mentoring in Test Driven Development
  • Expertise in quality development and testing practices
A quick primer on testing Selenium in Groovy PDF Print E-mail
Thursday, 28 May 2009 12:00

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:


<plugin>
  <groupId>org.codehaus.groovy.maven</groupId>
  <artifactId>gmaven-plugin</artifactId>
  <version>1.0-rc-4</version>
  <executions>
    <execution>
      <goals>
        <goal>generateStubs</goal>
        <goal>compile</goal>
        <goal>generateTestStubs</goal>
        <goal>testCompile</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Next, you need to set up Selenium in your Maven project. You can use the Selenium plugin for this:


<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>selenium-maven-plugin</artifactId>
  <version>1.0-rc-1</version>
  <executions>
    <execution>
      <id>xvfb</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>xvfb</goal>
      </goals>
    </execution>
    <execution>
      <id>start</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>start-server</goal>
      </goals>
      <configuration>
        <background>true</background>
        <logOutput>true</logOutput>
      </configuration>
    </execution>
    <execution>
      <id>stop</id>
      <phase>post-integration-test</phase>
      <goals>
        <goal>stop-server</goal>
      </goals>
    </execution>
  </executions>
</plugin>>

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:


<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <excludes>
      <exclude>**/*IntegrationTest.*</exclude>
      <exclude>**/selenium/**</exclude>
    </excludes>
    <forkMode>never</forkMode>
  </configuration>
  <executions>
    <execution>
      <id>integration-tests</id>
      <phase>integration-test</phase>
      <goals>
        <goal>test</goal>
      </goals>
      <configuration>
        <excludes>
          <exclude>**/*Base.java</exclude>
          <exclude>**/*$*</exclude>
        </excludes>
        <includes>
          <include>**/*IntegrationTest.*</include>
          <include>**/selenium/**</include>
        </includes>
      </configuration>
    </execution>
  </executions>
</plugin>

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:


<dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium.client-drivers</groupId>
      <artifactId>selenium-java-client-driver</artifactId>
      <version>1.0-beta-2</version>
      <scope>test</scope>
    </dependency>
</dependencies>

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:


public class SeleniumTestBase {

    /**
     * Only create one instance of this server for all the tests.
     */
    private static Selenium seleniumInstance

    /**
     * Base URL defined by the ${base.url} property.
     * Child classes should use this variable to ensure that tests are run
     * against the right test server.
     */
    private static String baseUrl;

    protected static synchronized Selenium getSelenium() {
        
        if (!seleniumInstance) {
            seleniumInstance = new DefaultSelenium("localhost", 4444,
                                                   "*firefox", getBaseUrl())
            seleniumInstance.start()
        }
        return seleniumInstance
    }

    public static String getBaseUrl() {
        if (!baseUrl) {
            baseUrl = System.getProperty("base.url") ?: ""
        }
        return baseUrl
    }

    @AfterClass
    public static synchronized void shutdown() { 
    	seleniumInstance.close()
    	seleniumInstance = null
    }    
}

Finally, the tests themselves are derived from this class, and use the selenium and baseUrl public class VersionNumberTest extends SeleniumTestBase { @Test void homePageShouldHaveAVersionNumberInFooter() { selenium.with { open "${baseUrl}/index.html" waitForPageToLoad "5000" assert isTextPresent(/regexp:myapp version \d+\.\d+\.\d+/) } } ... }

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 - 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.

Tags See All Tags Add New Tag...

Please Enter New Tags Separated By Comma's
  Or Close

Groovy  Selenium  Testing 

Trackback(0)
Comments (0)Add Comment

Write comment

busy