Running Selenium Headless

Our dog food Pulse server, which spends all day building itself, is a headless box. This presented no challenge for the Pulse 1.x series of releases, as our builds are all scripted and don’t require any GUI tools. With Pulse 2.0, however, things changed. As mentioned in my previous post, this new release has a whole new acceptance test suite based on Selenium. The problem is: Selenium runs in a real browser, and that browser requires a display.

Fortunately, the dog food server is also a Linux box. Thus there is no need to add in a full X setup with requisite hardware just to have Selenium running. This is thanks to the magic of Xvfb - the X virtual framebuffer. Basically, this is a stripped back X server that maintains a virtual display in memory. Hence no actual video hardware or driver is needed, and we can keep things simple.

Setting things up is straightforward. First, install Xvfb:

# apt-get install xvfb

on Debian/Ubuntu; or

# yum install xorg-x11-Xvfb

on Fedora/RedHat. Then, choose a display number that is unlikely to ever clash (even if you add a real display later) - something high like 99 should do. Run Xvfb on this display, with access control off1:

# Xvfb :99 -ac

Now you need to ensure that your display is set to 99 before running the Selenium server (which itself launches the browser). The easiest way to do this is to export DISPLAY=:99 into the environment for Selenium. First, make sure things are working from the command line like so:

$ export DISPLAY=:99
$ firefox

Firefox should launch without error, and stay running (until you kill it with Control-C or similar). You won’t see anything of course! If things go well, then you need to modify the way you launch the Selenium server to ensure the DISPLAY is set. There are many ways to do this, in our Pulse setup we actually use a resource as it is a convenient way to modify the build environment.

An alternative which may suit some setups is to use the xvfb-run wrapper to launch your Selenium server. I opted against this as Pulse has resources to modify the environment and this way I do not need to change the actual build scripts at all. However if you are not using Pulse (shame! :) ) then you may want to look into it.


1 If you are worried about access on your network, then using -ac long-term is not a good idea. Once you have things working, I would suggest tightening things up by turning access control on.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • DZone
  • Ma.gnolia
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati

6 Responses to “Running Selenium Headless”

  1. Alan Says:

    Ugh … export DISPLAY=:99 …

    I’ve been messing around for an hour now trying to figure out how to pass a –display=xx option to Firefox from Selenium in order to get the damned thing running headless. Cheers for reminding me of the obvious!

  2. Jason Says:

    Hi Alan,

    I’m glad someone found this post useful :). I was lucky as using the environment was natural in Pulse so I never considered an alternative.

  3. Jim Says:

    So, I was wondering if you’d be able to give me a hand with this.. I have Xvfb running (I think), and firefox-bin now runs on my headless gentoo box (when I run /usr/bin/firefox). However, when I try to run a Selenium script, the RC server errors with:

    java.lang.RuntimeException: File was a script file, not a real executable: /usr/bin/firefox-bin

    and the actual error from the attempted test run is:
    http://pastebin.com/m265d6539

    Any idea where I should get started? It’s definitely some sort of a setup issue - not a code bug (I have the same exact test running just fine on my not-so-headless ubuntu box). So, something with the environment vars? Or finding and moving the firefox binary somewhere else?

  4. Jason Says:

    Hi Jim,

    It looks like the file that Selenium is identifying as your firefox executable (/usr/bin/firefox-bin, or whatever that links to) is not a binary file, but a script. Selenium expects to find the binary file itself, and refuses to launch Firefox from a script. The solution is to find where the actual binary file is (you can probably tell from reading script and seeing what it launches), and point Selenium at it by setting the browser to:

    *firefox /path/to/the/real/binary

    when you create your Selenium instance.

  5. Pam Says:

    Hi, thanks for the post. I’m having problems on Fedora 8 getting firefox to run with Xvfb… I’ve done everything except the pam stuff, because Fedora’s configured differently… anyone else?

  6. Pam Says:

    Okay, what we had to do to get it working was:
    yum update firefox
    yum update xorg-x11-server-Xvfb
    yum update cairo

    Now firefox will work with the Xvfb on Fedora 8.

    We edited /etc/rc.d/rc.local to include:
    Xvfb :99 -ac -noreset &

    In Hudson (our CI server) we added a “shell execute” that did this:
    export LD_LIBRARY_PATH=”/usr/lib/firefox/”; export PATH=”$PATH:/usr/lib/firefox/”; export DISPLAY=:99;
    java -jar /usr/share/selenium-rc/selenium-server/selenium-server.jar -htmlSuite “*firefox /usr/lib/firefox/firefox-bin” “http://test-server/” “/var/www/html/hs/tests/TestSuite.html” “/var/www/html/hs/tests/selenium-results.html”

    Thanks for the good website.

    Also for anyone else trying to do this on Fedora 8, you don’t have to do the PAM stuff, and probably shouldn’t because it’s different.

Leave a Reply