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:

Wednesday, 27 February 2008

Installing Grails on Linux

I run Linux (Ubuntu 7.10) as my desktop operating system, and it's working out well. However, every time I upgrade Grails I forget to make the script in $GRAILS_HOME/bin executable and get this error:
prule@dev2:~/workspace/film-suggestions-grails$ grails clean
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/groovy/grails/cli/support/GrailsStarter

So, if you find yourself with this cryptic message, it is time to RTFM - the installation instructions actually point out:
If you get an error message, try to chmod +x the grails script inside the bin directory.

When I googled this exception I got unrelated stuff. Hopefully now if you search for this you'll find this post and be up and running fast.

Note:  I experience this problem because I always download the zip distribution. If you get the tar.gz binary, extracting this will preserve the correct file permissions.

Friday, 22 February 2008

Linux VPS - RimuHosting.com

Looking to host an application on the web? The most flexible solution is to have a virtual server - JavaThinking.com and FilmSuggestions.com are hosted on a Linux VPS from RimuHosting.com. This gives me root access to my very own Linux installation where I can install anything I want and configure it any way I want.

I can recommend RimuHosting (although they are the only provider I've tried) - they have been very helpful and quick to respond to questions. Their services seem complete and appropriately priced, and the service is excellent.

They've got very useful information about Linux and Java hosting on their Bliki and in their HOWTOs.

Our VPS Hosting By RimuHosting
Java and Linux VPS Hosting by RimuHosting