Tag Archives: watir

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.