Configuring Ruby and Watir

by Klaus Graefensteiner 18. May 2010 07:26

Introduction

This is the ninth article of a series of blog post called the WIMPinator Chronicles that describe how to setup a PHP development environment for Windows 7 and IIS 7.5.

So far we covered how to install a Wordpress blog and all its dependencies using the WPI (Web Platform Installer). Then we installed additional features and extensions to the PHP deployment on Windows for IIS. I explained how to get PEAR setup and how to download and deploy the PHPUnit unit testing framework using PEAR. In the next two posts I described how to install Eclipse PDT and make it work with Subversion source control using Subclipse. I showed how to create an IIS 7.5 Fast CGI web site from scratch and in the most recent tutorial I covered how to use ANT build tasks to deploy your PHP project from Eclipse to your IIS test server using custom build steps. Last time I demonstrated how to use XDebug with PDT and IIS. Now we are ready to do even more automation.

This blog post describes how to install and configure the de facto Web UI test automation standard Watir. Watir’s scripting engine is Ruby. This article describes how to setup Ruby on Windows, install  and run some hello-world Web UI tests interactively.

WindowClipping

Figure 1: Watir for Web UI test automation

The Series

  1. Getting a Wordpress blog installed in a jiffy on Windows 7 using the Web Platform Installer
  2. Adding additional features to PHP
  3. Installing PEAR and PHPUnit
  4. Installing Eclipse PDT
  5. Configuring Eclipse PDT to work with Subversion source control
  6. Creating a new IIS 7.5 fast CGI web site
  7. Configuring Eclipse to work with Ant build tasks
  8. Setting up XDebug with Eclipse and IIS 7.5
  9. Configuring Ruby and Watir
  10. NEW: Writing Watir unit tests with Eclipse and RDT
  11. Moving a Wordpress blog from GoDaddy shared hosting to my local debugging system.
  12. NEW: Setting up the CodeIgnitor MVC framework with PDT and IIS 7.5
  13. NEW: Configuring IIS 7.5 Rewrite rules to exclude index.php from the URL
  14. NEW: Configuring SSL on IIS 7.5
  15. NEW: Writing unit tests with PHP Unit

References

Here are the links that I used to research the topic of this blog post:

Watir Hello World example:
http://wiki.openqa.org/display/WTR/Watir+in+5+Minutes

Installing Watir on windows:
http://watir.com/installation/#win

Installing Ruby on Windows:
http://wiki.openqa.org/display/WTR/Install+Ruby

Link to the correct one click installer of Ruby (186.26) for Watir 2.6
http://rubyforge.org/frs/download.php/29263/ruby186-26.exe

Watir plugin for Firefox on Windows
Windows plugin for Firefox 3

Using the Ruby Development Tools with Eclipse
http://www.ibm.com/developerworks/opensource/library/os-rubyeclipse/

Watir Tutorial:
http://wiki.openqa.org/display/WTR/Tutorial

Unit Test Examples:
http://svn.openqa.org/svn/watir/trunk/watir/unittests/

Installing Ruby

Install Ruby using the one-click installer for Windows. The Watir team recommends using Ruby 1.8.6-26 with Watir 1.6. A detailed description about how to install Ruby on Windows can be found here. Use the following link to download the One-Click installer: http://rubyforge.org/frs/download.php/29263/ruby186-26.exe .

File Download - Security Warning

Figure 2: Follow the link to download the Ruby One-Click installer

32% of 1 file - Downloads

Figure 3: Downloading Ruby

Ruby-186-26 Setup

Figure 4: Run the Ruby One-Click installer

Ruby-186-26 Setup

Figure 5: Acknowledge the license agreement

Ruby-186-26 Setup  (2)

Figure 6: Leave the default features checked

Ruby-186-26 Setup  (3)

Figure 7: Pick a Destination Folder

Ruby-186-26 Setup  (4)

Figure 8: Pick a Start Menu Folder

Ruby-186-26 Setup  (5)

Figure 9: Ruby is being installed

Update Ruby Gems and install Watir Gem

After Ruby is installed we need to update the Ruby Gems version to the latest revision and download and install the Watir gem

Make sure you have latest rubygems version. Which currently is 1.3.5 or above. Type the following command at command prompt:

gem -v

Administrator Command Prompt 

Figure 10:  Getting the rubygems version

Watir drivers are packaged as gems. Gems are Ruby libraries that can be installed over the internet.
First we need to update Ruby Gems to the latest version. Run the following command in the Administrator Command Prompt:

gem update --system

Administrator Command Prompt (2)

Figure 11: Updating to the latest rubygems version

For more information about ruby gems the this link: http://docs.rubygems.org/read/chapter/3

The following listing is the output of the update command:

Microsoft Windows [Version 6.1.7600] 
Copyright (c) 2009 Microsoft Corporation.  All rights reserved. 

C:\windows\system32>gem -v 
0.9.4 

