Selenium Headless

November 23, 2010

If you try to run selenium-server on a machine without firefox installed, you'll get the following error when running your tests:

$ python bbr.py
E
======================================================================
ERROR: test_bbr (__main__.bbr)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "bbr.py", line 8, in setUp
    self.selenium.start()
  File "/home/user/Programming/Selenium/bbr/selenium.py", line 189, in start
    result = self.get_string("getNewBrowserSession", [self.browserStartCommand, self.browserURL, self.extensionJs])
  File "/home/user/Programming/Selenium/bbr/selenium.py", line 219, in get_string
    result = self.do_command(verb, args)
  File "/home/user/Programming/Selenium/bbr/selenium.py", line 215, in do_command
    raise Exception, data
Exception: Failed to start new browser session: java.lang.RuntimeException: Firefox 3 could not be found in the path!
Please add the directory containing ''firefox-bin' or 'firefox'' to your PATH environment
variable, or explicitly specify a path to Firefox 3 like this:
*firefox3/blah/blah/firefox-bin

----------------------------------------------------------------------
Ran 1 test in 1.684s

FAILED (errors=1)

Installing a Browser

I want to run this on a machine without X, running Debian. Firefox is not installed, and not available in the repositories. The recommended replacement is iceweasel:

# apt-get install iceweasel

will install it.

You can now run firefox, though it won't start:

$ firefox
Error: no display specified

We can use Xvfb, the X virtual framebuffer. This is a stripped down X server that maintains a virtual display in memory, and doesn't need a real screen. Install it with:

# apt-get install xvfb

We now need to run Xvfb as root:

# Xvfb :99 -ac

99 is the port to use, whilst -ac runs it without access control. (This is probably ok for a private network but beware this on anything else!)

Next, set the DISPLAY environment variable to point to port 99, then run up selenium server:

$ export DISPLAY=:99
$ java -jar selenium-server.jar -firefoxProfileTemplate /home/tjs/.mozilla/firefox/ixtvuw8r.Selenium

Remember if you need to create the firefox profile on a remote headless machine, you can do so using:

$ ssh -X server

This will forward the screen output back to your local computer.

You should now be able to run the tests against the headless server successfully:

$ python bbr.py
.
----------------------------------------------------------------------
Ran 1 test in 49.188s

OK

References