Run selenium UI tests in Docker container

A few months ago Atlassian announced the end of life of the Bamboo Cloud continuous delivery offering that we have been using for our project Flow. A new solution called Bitbucket pipelines has been introduced as the replacement of Bamboo. It is built within Bitbucket which makes it really close to the source code and it uses Docker containers to run builds. You can use all images hosted on Docker Hub as your build environment or build your own.

One of the problem that we ran into with this change is that we need to run our selenium UI tests inside of a Docker container. I think this case would become more and more common so I’d like to share in this post how to do that.

The major problem of a Docker container for UI testing is that it does not have a screen output. There are in general two solutions: use a headless browser such as HTMLUnit that does not require a graphical user interface or simulate a screen output.

I choose the second one because in this case I do not need to change my testing code to use a WebDriver for a headless browser. Moreover, headless browser may not have full functionalities of a real browser. For example, the web application that we test uses WebSocket which is not supported by HTMLUnit.

What I need is a display server called Xvfb or X virtual frame buffer. It performs all graphical operations in memory without showing any screen output.

So first I create a Docker image with a Dockerfile and install firefox and Xvfb on it.

Dockerfile to install firefox and xvfb

Once the image is built, I can run this image and verify that firefox is installed at /usr/bin/firefox.

$ docker run -it firefox-image
$ firefox -v
Mozilla Firefox 45.0

But if I try to run firefox I will get an error:

$ firefox
Error: cannot open display: :99

This is because there is no screen output. You will get the same error if you try to run the UI tests directly in this Docker container. To solve this issue, we need to start the Xvfb display server before running the UI tests. You can find the command and options by referring to the Xvfb manual :

$ Xvfb :99 -ac &

This command runs a virtual display server as a background process. It will listen for connections as server number 99, which is required by firefox. The -ac flag disabled the access control to the display. Now you can run your UI tests with maven or whatever command you use.