C:\windows\system32>gem update --system 
Updating RubyGems... 
Need to update 1 gems from http://gems.rubyforge.org 
. 
complete 
Attempting remote update of rubygems-update 
Successfully installed rubygems-update-1.3.6 
Installing ri documentation for rubygems-update-1.3.6... 
Installing RDoc documentation for rubygems-update-1.3.6... 
Could not find main page README 
Could not find main page README 
Could not find main page README 
Could not find main page README 
Updating version of RubyGems to 1.3.6 
Installing RubyGems 1.3.6 
RubyGems 1.3.6 installed 

=== 1.3.6 / 2010-02-17 

NOTE: 

http://rubygems.org is now the default source for downloading gems. 

You may have sources set via ~/.gemrc, so you should replace 
http://gems.rubyforge.org with http://rubygems.org

http://gems.rubyforge.org will continue to work for the forseeable future. 

New features: 

* `gem` commands 
  * Added `gem push` and `gem owner` for interacting with modern/Gemcutter 
    sources 
  * `gem dep` now supports --prerelease. 
  * `gem fetch` now supports --prerelease. 
  * `gem server` now supports --bind.  Patch #27357 by Bruno Michel. 
  * `gem rdoc` no longer overwrites built documentation.  Use --overwrite 
    force rebuilding.  Patch #25982 by Akinori MUSHA. 
* Captial letters are now allowed in prerelease versions. 

Bug fixes: 

* Development deps are no longer added to rubygems-update gem so older 
  versions can update sucessfully. 
* Installer bugs: 
  * Prerelease gems can now depend on non-prerelease gems. 
  * Development dependencies are ignored unless explicitly needed.  Bug #27608 
    by Roger Pack. 
* `gem` commands 
  * `gem which` now fails if no paths were found.  Adapted patch #27681 by 
    Caio Chassot. 
  * `gem server` no longer has invalid markup.  Bug #27045 by Eric Young. 
  * `gem list` and friends show both prerelease and regular gems when 
    --prerelease --all is given 
* Gem::Format no longer crashes on empty files.  Bug #27292 by Ian Ragsdale. 
* Gem::GemPathSearcher handles nil require_paths. Patch #27334 by Roger Pack. 
* Gem::RemoteFetcher no longer copies the file if it is where we want it. 
  Patch #27409 by Jakub Šťastný. 

Deprecation Notices: 

* lib/rubygems/timer.rb has been removed. 
* Gem::Dependency#version_requirements is deprecated and will be removed on or 
  after August 2010. 
* Bulk index update is no longer supported. 
* Gem::manage_gems was removed in 1.3.3. 
* Time::today was removed in 1.3.3. 

------------------------------------------------------------------------------ 

RubyGems installed the following executables: 
        c:/ruby/bin/gem 

RubyGems system software updated 

C:\windows\system32> 

Now we are ready to deploy Watir using the gem deployment mechanism. In the Administrator Command Prompt run the following command:

gem install watir

Administrator Command Prompt (3)

Figure 12: Installing Watir

The following listing is the output of the install command:

C:\windows\system32>gem install watir 
Successfully installed win32-api-1.4.6-x86-mswin32-60 
Successfully installed windows-api-0.4.0 
Successfully installed windows-pr-1.0.9 
Successfully installed win32-process-0.6.2 
Successfully installed xml-simple-1.0.12 
Successfully installed json_pure-1.4.3 
Successfully installed rubyforge-2.0.4 
Successfully installed rake-0.8.7 
Successfully installed hoe-2.6.0 
Successfully installed s4t-utils-1.0.4 
Successfully installed builder-2.1.2 
Successfully installed user-choices-1.1.6.1 
Successfully installed commonwatir-1.6.5 
Successfully installed activesupport-2.3.5 
Successfully installed firewatir-1.6.5 
Successfully installed nokogiri-1.4.1-x86-mswin32 
Successfully installed watir-1.6.5 
17 gems installed 
Installing ri documentation for win32-api-1.4.6-x86-mswin32-60... 
Installing ri documentation for windows-api-0.4.0... 
Installing ri documentation for windows-pr-1.0.9... 
Installing ri documentation for win32-process-0.6.2... 
Installing ri documentation for xml-simple-1.0.12... 
Installing ri documentation for json_pure-1.4.3... 
Installing ri documentation for rubyforge-2.0.4... 
Installing ri documentation for rake-0.8.7... 
Installing ri documentation for hoe-2.6.0... 
Installing ri documentation for s4t-utils-1.0.4... 
Installing ri documentation for builder-2.1.2... 
ERROR:  While generating documentation for builder-2.1.2 
... MESSAGE:   Unhandled special: Special: type=17, text="<!-- HI -->" 
... RDOC args: --ri --op c:/ruby/lib/ruby/gems/1.8/doc/builder-2.1.2/ri --title 
Builder -- Easy XML Building --main README --line-numbers --quiet lib CHANGES Ra 
kefile README doc/releases/builder-1.2.4.rdoc doc/releases/builder-2.0.0.rdoc do 
c/releases/builder-2.1.1.rdoc --title builder-2.1.2 Documentation 
(continuing with the rest of the installation) 
Installing ri documentation for user-choices-1.1.6.1... 
Installing ri documentation for commonwatir-1.6.5... 
Installing ri documentation for activesupport-2.3.5... 
Installing ri documentation for firewatir-1.6.5... 
Installing ri documentation for nokogiri-1.4.1-x86-mswin32... 

