Tag Archives: selenium

Running selenium tests on debian headlessly using iceweasel and firefox

We wanted to run selenium tests headlessly on a debian server from phpunit. Unfortunately the whole htmlunit via selenium2 isn’t available yet from PHPUnit. So, we installed xvfb (which is essentially a desktop), iceweasel (aka debian’s version of firefox) and set up xvfb to run in an init script at startup.

Install xvfb

To install xvfb:

sudo apt-get install xvfb

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)

To install iceweasel, which is currently the debian version of firefox:

sudo apt-get install iceweasel

And then, once you’re installed, to test that it works OK. Start Xvfb in one window as above, then in a second terminal window:

export DISPLAY=:99
firefox

you should see nothing – 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.

Create Xvfb startup script

We want xvfb to be running all the time, so that we can run selenium tests whenever we please. To do this, we create an init.d script. Here it is:

#!/bin/bash

XVFB=/usr/bin/Xvfb
# XVFBARGS=":99 -fbdir /var/run -ac"
XVFBARGS=":99 -nolisten tcp -fbdir /var/run"
PIDFILE=/var/run/xvfb.pid
case "$1" in
  start)
    echo -n "Starting virtual X frame buffer: Xvfb"
    start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS
    echo "."
    ;;
  stop)
    echo -n "Stopping virtual X frame buffer: Xvfb"
    start-stop-daemon --stop --quiet --pidfile $PIDFILE
    echo "."
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  *)
        echo "Usage: /etc/init.d/xvfb {start|stop|restart}"
        exit 1
esac

exit 0

You can also download a copy of the init script here.

Save this as /etc/init.d/xvfb.

Then:

sudo chmod a+x xvfb

Note: We’re running xvfb on the same server as selenium, and so we’ve enabled access controls to ensure that only processes from the same server can connect. Should you want to connect selenium from another server, you can run with the “-ac” option, which disables all access controls – anyone can connect.

Finally, we will add it to rc.d so that it starts when the system does:

update-rc.d xvfb defaults

Changing the selenium startup script

Finally, when firefox is run from selenium we want it to use display # 99. Edit the selenium startup script from this earlier post and add “export DISPLAY=:99″ just before the selenium server is started in both the “start” and “restart” sections:

export DISPLAY=:99
java -jar /usr/lib/selenium/selenium-server-standalone-2.0a7.jar -port 4443 > /var/log
/selenium/selenium-output.log 2> /var/log/selenium/selenium-error.log & echo $! > /tmp/selenium.pid

Installing Watir-webdriver, Selenium and Ruby on Windows XP to run browser tests

Running local browser tests can be a pain for a couple of reasons. They’re generally not intuititive to write, and they take an age to run. Using the new selenium-webdriver with watir solves both these issues. Watir tests are easy to write:

browser.goto "http://google.com"
browser.text_field(:name => 'q').set("Watir is working!")
browser.button(:name => 'btnG').click

and you can run selenium in “headless” mode. This means that you don’t see a browser instance opening and running the tests like in normal selenium tests, but rather it’s run using HTMLUnit.

I wrote the instructions below to help people set up their environment for some recent training. You can also download them in PDF format.

Installing Ruby

Downloading

We will be installing Ruby version 1.9.2 – the latest version of Ruby. You can download the installer from:

http://rubyforge.org/frs/download.php/72170/rubyinstaller-1.9.2-p0.exe

Download the file, save it on your desktop, and once complete, double click on the .exe file to open it. Agree with any security warnings that appear.

Installing Ruby

You should now see this window:

  • Click “Next”
  • Select “I accept the license” and click “Next”

On the next screen:

  • Change the installation path to “C:\Tech\Ruby192”
  • Check the “Add Ruby executables to your PATH” checkbox
  • Check the “Associate .rb and .rbw files with this Ruby installation” checkbox

The screen should now look like this:

Click “Install”

Once installation is complete, you should see this screen:

