Selenium Server without X Window System – xvfb

For my acceptance tests configured in Jenkins I wanted to omit installing of any kind of X Window System. That’s pretty obvious when there is not enough RAM or we have to pay for every megs of it. But how a real browser like Firefox or Chrome would run without X servers? The answer is: X virtual buffer.

Let’s configure Selenium Server on top of xvfb on Ubuntu.

xvfb?

To run acceptance web tests on server you don’t need to install desktop environment like Gnome or XFCE just to launch a browser. Better to use Virtual Framebuffer for running Selenium tests without displaying on real screen.

virtual buffer is a graphics buffer kept in memory. It probably won’t be accelerated by graphics card but we can look into it in real time on another computer.

Install xvfb

Open your terminal and type few things:

Here’s the script that will be auto-started with machine. Default X servers run on DISPLAY number 0, so we’ll avoid future conflict. This runs xvfb on DISPLAY number 1:

Browser

Selenium by default works with Firefox, that’s why I chosed this browser.

But hold your horses. One can’t simply install Firefox. Latest versions don’t work due to problems with dbus-launch. FirefoxDriver built into Selenium is now obsolete so it’s recommended to start using Marionette WebDriver.

The other way is to simply install older version of the browser.

To me, Firefox 40.0.3 seems to work properly, some sources note that 41.0 is the greatest version supporting Selenium. Download some version for your system from Mozilla Firefox builds on SourceForge.

Install it using dkpg:

To make sure that your Ubuntu won’t update Firefox hold the installed version:

Selenium

You need Java installed.

Download “Selenium Standalone Server” from http://www.seleniumhq.org/download/ To simply run it call: export DISPLAY=":1" && ava -jar selenium-server-standalone-2.53.1.jar .

It would be nice to have it launched when operating system starts:

And here’s the script that starts/stops Selenium:

Now, to manually restart Selenium from terminal you simply type:

sudo /etc/init.d/selenium restart

Bonus! See acceptance tests at real time

To achieve this we will use VNC.

Now, you can log in using some VNC client/viewer. For Windows, TightVNC Viewer works well.

The hidden benefit here is that we can look into the console under development tools in the browser.

Summary

Acceptance tests are best to be performed in real browsers. Some poeple use headless browsers like PhantomJS, however this approach increses some risk of seeing false positives or having issues with stopped development of these.

Now, go write your tests with some Selenium Driver in your favorite testing framework and feel happy of automated tests with saved memory.

References

  • Pingback: How to continuously deploy web application to Azure VM Scale sets using VSTS and Packer? – Mani Bindra’s Blog()

  • AlexandrSN

    Thank you for thorough guide with examples, Kamil! I have tried it.
    The thing I noticed that “java -jar /usr/lib/selenium/selenium-server-standalone-2.53.1.jar -port 4444” is not being killed, when “sudo /etc/init.d/selenium stop” command is initiated. The script kills only “/etc/init.d/selenium” process. “java -jar /usr/lib/selenium/selenium-server-standalone-2.53.1.jar -port 4444” processes continue to hang.
    I’m trying to amend the script to kill exactly “java -jar /usr/lib/selenium/selenium-server-standalone-2.53.1.jar -port 4444” processes.