Headless Selenium

Submitted by allan on Tue, 07/31/2012 - 16:26
In my work with AllPlayers.com, I have excelled my knowledge of Selenium probably double again. I started with Selenium for all the wrong reasons, but now, I consider myself pretty darn close to an expert. As a result, I was asked to come up with a solution that would implement Selenium on all of our development sandboxes via our configuration management system, Puppet. Now the truth is, I know very little about shell scripting. What I did learn though was invaluable as I built this project I wish to show you all today! However, I'm going to save the shell scripting lesson for another blog post.

What do I need?

Yes there are a few dependencies for this to work. The following list (possibly incomplete) is everything that I can think of off the top of my head: You should be able to get all of these things from your local Linux distribution. Notice, I tend to sway for Debian based Linux distributions.

How do I install?

Step 1

Download the latest script from my github repository and place the script in /etc/init.d/.

Step 2

Make the directory /usr/lib/headless-selenium.

Step 3

Download Selenium Server and place it in /usr/lib/headless-selenium.

Step 4

Edit the variable SELENIUM_JAR in the service file with the name of the selenium server you downloaded. (At the time I wrote the service the latest was 2.24.1)

Step 5

Create the directores up to /usr/lib/headless-selenium/profiles/firefox

Step 6

Create a selenium profile using firefox. There are several tutorials out there (particularly handy one). I've taken the liberty of creating a default one that you can do what you want to with.

Step 7

Put that Profile in /usr/lib/headless-selenium/profiles/firefox/selenium.

Step 8

Start the service! sudo /etc/init.d/headless-selenium start

Usage

sudo /etc/init.d/headless-selenium start sudo /etc/init.d/headless-selenium stop sudo /etc/init.d/headless-selenium restart sudo /etc/init.d/headless-selenium status

Debugging

You may need to debug whether headless selenium is running. The process that is best used is the following:

Check service status:

sudo /etc/init.d/headless-selenium status Headless Selenium should be “running”

Restart service:

sudo /etc/init.d/headless-selenium restart

Get a screenshot of Xvfb

DISPLAY=:42 import -window root screenshot.png

Check for output printed to the logs:

sudo tail -f /var/log/selenium/selenium-output.log Note: There are several different logs in /var/log/selenium which may be useful. Check to see if headless selenium is running via a process manager: ps -ef | grep selenium ps -ef | grep Xvfb

Final Thoughts

So now you have a running selenium server on port 4444 on a server that has no gui. You can run tests to your heart's content. I do however suggest that you set up something in your selenium framework to take screenshots so you can see "Why" your test failed, and you possibly don't have to rerun the test from your local machine to figure out what the hell went wrong with it. I've got a custom implementation that I may go over one day right here on this blog, and reasoning for doing so. Feel free to fork my service and contribute back to it if you have something better! As I said, I'm not the best shell coder in the world and would be happy to have someone else's opinion.