Setting up Selenium server on a headless Jenkins CI build machine
Selenium is a great tool for automating browser based testing of web applications. This post is regarding the installation and configuration of Selenium with the excellent Jenkins Continuous Integration server.
First grab a copy of Selenium server (formerly Selenium RC) from http://seleniumhq.org/download/. You might also want to symlink the versioned file to a standard location.
wget http://selenium.googlecode.com/files/selenium-server-standalone-2.0b2.jar
sudo mkdir /var/lib/selenium
sudo mv selenium-server-standalone-2.0b2.jar /var/lib/selenium
cd /var/lib/selenium
sudo ln -s selenium-server-standalone-2.0b2.jar selenium-server.jar
Assuming at least one of your browser nodes will be running on the local machine, you’ll also need a virtual framebuffer so that a browser can actually load.
On Ubuntu you can:
sudo apt-get install xvfb
The following init.d script might also be useful to help start and stop that service:
#!/bin/bash
if [ -z "$1" ]; then
echo "`basename $0` {start|stop}"
exit
fi
case "$1" in
start)
/usr/bin/Xvfb :99 -ac -screen 0 1024x768x8 &
;;
stop)
killall Xvfb
;;
esac
Save it to /etc/init.d/xvfb and chmod 755, then confirm that it works:
sudo chmod 755 /etc/init.d/xvfb
/etc/init.d/xvfb start
You may receive a few warnings regarding fonts and such, but it’ll still work despite these. If you want the framebuffer to start automatically on system startup you can do:
sudo update-rc.d xvfb defaults 10
If you don’t already have any browsers you might also want to install something like Firefox.
sudo apt-get install firefox
Now to Jenkins, you’ll probably want to install the Selenium HQ plugin. This will allow you to configure a path to your selenium report file so that it can be linked to your projects and Jenkins will draw a graph alongside your other metrics. It also adds a custom build step for configuring Selenium, but i had issues using this due to the need for the above framebuffer.
In your project configuration, add an “Execute shell” build step. In here we’ll export a display and run Selenium as follows:
export DISPLAY=":99" && java -jar /var/lib/selenium/selenium-server.jar
-browserSessionReuse -htmlSuite *firefox http://path/to/app
/var/lib/jenkins/jobs/project/workspace/path/to/Suite.html
/var/lib/jenkins/jobs/project/workspace/path/to/logs/selenium.html
Finally, under post build actions, tick the box for “Publish Selenium Report” and add the path to your log file from above, relative to your workspace dir.
path/to/logs/selenium.html
And that should be pretty much all you need. Run a build and watch your shiny new Selenium reports roll in.
For reference, if you don’t export the display you’ll probably receive exceptions that look something like:
HTML suite exception seen:
java.lang.RuntimeException: Timed out waiting for profile to be created!
Jenkins’ shell commands run through /bin/sh instead of bash, meaning it won’t look in your standard ~/.bashrc file. Although, if you also want to be able to run Selenium manually then you can always stick the export command in there too.
Hope that works for you. Let me know on Twitter @labelmedia or @samh. Bye for now.