Click “Finish”, and the installer will close.

Testing Ruby Installation

We’re now going to test that ruby has installed properly by running ruby.

Open the windows Command Prompt by clicking on the start menu, and clicking “Run”.

When the “Run” popup appears, type “cmd” into the text box and click “Ok”:

When the Command Prompt appears, type:

ruby –v

and press Enter. You should see the ruby version number as in this screenshot:

Here you can see we have installed ruby version 1.9.2p0.

Installing Ruby DevKit

The rest of this howto (except for the paths) works across pretty much any operating system. There are some problems compiling some Ruby gems on Windows because of the lack of a C compiler installed by default. The Ruby DevKit takes care of all of this for you.

First of all, download the DevKit from:

http://github.com/downloads/oneclick/rubyinstaller/DevKit-4.5.0-20100819-1536-sfx.exe

Download the file, save it on your desktop, and once complete, double click on the .exe file to open it. Agree with any security warnings that appear.

When the 7-Zip self-extracting archive window pops up, you should change the path to:

C:\Tech\Ruby-DevKit\

Once the file has extracted, open the Windows Command Prompt by select Start / Ru, typing “cmd” and pressing Enter.

Next, navigate into the directory that you just extracted the files into. For instance:

cd ../../
cd Tech/Ruby-DevKit

Now type:

ruby dk.rb init

Which will generate a config file which should list the version of Ruby that we installed earlier.

Type:

notepad config.yml

To check that it’s OK. You should see a line at the bottom of the file which reads:

- C:/Tech/Ruby192

Next, we will install the DevKit enhancements. To do this, type:

ruby dk.rb install

Which should produce:

Finally, to check that the installation has been successful, we’re going to try compiling a gem (see section 2.4 below for a full explanation of gems) to check this installation. Type:

gem install rdiscount --platform=ruby

RDiscount should install correctly and you should see:

Temporarily enhancing PATH to include DevKit...

in the screen messages.

Next type:

ruby -rubygems -e "require 'rdiscount'; puts RDiscount.new('**Hello RubyInstaller**').to_html"

and press enter. You should see:

<p><strong>Hello RubyInstaller</strong></p>

In summary:

Installing Ruby Gems

Gems in Ruby are self-contained programs or libraries. So, the “mysql” gem contains code that you may use to connect to mysql. I say may as there can be multiple gems that do the similar things in different ways. For instance there are two well known and competing Object Relational Mappers – DataMapper and ActiveRecord.

In this section we will download and install the gems you will need to test.

To do this, return to the command prompt you have open above, and type in:

gem list

This lists the gems which are already installed.

We will now download and install the gems you need; which are:

  • selenium-webdriver
  • watir-webdriver
  • rspec
  • ruby-debug-ide
  • test-unit

The gem package manager will also download any dependencies that are required by these gems, and that you do not have installed. Return to the Command Prompt window, and type:

gem install selenium-webdriver watir-webdriver rspec ruby-debug-ide test-unit --platform=ruby

and press “Enter”.

Note: When installing gems on Windows with DevKit, you should always append

-- platform=ruby

This ensures that the source is downloaded and compiled locally rather than binaries downloaded which will not work on windows. More information can be found here: http://github.com/oneclick/rubyinstaller/wiki/Development-Kit under the heading “Example Native RubyGem Installations using the DevKit”.

It may take a while to complete, but once done you should see output that looks like this:

Installing Selenium

Although we will be creating tests using Watir, which is a Ruby test API, the tests will be run by a Selenium server. The call stack looks like this:

Watir tests -> Watir-Webdriver -> Selenium-Webdriver -> Selenium Server

This means that you can run Selenium tests without having it open a browser window. This is called running “headlessly”. This is much, much faster than having watir open a browser and run the tests. This means that you can run tests headless locally, prior to checking in, and then the build server can take care of the longer and heavier cross browser testing in the nightly build.

