Project

General

Profile


Functional Website Testing with Selenium

We will use Jenkins CI":http://jenkins-ci.org/ in order to run "SeleniumHQ tests headlessly on a Debian server.

Install SeleniumHQ

The Selenium Server is needed in order to run either Selenium RC style scripts or Remote Selenium Webdriver ones. Download the Selenium Standalone Server from http://code.google.com/p/selenium/ into an appropriate location and create a softlink:

mkdir /usr/lib/selenium
cd /usr/lib/selenium
wget http://selenium.googlecode.com/files/selenium-server-standalone-${Version}.jar
ln -s selenium-server-standalone-${Version}.jar selenium-server.jar

Test run the selenium server

edit-develop:/usr/lib/selenium# java -jar selenium-server.jar
15-Mar-2011 12:29:48 java.util.prefs.FileSystemPreferences$2 run
INFO: Created user preferences directory.
12:29:48.291 INFO - Java: Sun Microsystems Inc. 17.1-b03
12:29:48.295 INFO - OS: Linux 2.6.26-1-xen-amd64 amd64
12:29:48.299 INFO - v2.0 [b2], with Core v2.0 [b2]
12:29:48.475 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
12:29:48.479 INFO - Version Jetty/5.1.x
12:29:48.479 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
12:29:48.479 INFO - Started HttpContext[/selenium-server,/selenium-server]
12:29:48.479 INFO - Started HttpContext[/,/]
12:29:48.507 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@68e6ff0d
12:29:48.507 INFO - Started HttpContext[/wd,/wd]
12:29:48.511 INFO - Started SocketListener on 0.0.0.0:4444
12:29:48.511 INFO - Started org.openqa.jetty.jetty.Server@30f7f540

See also Installing Selenium server 2 as a service

Install xvfb

The following installation instructions have been copied from the great guidance on this topic http://www.danstraw.com/running-selenium-tests-on-debian-headlessly-using-iceweasel-and-firefox/2010/11/24/, though we found that we need to install additional packages we decided repeat and extend this guidance here.

To install xvfb:

sudo apt-get install xvfb apt-get libgl1-mesa-dri xfonts-cyrillic xfonts-100dpi xfonts-75dpi

And then to check everything’s OK:

Xvfb :99

Where :99 is the display number. We set it to 99 to avoid any conflict with other displays, should they be attached. The command line should just hang without any comments until you press CTRL-C to close.

Install iceweasel (firefox)

Iceweasel is a fork Firefox with the following purpose.

Iceweasel can be installed from the Debian repository with

@ apt-get install icewease@.

Nevertheless a newer backport from the Debian Mozilla team is usually preferable:

Add the sources (in this example for debian squeeze), add the apt key and install the latest iceweasel release

echo "deb http://backports.debian.org/debian-backports squeeze-backports main" > /etc/apt/sources.list/iceweasel.list
echo "deb http://mozilla.debian.net/ squeeze-backports iceweasel-release" >> /etc/apt/sources.list/iceweasel.list
apt-get install pkg-mozilla-archive-keyring
apt-get update
apt-get install -t squeeze-backports iceweasel

Test & troubleshooting iceweasle & tests

Remotely via X11 forwarding

Open a ssh session with X11 forwarding (with compression) and start icewaesel from within the session

ssh -c arcfour,blowfish-cbc -XC root@160.45.63.201
iceweasel &

After a little while (depending on the connection speed) iceweasel will appear on your local computer screen.

Headless with screen shot

And then, once you’re installed, to test that it works OK.

  1. Start Xvfb in one window as above,

  2. then in a second terminal window:

export DISPLAY=:99
firefox

you should see nothing except maybe a message that the extension [RANDR" missing on display ](99.0") – firefox should just sit there until you CTRL+C.

The “export DISPLAY=:99″ line is telling firefox to use display # 99, which we started above.

we want to take take a screenshot in order to check that everything is ok. We will use ImagMagick to take screen shots in addition we also install the x11-utils in order get the xwininfo tool.

See also:

apt-get install imagemagick x11-utils

Now take a screenshot of the root window:

export DISPLAY=:99
import -window root example.png

The screenshot would look like this:

[!examplepng|50%!]

If you want to get a list of all windows currently managed by Xvfb :

xwininfo -root -tree

Jenkins CI

Install the Seleniumhq Plugin and configure it according to http://wiki.hudson-ci.org/display/HUDSON/Seleniumhq+Plugin

However in order to actually run the test suites we need to configure a Execute shell build step which will start selenium using the headlessSelenium.sh which is found below.

The Execute shell build step:

 bash -ex /usr/lib/selenium/headlessSelenium.sh $WORKSPACE  http://160.45.63.201/dataportal/jenkins/ $WORKSPACE/test/selenium/TestSuite-search.html

The headlessSelenium.sh script:

#!/bin/bash
#
# USAGE:
#     bash -ex /usr/lib/selenium/headlessSelenium.sh $BASEURL $WORKSPACE $TEST_SUITE
#
#
WORKSPACE=$1
BASEURL=$2
TEST_SUITE=$3

SELENIUM="java -jar /usr/lib/selenium/selenium-server.jar"
FIREFOX_BIN="/usr/lib/iceweasel/firefox-bin"
DISPLAY=":99"
#
# This option creates screen screennum and sets its width, height, and depth to W, H, and  D  respectively.
# By default, only screen 0 exists and has the dimensions 1280x1024x8.
#
SCREEN="0 1280x1024x16"

#Use virtual X server
VIRTUAL_X="Xvfb $DISPLAY"

#init
if [ -z "$(pidof Xvfb)" ]; then
        $VIRTUAL_X &
fi
export DISPLAY

$SELENIUM -htmlSuite "*firefox $FIREFOX_BIN" ${BASEURL} ${TEST_SUITE} ${WORKSPACE}/selenium-results.html

The following screenshot givens an example of a Jenkins job configuration:

[!drupal5-cdm_dataportal_Configpng|50%!]

example.png View (42.4 KB) Andreas Kohlbecker, 03/15/2011 12:03 PM

drupal5-cdm_dataportal_Config.png View (135 KB) Andreas Kohlbecker, 03/15/2011 09:06 PM

Add picture from clipboard (Maximum size: 40 MB)