Run UI tests in the background on Linux

Run UI tests in the background on Linux

When you run JUnit tests that open windows and dialogs, all these windows pop up on your screen like fireworks, making your desktop unusable for minutes.

If you’re lucky enough to work on Linux, there’s an easy way to get rid of all the flickering. You can simply redirect your UI tests onto a virtual screen. I was surprised how easy that is and thought it’s worth sharing.

All you need is the X virtual framebuffer (Xvfb). Install this tool if you don’t have it yet, e.g. on a Debian-based system, you’d type:

$ sudo apt-get install xvfb

Then start a virtual display with a number of your choice, but greater than zero (I use 99). You can either do that manually:

$ Xvfb :99 &

or automatically at system startup using an init script like this one. There are some command line options to adjust the dimensions and color depth of the virtual screen, but I’m fine with the defaults.

Now you can run your tests with the environment variable DISPLAY set to :99. In Eclipse, you can do this in the Environment tab of your launch configuration:

UI tests on a virtual display

When running a Maven build from the command line, you can simply export this variable before you start maven.

$ export DISPLAY=:99
$ mvn clean install

That’s it. Now your UI test suite runs quietly in the background and let’s you keep working.

I couldn’t find anything similar for Windows or Mac, if you know how to do it, I’m interested to hear about it.

  • Andrew Eidsness
    Posted at 12:29 pm, December 6, 2013

    That is a great idea, I can’t believe I never thought of it. Thanks for posting!

  • Lorenzo
    Posted at 7:18 pm, December 6, 2013

    A similar procedure is detailed on swtbot wiki, but for that kind of tests you also need a window manager as far as I know, and xephyr.

    And of course Linux rocks 🙂

  • Lars Vogel
    Posted at 2:47 pm, December 17, 2013