Continuous Integration: Selenium + Firefox + Flash

Another continuous integration how-to to put into the Web.  This one is specifically about getting Selenium working in Ubuntu to test Firefox and Flash.

Download and Unpack Selenium RC.  Make sure you get the latest version since it has udpated support for firefox3.

You put the Selenium RC files wherever you want, I put it in ‘/opt/local’

Now install a couple of things:

apt-get install firefox-3.0
apt-get install flashplugin-nonfree
apt-get install xvfb

To do a quick test you can kick-off xvfb to create a fake virtual framebuffer using:

/usr/bin/Xvfb :99 &

If you type ‘firefox’ now you should see it prompt to the next line (without any errors) meaning that it was successfully loaded.  Hit CTRL-C to kill this instance of Firefox.

With Xvfb still running, kick-off the selenium server:

java -jar <PATH TO YOUR SELENIUM SERVER>/selenium-server.jar

Run your FlashSelenium tests and they should go green!  No need to do anything specific with the browser type.  The 1.0 version of selenium can properly find Firefox 3.0 in Ubuntu.

Now if you are like me, you’ll probably want these two guys to be loaded upon startup.

Here’s the init.d for Xvfb:

#!/bin/bash

if [ -z "$1" ]; then
echo "`basename $0` {start|stop}"
exit
fi

case "$1" in
start)
/usr/bin/Xvfb :99 &
;;

stop)
killall Xvfb
;;
esac

Make sure you give the script executable permissions with:

chmod 755 xvfb

Here’s the init.d for Selenium:

#!/bin/bash

case "${1:-''}" in
        'start')
                if test -f /tmp/selenium.pid
                then
                        echo "Selenium is already running."
                else
                        java -jar /opt/local/selenium-remote-control-1.0.1/selenium-server-1.0.1/selenium-server.jar > /tmp/selenium.log & echo $! > /tmp/selenium.pid
                        echo "Starting Selenium..."

                        error=$?
                        if test $error -gt 0
                        then
                                echo "${bon}Error $error! Couldn't start Selenium!${boff}"
                        fi
                fi
        ;;
        'stop')
                if test -f /tmp/selenium.pid
                then
                        echo "Stopping Selenium..."
                        PID=`cat /tmp/selenium.pid`
                        kill -3 $PID
                        if kill -9 $PID ;
                                then
                                        sleep 2
                                        test -f /tmp/selenium.pid && rm -f /tmp/selenium.pid
                                else
                                        echo "Selenium could not be stopped..."
                                fi
                else
                        echo "Selenium is not running."
                fi
                ;;
        'restart')
                if test -f /tmp/selenium.pid
                then
                        kill -HUP `cat /tmp/selenium.pid`
                        test -f /tmp/selenium.pid && rm -f /tmp/selenium.pid
                        sleep 1
                        java -jar /opt/local/selenium-remote-control-1.0.1/selenium-server-1.0.1/selenium-server.jar > /tmp/selenium.log & echo $! > /tmp/selenium.pid
                        echo "Reload Selenium..."
                else
                        echo "Selenium isn't running..."
                fi
                ;;
        *)      # no parameter specified
                echo "Usage: $SELF start|stop|restart|reload|force-reload|status"
                exit 1
        ;;
esac

Again, make this executable:

chmod 755 selenium

Before you add this to your startup scripts, you should probably test them out. If you’ve been following these write-up to a tee, you’ll have to kill both Selenium and Xvfb.  You can do that by using the following (If you are unsure if you have anything else using java in the background you may want to skip the ‘killall java’ line):

killall Xvfb
killall java

Test the scripts out now by running (in the init.d directory):

./xvfb start
./selenium start

To get these running on start-up use the following commands:

update-rc.d xvfb defaults 10
update-rc.d selenium defaults 

You’re now ready to get continually integrated.