No definition for parse_memory 

No definition for parse_file 

No definition for parse_with 

No definition for get_options 

No definition for set_options 
Installing ri documentation for watir-1.6.5... 
Installing RDoc documentation for win32-api-1.4.6-x86-mswin32-60... 
Installing RDoc documentation for windows-api-0.4.0... 
Installing RDoc documentation for windows-pr-1.0.9... 
Installing RDoc documentation for win32-process-0.6.2... 
Installing RDoc documentation for xml-simple-1.0.12... 
Installing RDoc documentation for json_pure-1.4.3... 
Installing RDoc documentation for rubyforge-2.0.4... 
Installing RDoc documentation for rake-0.8.7... 
Installing RDoc documentation for hoe-2.6.0... 
Installing RDoc documentation for s4t-utils-1.0.4... 
Installing RDoc documentation for builder-2.1.2... 
Installing RDoc documentation for user-choices-1.1.6.1... 
Installing RDoc documentation for commonwatir-1.6.5... 
Installing RDoc documentation for activesupport-2.3.5... 
Installing RDoc documentation for firewatir-1.6.5... 
Installing RDoc documentation for nokogiri-1.4.1-x86-mswin32... 

No definition for parse_memory 

No definition for parse_file 

No definition for parse_with 

No definition for get_options 

No definition for set_options 
Installing RDoc documentation for watir-1.6.5... 

C:\windows\system32> 

Administrator Command Prompt (4)

Figure 13: Watir installation output

Note: If you are using Windows 7 or Vista, make sure you run the above commands with Administrator privileges.

Tip: While updating RubyGems, you can use:
gem update --system --no-rdoc --no-ri instead of gem update –system. This will speed up the gems system update.

Prepare Firefox for Watir interaction

Watir’s support for Firefox also requires a plug-in. To install the plug-in you need to click the following link using Firefox 3.6 as your web browser. Windows plugin for Firefox 3.6

File Download

Figure 14: Installing Watir plug-in for Firefox

Software Installation

Figure 15: Trust this plug-in

Add-ons

Figure 16: Plug-in installation requires Firefox restart

Add-ons (2)

Figure 17: Plug-in ready to use

For optimal fidelity you also need to make sure that Firefox is configured correctly. The following two sets of screen shots show the default configuration and the for Watir testing preferred configuration.

clip_image016_2

Figure 18: Popup windows are blocked by default

clip_image018_2

Figure 19: Don’t block popup windows for Watir

clip_image020_2

Figure 20: Open new windows in new tab is the default setting

clip_image022_2

Figure 21: For Watir make sure that new windows get opened as new windows

Interactive Watir

Finally we are at the point where we can take Watir for a spin. Using the Interactive Ruby Command line interpreter we are going to open Google in IE or Firefox, enter "Watir" in search text box and push the search button. In the result page we will click on the Watir link.
After the redirect the script will search the Watir homepage for the term "Why Watir?".

To start IRB open a Command Prompt Window and type irb. This will launch the interactive ruby shell.

Note: If it IRB doesn’t start verify that your ruby binaries are included in the PATH environment variable.

Type the following two examples into the IRB line by line and commit each line by pressing the enter button:

require 'watir' 
Watir::Browser.default = "firefox" 
b = Watir::Browser.start("http://www.google.com") 
b.text_field(:name, "q").set "watir" 
b.button(:name, "btnG").click 
b.link(:href, "http://watir.com/").click 
puts b.text.include?("Why Watir?")

require 'watir' 
ie = Watir::IE.new 
ie.goto("http://www.google.com") 
ie.text_field(:name, "q").set "watir" 
ie.button(:name, "btnG").click 
ie.link(:href, "http://watir.com/").click 
puts ie.text.include?("Why Watir?") 

The following screenshot shows an IRB session:

image

Figure 22: Interactive Ruby and Watir session

Ausblick

At this point we are ready to automate web browser tests with Watir. In an upcoming blog post I am going to demonstrate how to write and run Watir unit tests in Eclipse using the RDT (Ruby Development Tools) plug-in.

Tags: , ,

WIMPinator Chronicles | Test Automation | Php | Ruby | Eclipse

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2014, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2014 by Klaus Graefensteiner

Rendertime:
Page rendered at 10/24/2014 8:26:34 PM (PST Pacific Standard Time UTC DST -7)