Mark Needham

Thoughts on Software Development

WebDriver: Getting it to play nicely with Xvfb

with 2 comments

Another thing we’ve been doing with WebDriver is having it run with the FirefoxDriver while redirecting the display output into the Xvfb framebuffer so that we can run it on our continuous integration agents which don’t have a display attached.

The first thing we needed to do was set the environment property ‘webdriver.firefox.bin’ to our own script which would point the display to Xvfb before starting Firefox:

import java.lang.System._
lazy val firefoxDriver: FirefoxDriver = {
  setProperty("webdriver.firefox.bin", "/our/awesome/starting-firefox.sh")
  new FirefoxDriver()
}

Our first version of the script looked like this:

/our/awesome/starting-firefox.sh

#!/bin/bash
 
rm -f ~/.mozilla/firefox/*/.parentlock
rm -rf /var/go/.mozilla
 
 
XVFB=`which xVfb`
if [ "$?" -eq 1 ];
then
    echo "Xvfb not found."
    exit 1
fi
 
$XVFB :99 -ac &
 
 
BROWSER=`which firefox`
if [ "$?" -eq 1 ];
then
    echo "Firefox not found."
    exit 1
fi
 
export DISPLAY=:99
$BROWSER &

The mistake we made here was that we started Xvfb in the background which meant that sometimes it hadn’t actually started by the time Firefox tried to connect to the display and we ended up with this error message:

No Protocol specified
Error cannot open display :99

We really wanted to keep Xvfb running regardless of whether the Firefox instances being used by WebDriver were alive or not so we moved the starting of Xvfb out into a separate script which we run as one of the earlier steps in the build.

We also struggled to get the FirefoxDriver to kill itself after each test as calling ‘close’ or ‘quit’ on the driver didn’t seem to kill off the process.

We eventually resorted to putting a ‘pkill firefox’ statement at the start of our firefox starting script:

/our/awesome/starting-firefox.sh

#!/bin/bash
 
rm -f ~/.mozilla/firefox/*/.parentlock
rm -rf /var/go/.mozilla
 
pkill firefox
 
BROWSER=`which firefox`
if [ "$?" -eq 1 ];
then
    echo "Firefox not found."
    exit 1
fi
 
export DISPLAY=:99
$BROWSER &

It’s a bit hacky but it does the job more deterministically than anything else we’ve tried previously.

Be Sociable, Share!

Written by Mark Needham

December 15th, 2011 at 11:19 pm

Posted in Software Development

Tagged with

  • The `xvfb-run` script that ships with most Linux distributions is useful in these situations.

        xvfb-run –auto-servernum my-build-script

    This starts an X display, runs `my-build-script`, then closes the X display. X authentication, exit codes, and the process tree are all taken care of.

    If you’re using Jenkins, there’s a plugin which provides a similar convenience using Xvncserver instead.

    xvfb-run: http://www.manpagez.com/man/1/Xvfb/
    VNC plugin: https://wiki.jenkins-ci.org/display/JENKINS/Xvnc+Plugin

  • Krishnam

    Guys, I am able to run web driver scripts locally in windows desktop. HOwever, the same does not seem tow work fom Jenkins in unix machine and my desktop is in windows