Wednesday, October 6, 2010

Grails EasyB and Selenium

Hi there,

having EasyB in your grails project is handy. There's no question about it. However combining it with Selenium RC adds a whole new flavor to the mixture!

Let's examine a step-by-step scenario on how to include both grails-easyb and selenium-rc in a brand new application, then let's create a simple home controller with one action and let's test it using the potent mix of behaviors and remote test execution :)

Step 1: Create a new application

This one is extremely easy:
grails create-app example
Step 2: Install the grails-easyb plugin.

This one is extremely easy to:
grails install-plugin easyb
Step 3: Install selenium rc.

Well, there's 4 things to it.
a) download the Selenium RC package
b) extract the driver (selenium-java-client-driver.jar) into the lib folder of our example application
c) extract the Selenium RC server (the whole folder selenium-server-1.0.3)
d) run Selenium RC server
java -jar selenium-server.jar
Step 4: Create the home controller.

As you might have already guessed:
grails create-controller home
Step 5: Add some meat to the index action.

Let's change the default empty implementation of our index action into
[ message: "Hello, world!" ]
Step 6: Create a functional (yes! functional test at last!) in test/functional (this folder does not exist at first so you have to create it yourself). I'll call it MyFirstFunctional.story
import com.thoughtworks.selenium.*

before "start selenium", {
given "selenium is up and running", {
selenium = new DefaultSelenium(
"localhost", 4444, "*iexplore",

scenario "Will show 'hello, world!' message", {
when "home/index opens", {"home/index")

then "the text 'Hello, world! appears on the page", {
selenium.isTextPresent("Hello, world!")

after "stop selenium", {
then "selenium should be shutdown", {
WARNING: you might naturally want to move the "after" section right below the "before" section. DON'T EVER DO THAT! EasyB test segments are executed in the order they have been declared so if you do that selenium will get stopped before any scenario can be executed!

Step 7: Run the test and see it fail (we have no view just yet)
grails test-app functional:easyb
Step 8: Create the view (/views/home/index.gsp).
Message: ${message}
Step 9: Execute the test and see it pass:
grails test-app functional:easyb

I hope this will cut down the time you spent implementing easyb- and selenium-enabled functional tests clearing any misunderstandings you might have after reading online docs (they suck big time!).

Do I hear "That's a lot of work, man!"??? Well if you're really that lazy grab the ready-to-run package here and enjoy the rest of the evening :)

Have fun!

1 comment:

MyOpenDraft said...

Nice post, thanks for sharing.