We’re going to install the standalone selenium server on your PC. To do this:

Create a new directory; C:\Tech\Selenium\

Go to http://code.google.com/p/selenium/downloads/list, and download the latest version of the selenium-server-standalone-2* file, saving it to C:\Tech\Selenium\

Open the Windows Command Prompt by clicking Start / Run, typing “cmd” and pressing Enter.

Navigate to the Selenium directory and list the contents by typing:

cd ../..
cd Tech/Selenium

You should see:

Now we’re going to start the Selenium server. Type:

java –jar selenium-server-standalone-2.0a5.jar

replacing 2.0a5 with the version number of your version. The Selenium server will now start. When the security alert appears, click the “Unblock” button:

You should see something like this in the Command Prompt window:

This means that the Selenium server is up and running and waiting for connections on port 4444. Leave the window where it is, as we will return to it later.

Installing the IDE

Everyone has their favourite IDE, but if you do not have one yet and want one that works well with Ruby, then Netbeans is a good choice. In this section we will install Netbeans and configure it for use with the version of Ruby we just installed.

Install the Java JDK where required

Netbeans is a Java application and needs the JDK (Java Development Kit) to run. So that we can be sure that everyone’s IDE works in the same way, we’re first going to download the current version of the Java JDK from here: http://is.gd/fdHJl

Download the file, save it on your desktop, and once complete, double click on the .exe file to open it. Agree with any security warnings that appear.

You should now see this window:

Click “Next”

Next, you will see the installation options page. Leave the default options as they are and click “Next”:

The installation will start.

When the installation is about half way through, a second window will pop up. This is for the JRE – Java Runtime Environment. You will see a window that looks like this:

Click “Next”, and the JRE will be installed.

The second window will disappear when the JRE has been successfully installed, and you will be left with:

Click “Finish” to close the window. When the browser window opens asking you to register, close it again.

Installing Netbeans

We’re going to download the Ruby version of Netbeans, which you can get from here:

http://netbeans.org/downloads/index.html

Download the Ruby version, save it on your desktop, and once complete, double click on the .exe file to open it. Agree with any security warnings that appear.

The first installation screen asks you which server you want to install with the IDE. As we’re not going to be doing any web development, deselect the “Glassfish Server Open Source Edition 3.0.1” checkbox:

Then click “Next”.

On the next screen, accept the Terms & Conditions by checking the checkbox, and select “Next”.

The next screen asks you for your installation location and the location of the JDK. The screen should look like this:

You must ensure that the “Java environment for the NetBeans IDE” path matches the one you just installed:

C:\Program Files\Java\jdk1.6.0_21

Click “Next”.

Click “Install”

Once the registration is done, uncheck the “Register” checkbox and click “Finish”.

1.5.3 Configuring NetBeans

We’re now going to check that NetBeans is configured correctly. To start, open NetBeans by selecting:

“Start / All Programs / NetBeans / NetBeans IDE 6.9.1”

Once it’s finished loading, select the Tools menu and select “Ruby Platforms”

You should now see the “Ruby Platform Manager”, which shows the different versions of Ruby installed on your computer. You should see two versions:

  1. Built in JRuby 1.5.1 – which comes bundled with NetBeans
  2. Ruby 1.0.2-p0 – the version we installed earlier

Click “Close” to close.

Testing your installation

Lastly, we’re going to test that your installation works by writing a quick test that uses Watir to run a test against the google homepage.

Create a new project

With Netbeans open, select “File” from the top menu, and then “New Project”.
In the new project dialog, select “Ruby” in the middle section, and then “Ruby Application” in the right panel.

Click “Next”.

On the next screen:

  • Rename the project to “RubyTraining”
  • Change the “Project Path” to “C:\Tech\Projects”
  • Deselect the “Create Main File” checkbox
  • Change the Ruby Platform to “Ruby 1.9.2-p0”

The screen should now look like this:

Click “Finish”.

Create your test file

