When utilising Selenium to test web applications, I was always under the impression that PhantomJS was the only option for servers without an X server. I have nothing but good things to say about PhantomJS and have used it successfully for testing in the past. However, while working on some pull requests for splinter recently, I discovered that Chrome and Firefox can also be configured to do the same thing.
To begin with, download the Google Chrome debian package using a regular PC and upload it to your server.
Install Chrome as follows:
|
|
In addition to Chrome, you’ll also need the Chrome Driver. Download this on a regular PC and upload the zip file to your server.
Now extract it to /usr/bin and set appropriate permissions:
|
|
Next up, we’ll install Firefox:
|
|
To make use of a regular browser in Selenium sessions, we need to install the X Virtual Framebuffer and related fonts:
|
|
Now that everything’s installed, you may start up a virtual display. Each virtual display has a number. I’ll use display number 99 below to setup our example display.
|
|
An even better way of starting and stopping an xvfb session in your Python code is with the use of xvfbwrapper.
Install it as follows in your virtualenv:
|
|
And here’s some sample code to get you started:
|
|
Another popular option is PyVirtualDisplay but I couldn’t get that working when I tried it. I’ll need to look into why soon.
xvfbwrapper is robust and has no dependencies so I honestly had no reason to really pursue PyVirtualDisplay but it’s a similar library and worth a look too.
One of the biggest challenges one may face when testing like this is the multitude of OS and browser combinations. On bigger projects where browser and OS compatibility are very important, you may wish to use a service such as Sauce to perform your testing. Their service gives access to over 300 browser/OS combinations and is free for open source projects.
Have fun! :)