I use Selenium since many years. It's a great project and turned out to be very useful for me on various small and large projects. Currently, I'm creating a few tests for a project of mine using Python. This works reasonably well. However, it turned out to be a little tricky to get things up and running.

Installing the Bits

Installing Selenium looks pretty straight-forward at first sight. A quick check on the fedora repolist reveals that all we need is provided:

sudo yum info selenium-core

Loaded plugins: fastestmirror, langpacks, presto, refresh-packagekit
Installed Packages
Name        : selenium-core
Arch        : noarch
Version     : 1.0.2
Release     : 0.5.20100324svn.fc15
Size        : 1.0 M
Repo        : installed
From repo   : fedora
Summary     : A DHTML test execution framework
URL         : http://seleniumhq.org/projects/core/
License     : LGPLv2 and MIT and ASL 2.0 and GPLv2+ and (MPLv1.1 or GPLv2+ or LGPLv2+)
Description : Selenium Core is the engine of both, Selenium IDE and Selenium RC (driven
            : mode), but it also can be deployed on the desired application server.

Same goes for selenium-server, btw. Well, not really. Surprisingly, the selenium packages for fedora are quite outdated. They provide selenium version 1.02, while the current standalone server version happens to be on version 2.24.x. This is quite exceptional since fedora packages are quite on track with latest stables in general.

That's not a big stopper anyway, since downloading and using selenium is a kids task. Grab the standalone server, put in on a reasonable place like /usr/local/lib/java and create a simple start-script called selenium-server:

#!/bin/bash
java -jar /usr/local/lib/java/selenium-standalone-server.jar

plus a quick chmod a+x, and we're basically done. Starting your server with selenium-server on the command line should be fine now.

As for the python side, the python bindings for selenium are not in fedora repos. Hence we're going to PyPI and get them there with a quick

sudo pip install selenium

Voila, we're done with the installation part.

Webdriving Firefox 64bit

A subsequent check with python to verify correct operation revealed another caveat. I checked the firefox webdriver with

>>> from selenium import webdriver
>>> browser = webdriver.Firefox()

and got a nice message that the webdriver wasn't able to fire up firefox due to a "wrong ELF class: ELFCLASS32" error for libX11.so.6. A short research uncovered the issue being a known problem of selenium as described in Issue 2852. The selenium driver looks up libX11 on a wrong location.

The somewhat ugly workaround for this is to redirect to the lib64 dll with a symlink:

mv /usr/lib/libX11.so.6{,.x86}
ln -s /usr/lib64/libX11.so.6.3.0 /usr/lib/libX11.so.6

After this little hack, the firefox webdriver works as expected and the selenium testing fun finally was about to begin!

Going headless

I had written a couple of tests, I wanted to integrate them to the overall testing scripts. This actually requires to enable the tests to be headless. The're basically two options to choose from for headless testing.

The first option is to go for the (above created) server and use the RemoteDriver in the tests. This is quite straight-forward and did work very well in a few other projects I made. So I decided to check this path from within Python as well. Well, basically, it's just as easy as:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

browser = webdriver.Remote(
    desired_capabilities = DesiredCapabilities.HTMLUNIT
)

Naturally, the selenium server must be running as well. In our case it's running on localhost.

It's worth mentioning that the python bindings are fairly well documented.

While this server-based approach works very well and in my experience plays well in mid-sized projects / teams, there's a second neat alternative to go headless using a virtual display like Xvfb. This is especially useful if you want to use specific functionality like capturing network traffic of taking screenshots. I came across a nice blog post describing the basic usage and want to reiterate it for my case.

In order to use Xvfb with python, Xvfb itself and pyvirtualdisplay need to be grabbed:

sudo yum install xorg-x11-server-Xvfb
sudo pip install pyvirtualdisplay

The xvfb package name may vary on other distributions. After installing pyvirtualdisplay, running a headless test with Xvfb is as easy as:

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size(1024,768))
display.start()

browser = webdriver.Firefox()
browser.get('http://google.com')

print browser.title

browser.quit()
display.stop()

And: it works beautifully! The nice thing about it is that switching from headless to visual mode is just changing a few lines, which can be easily parameterized. As for my simple tests on my little project, I'm using the virtual display version. It's fast enough and enables me to do some advanced checking as well.


(c) Copyright . 1998 - 2013. Ilker Cetinkaya.