Once the project has been created, right click on “Source Files” in the project pane, select select “New” / “Ruby File”.

Enter “google_test” into the ‘File Name” text box, and click “Finish”.

When the file opens, select all of the text in the file and replace it with the text below:

require 'watir-webdriver'

browser = Watir::Browser.new(:remote, :url => "http://127.0.0.1:4444/wd/hub", :desired_capabilities => :htmlunit)
browser.goto "http://google.com"
browser.text_field(:name => 'q').set("Watir is working!")
browser.button(:name => 'btnG').click
puts browser.url
browser.close

Save the file by pressing “Ctrl + S” or selecting File / Save from the menu.

This script

  • goes to www.google.com
  • enters “Watir is working!” in the question box
  • Presses the search button (btnG)
  • “puts” (prints to screen) the new browser URL
  • closes the browser

Run your test file

We’re now going to run this file in debug mode. To do this, either press “Shift + F6”, or select “Run” / “Run File” from the top menu.

When the Windows Security Alert pops up, select “Unblock”.

You’ll only need to do this once.

The IDE’s appearance will now change as the file is run.

When it’s finished, click on the “Output – google_test” tab in the lower right pane. You will see the output of the script:

The important line here is:

http://www.google.co.uk/search?hl=en&source=hp&q=Watir+is+working%21&btnG=Google+Search

This is the new URL that the script “put”

If you check the Command Prompt window where you earlier left Selenium open, you will also be able to see that Selenium has also been logging the output.

Installing Selenium server 2 as a service on Ubuntu

This post follows on from the earlier how to install selenium as a service on mac os x , with details of how to install the same standalone server on the Ubuntu server.  In this case, it was 8.04, but it should work in later versions too.

First, browse to http://code.google.com/p/selenium/downloads/list click on selenium-server-standlone-2.*.jar link, and then copy the url of the selenium-server-standlone-2.*.jar file.

Next, open up a terminal window and type:

sudo su
mkdir /usr/lib/selenium/
cd /usr/lib/selenium/
wget url-you-copied-above
mkdir -p /var/log/selenium/
chmod a+w /var/log/selenium/

Next, take this attachment, and save it as /etc/init.d/selenium

Now edit the file, and change the filename of the standalone server file (currently selenium-server-standalone-2.0a5.jar) to the name of the server file you downloaded above.

Finally, make the script executable:

chmod 755 /etc/init.d/selenium

To test:

/etc/init.d/selenium start

Finally, add the script so that it starts automatically when the server does:

update-rc.d selenium defaults

Installing Selenium 2 as a service on Mac OS X

This post covers how to install selenium standalone as a service on Mac OS X.  Version 10.6 in this case, but should work on 10.x

First, browse to http://code.google.com/p/selenium/downloads/list and copy the url of the selenium-server-standlone-2.*.jar file.

Next, open Terminal.app and type the following:

sudo mkdir /usr/lib/selenium/
sudo wget url-you-copied-above /usr/lib/selenium/
sudo mkdir -p /var/log/selenium/
sudo chmod a+w /var/log/selenium/

Next, save this file as ~/Library/LaunchAgents/org.nhabit.Selenium.plist.  Edit the file in your favourite editor, and change the filename on line 13 (currently selenium-server-standalone-2.0a5.jar) to that of the server file you downloaded above.

Now go back to Terminal and type:

launchctl load ~/Library/LaunchAgents/org.nhabit.Selenium.plist
launchctl start org.nhabit.Selenium

The Selenium server should now be started and running on port 4443.  You can check everything’s fine by typing:

ps auxwww | grep selenium

It logs to /var/log/selenium/.  You can check the error log here:

tail /var/log/selenium/selenium-error.log
Selenium server is being run as a daemon via launchd, which is controlled by launchctl.  The config file above was set up using this GUI:  http://sourceforge.net/projects/lingon/files/
Next: Installing Selenium server as a service on Ubtuntu