Thursday, 28 February 2008


This release has been superseded - Please see for the latest release information.

I'm a big fan of Selenium - it lets me test my web applications easily, quickly, and inside the browser. Now I've created a Grails plugin to bring the efficiency of Selenium to Grails projects easily.

When you install this plugin, it will download the Selenium-Core distribution (currently version 0.8.3 - 1.5MB) and extract it into your grails application (into <grails project>/web-app/selenium/core). Now you simply create your Selenium tests - putting them in <grails project>/web-app/selenium/tests.

This plugin will automatically generate your test suite by listing all of the files in the selenium/tests directory. You can create tests as standard HTML as per the selenium documentation (and easily created using SeleniumIDE). Or, you can for-go the HTML for pipe delimited text files (*.psv) - this plugin will convert the pipe delimited files on the fly to HTML.

Once you have your tests in place, you can run them using the Selenium Test Runner - at http://localhost:8080/<CONTEXT_PATH>/selenium/core/TestRunner.html?test=..%2F..%2Fselenium/suite.
NOTE: Using Selenium-core means that selenium and your tests are part of your web application - if you do not want your tests packaged in your WAR when using 'grails war' you must add this to your applications Config.groovy:
grails.war.resources = {stagingDir ->
delete(dir: "$stagingDir/selenium")

This will remove the 'selenium' directory from the war - removing selenium-core and your tests. The SeleniumController class files are still included in your war - I haven't found a cleaner way to completely remove the plugin yet.

To test-drive this plugin, you can follow this procedure (here is a shell script which does the same):
grails create-app selenium-test
cd selenium-test
grails install-plugin
grails create-domain-class book
grails create-controller book

Now, modify the domain class and controller:
class Book {
String title
static constraints = {

class BookController {
def scaffold = Book

Now create the tests:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<table cellpadding="1" cellspacing="1" border="1">
<tr><td colspan="3">test1</td></tr>
<tr><td>clickAndWait</td><td>link=New Book</td><td></td></tr>
<tr> <td>clickAndWait</td> <td>//input[@value='Create']</td> <td></td></tr>
<tr> <td>clickAndWait</td> <td>link=Book List</td> <td></td></tr>
<tr> <td>verifyTextPresent</td> <td>book1</td> <td></td></tr>

or alternatively as pipe delimited /web-app/selenium/tests/test2.psv:

clickAndWait|link=New Book
clickAndWait|link=Book List

Now run your application with grails run-app and point your browser to http://localhost:8080/selenium-test/selenium/core/TestRunner.html?test=..%2F..%2Fselenium/suite

At the top left, you'll see your test suite - on the right, you'll see the 'execute tests' where you can run the entire suite or just the selected test.

For more on Selenium, see:


Post a Comment