Sunday 9 February 2014

Getting Started with Watir In One Minute Guide

Watir (Web Application Testing in Ruby) is a great functional testing framework for web applications. However, newcomers often have difficulties. If you do, you may find this useful.

Use TestWise RubyShell

TestWise Rubyshell is a pre-packaged Watir software on Windows platform.
  1. Install TextWise RubyShell
    Download latest RubyShell installer, double click rubyshell-1.x.x-setup.exe to install, accept all default options.
    (Default to c:\rubyshell)
  2. Try out: Watir
    You are ready to run Watir scripts. Start a Window command window,
    1.cd C:\rubyshell\samples\watir
    2.ruby google_watir.rb
  3. Try out: Watir with RSpec + Reports
    Double click C:\rubyshell\samples\watir_rspec\demo.bat

Use RubyInstaller and install Watir manually

  1. Install Ruby
    Install Ruby (recommending v1.8.7) using the one-click installer for Windows.
  2. Install Watir
    Start a Windows command window (if you are unfamiliar with this, you'd better try TestWise IDE)
    gem install watir
    If you behind a firewall and need proxy to access Internet,
    gem install -p http://proxy_server:proxy_port watir
  3. Create a sample Watir script file
    Open notepad, paste into the text below,
    1.require 'watir'
    2. 
    3.browser = Watir::IE.start("http://google.com")
    4.browser.text_field(:name"q").set "Watir IDE"
    5.browser.button(:name"btnG").click
    then save as c:\google_watir.rb
  4. Run Watir script
    Start a Windows command window,
    1.cd C:\
    2.ruby c:\google_watir.rb
    to run the watir test script you just created, which will start an Internet Explorer window: doing a google search.

Use TestWise IDE

Create, edit, manage and run Watir/RWebSpec test scripts in single tool.

  1. Install TestWise IDE
    Download free TestWise Community Edition installer, double click TestWise-1.x.x-setup.exe to install, accept all default options.
  2. Startup TestWise IDE
    You may launch TestWise after the installation (default) 
    Or start up normally
  3. Try it out
    TestWise will load the sample project if it is first time. 
    If not, close current project, click menu File -> Open Project, then choose c:\Program File\TestWise\samples\demo-watir\demo-watir.tprproject file.
    Select the file hello_world_watir.rb on the left, the test script will be opened in the editor.
    Right mouse click in the editor, select 'Run Ruby script'
    An Internet Explorer will be started to run the Watir script.

Installing Ruby and Watir on Windows 7

I recently started playing around with Web Application Testing in Ruby or Watir for short. As is recommended I downloaded the Homebrewer's Guide to Watir by Zeljko Filipin and started trying to install the latest versions of the RubyInstaller for Windows (currently 2.0.0) when I ran into a few problems. (Random tangent: I like the idea of using Leanpub to create and modify install documents for open source applications.)

I installed Ruby and Watir on a Windows 7 64 bit machine using the 32 bit versions and everything seems to work fine so far. Here's how I installed everything using the RubyInstaller and instructions from the guide above (note some of these instructions will be a duplicate of the guide):

Ruby Installation
  1. Open a command prompt (Run and type cmd) and type ruby -v to see if you have Ruby installed (if you don't it will say the command isn't recognized) and the version number. 
  2. I have Windows 7 64 bit but I use the 32 bit installers from RubyInstaller Downloads including:
    1. Ruby 2.0.0-p247
    2. DevKit-mingw64-32-4.7.2
  3. Run the Ruby installer and on the optional tasks page select the below items before completeing:
    1. Add Ruby executables to your PATH
    2. Associate .rb and .rbw files with this Ruby installation
  4. Open a new command prompt and type ruby -v to see if Ruby installed and the version number. 
  5. (Optional) Once installed you can update the RubyGems distributed with the installer by typinggem update --system and watching for the "RubyGems system software updated" to know when its complete. 
  6. Move on to the DevKit installation. 
DevKit Installation
  1. Run the DevKit installer but change the extraction folder to C:\devkit
  2. Open a command prompt (Run and type cmd) and change the folder to C:\devkit (use the command cd c:\devkit).
  3. Run the command ruby dk.rb init. If this step is successful you'll see a response like "[INFO] found RubyInstaller v2.0.0 at C:/Ruby200".
  4. Run the command ruby dk.rb install. If this step is successful you'll see a response like "[INFO] Installing 'C:/Ruby200/lib/ruby/site_ruby/2.0.0/rubygems/defaults/operating_system.rb'" and "[INFO] Installing 'C:/Ruby200/lib/ruby/site_ruby/devkit.rb'"
  5. Move on to Watir installation.
Watir Installation
This is where I ran into problems with the 2.0.0 version of Watir. Something about the mini_magick version erroring out. To prevent this problem we do:
  1. Run the command (still from the C:\devkit command window) gem install mini_magick -v 3.5.0 which works around the version problem. You should get a response like "2 gems installed"
  2. Then run the command gem install watir --no-ri --no-rdoc to install the rest of Watir. You should get a response like "Successfully installed watir-4.0.2-x86-mingw32 and 23 gems installed". 
Check the installation
In the same command window type (after each line of code submit the commands)

  1. irb
  2. require "watir"
  3. browser = Watir::Browser.new
  4. browser.goto "google.com"
If all is setup correctly you should get your default browser to open a new window and then browse to Google. Good luck.