Losing your Head with Xvfb


The Problem

When we just started with UI Automation using Selenium the tests were only running on a laptop specifically dedicated to UI Automation. As part of our continuous integration environment the total execution time was reached after some time. On extending the amount of servers on which the tests could run we had to realize it wasn’t an easy task since there is no display output for the browser to launch in.

The Solution

In order to run the tests on the CI servers it is needed to configure the tests to launch the browser virtually (e.g. using something like Xvfb). Short for x virtual framebuffer, Xvfb can run on machines with no display hardware and no physical input devices. It emulates a dumb framebuffer using virtual memory.

For some versions X11 was included in OS X, however this is no longer the case. But no worries, there is an alternative to it called XQuartz. Once installed a virtual display is simply created by:

Xvfb :1337 & export DISPLAY=:1337

This command starts Xvfb on the specific display port 1337, backgrounds the process and tells the terminal session to use this display port.

An alternative solution could be running Xvfb as part of the Jenkins job with a plugin.


Once in a while you might run into the following error:

_XSERVTransmkdir: ERROR: euid != 0,directory /tmp/.X11-unix will not be created.
_XSERVTransSocketUNIXCreateListener: mkdir(/tmp/.X11-unix) failed, errno = 2
_XSERVTransMakeAllCOTSServerListeners: failed to create listener for local
(EE) Fatal server error:
(EE) Cannot establish any listening sockets - Make sure an X server isn't already running(EE)

To solve it just a few commands are needed, it’s just a workaround but worked every time so far:

mkdir /tmp/.X11-unix
sudo chmod 1777/tmp/.X11-unix
sudo chown root /tmp/.X11-unix/
It can also happen that Xvfb crashed but still blocking the display port. It is then not possible to restart Xvfb on the same display port:
Fatal server error:
Server is already active for display 1337
    If this server is no longer running, remove /tmp/.X1337-lock
    and start again.
As suggested from the message the lock file needs to be removed:
rm /tmp/.X1337-lock
Afterwards it is possible to start Xvfb again on display port 1337.

Happy Testing!