Home > Java, Web > grails-selenium-0.1

grails-selenium-0.1

by paul on February 27, 2008

This release has been superseded – Please see http://grails.codehaus.org/Selenium+plugin 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 http://www.javathinking.com/grails/grails-selenium-plugin/0.1/grails-selenium-0.1.zip
grails create-domain-class book
grails create-controller book

Now, modify the domain class and controller:

class Book {
String title
static constraints = {
title(unique:true)
}
}

class BookController {
def scaffold = Book
}

Now create the tests:

/web-app/selenium/tests/test1.html:

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>test1</title>
</head>
<body>
<table cellpadding=”1″ cellspacing=”1″ border=”1″>
<thead>
<tr><td colspan=”3″>test1</td></tr>
</thead><tbody>
<tr><td>open</td><td>/selenium-test/</td><td></td></tr>
<tr><td>clickAndWait</td><td>link=BookController</td><td></td></tr>
<tr><td>clickAndWait</td><td>link=New Book</td><td></td></tr>
<tr><td>type</td><td>title</td><td>book1</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>
</tbody></table>
</body>
</html>

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

open|/selenium-test
clickAndWait|link=BookController
clickAndWait|link=New Book
type|title|book2
clickAndWait|//input[@value='Create']
clickAndWait|link=Book List
verifyTextPresent|book2

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:

{ 10 comments… read them below or add one }

Mike February 28, 2008 at 1:23 am

This is fantastic, Paul! Have you considered being able to create test suites based on subdirectories in the ’selenium/tests/’ directory? For example, given ’selenium/tests/book’ and ’selenium/tests/author’, the plugin would create a ‘Book’ test suite, an ‘Author’ test suite, and then a ‘AllTests’ suite that included both Book and Author. I currently use this type of structure so I can run just a subset of tests while I’m working on a particular area, then run the entire suite as a regression when I’m done.

If you’re interested in contributions to the plugin I could help out with trying to get that to work. Thanks for the plugin!

Andres Almiray February 28, 2008 at 6:35 am

Good! wouldn’t it be great to have a Selenium-DSL on GSPs that generate the test html for you?

paul February 28, 2008 at 11:28 am

Mike, subdirectories are definitely on the list of things to do. I just thought I’d release early. I’ll get it done tomorrow. Glad you like it!

paul February 28, 2008 at 11:29 am

Andres, sounds interesting, but would it make it any easier than the pipe delimited text files?

Marcos Silva Pereira February 28, 2008 at 1:38 pm

Fantastic. I was thinking about create a selenium plugin. Nice to have that I not the only one. Very, very congrats, Paul.

Kind Regards

Andres Almiray February 29, 2008 at 6:24 pm

Paul, I wasn’t aware of the pipe delimited format, which is a concise way of looking the testing scenario. I like it even it doesn’t read like a dsl.

paul February 29, 2008 at 7:43 pm

Andres, the pipe delimited format is my own contribution – I’ve never been a fan of writing all that verbose html when its not really necessary – its just presentation detail.

I think I need a way to add variables into the equation though. Perhaps this is where a DSL would shine? Currently I am going to see if simply using a setup page to produce the runtime values – which get stored in javascript variables with the storeXXX commands – is enough.

paul March 1, 2008 at 2:32 am

I’ve released 0.2 which supports directories – see http://www.javathinking.com/?p=66

Kris March 13, 2008 at 4:31 am

Unless I am using it wrong, I think support for .html files in directories is broken in 0.2. I added a directory /user under /tests/, then added a .psv file and it loaded that fine. When i added a .html file it tried to load it under /user\test.html (wrong slash.)

paul March 13, 2008 at 7:05 pm

Thanks for reporting that – its a windows problem. Apologies for not testing on windows, I have limited time available.

This issue will be fixed in 0.3 out soon!

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post: