Thursday, 28 February 2008

grails-selenium-0.1

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:

0 comments:

Post a Comment