Testing Grails applications with Geb and JsTestDriver on a headless Bamboo CI server

In our project we are using two testing tools – Geb and JsTestDriver. Geb is a tool for functional testing of web applications and is basically a layer on the top of Selenium’s WebDriver so that the test you write are more groovy and more fun to write. JsTestDriver on the other hand is a tool for unit testing javascript in your project. Together with Sinon.JS which is a javascript mocking framework they give you an easy solution to get your javascript code unit tested.

Both of the aforementioned frameworks need real browsers to run. This is good on one side, because you are actually testing your application in real environment in which it will be used. On the other hand it is nasty if your continuous integration server is headless. You might ask how to run a browser if there is no window management / graphical environment installed. Fortunatelly there is a solution – Xvfb which provides a virtual screen. So now, all you need is to install it, make sure that it is started during booting and configure the testing tools to use a virtual screen instead of a real one.

Xvfb is quite popular and is probably packaged for most of the mainstream Linux distros – on Ubuntu you can easily install it using apt-get. Unfortunately it doesn’t come with service scripts nor is started during booting by default. After googling for a while I was able to find the folowing startup script for Xvfb which I put into ‘/etc/init.d/xvfbd':

#!/bin/bash 
# 
# Author: Brian Connolly (LabKey.org) 
# 
# chkconfig: 345 98 90 
# description: Starts Virtual Framebuffer process to enable the  
# LabKey server to use R. 
# 
#

XVFB_OUTPUT=/tmp/Xvfb.out
XVFB=/usr/bin/Xvfb
XVFB_OPTIONS=":99 -ac"

start() {
        echo -n "Starting : X Virtual Frame Buffer "            
        $XVFB $XVFB_OPTIONS >>$XVFB_OUTPUT 2>&1&
        RETVAL=$?
        echo            
        return $RETVAL
}

stop() {
        echo -n "Shutting down : X Virtual Frame Buffer"            
        echo            
        killall Xvfb
        return 0
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        *)
                echo "Usage: xvfbd {start|stop}"            
                exit 1
                ;;

esac
exit $?

As you can see it is extremely simple as it biggest objective is to get the Xvfb running during booting. The approach to stopping is maybe not the cleanest one but my bash skills aren’t the best. Nevertheless note that in XVFB_OPTIONS variable we’re saying that we want Xfvb to create a screen number 99 for us. It’s important as we will use that value later on. Having a service script we now just have to link it from ‘/etc/rc*.d’ directories so it’s picked up during startup and shutdown of the server.

If you use Geb for testing a Grails application then running the tests is a piece of cake – all you have to do is call: ‘grails test-app functional:’. This is not the case for JsTestDriver tests which are executed using a standalone jar which is not integrated with Grails what so ever. As running the JsTestDriver involves setting a lot of switches and one of the parameters is actually the path to your browser I decided to create a bash script for it and then wrap it into a grails script so that it’s easier to run from Bamboo. That’s the source to the bash script which lands in ‘test/js’ subdirectory of your Grails application together with the JsTestDriver.jar and jsTestDriver.conf files:

#!/bin/bash
FIREFOX=`which firefox`
if [ "$?" -eq 1 ];
then
  echo "Firefox not found."
  exit 1
fi

# run the tests
java -jar JsTestDriver-1.3.1.jar --port 9876 --config jsTestDriver.conf --browser $FIREFOX --tests all --testOutput "../../target/js-test-reports"
RESULT=$?

exit $RESULT

And here comes the really simple ‘RunJsTests.groovy’ script:

target('default': 'Runs JsTestDriver tests') {
    ant.mkdir(dir: 'target/js-test-reports')
    ant.exec(executable: './run.sh', dir: 'test/js', failonerror: true)
}

Finally the Bamboo configuration. Apart from running the tests the most important part is to set the ‘DISPLAY’ environment variable in your Bamboo Grails task to ‘:99′. This will tell the browsers that are started to use the display number 99 which is handled by Xvfb. The following screenshot shows where you can set the environment variables in your Bamboo Grails build task:
Setting Bamboo for testing with Xfvb

0 Comments

Trackbacks/Pingbacks

  1. An Army of Solipsists » Blog Archive » This Week in Grails (2011-34) - [...] Testing Grails applications with Geb and JsTestDriver on a headless Bamboo CI server [...]