Starting with Watir-WebDriver

Sometime ago, I started working with Watir-WebDriver – an API that runs on top of Selenium 2.0 which means it is capable of automating most major browsers. Watir is short for Web Application Testing in Ruby (my new favorite language). Don’t get confused with Watir and Watir-WebDriver though – Watir only automates IE.

Installation

I am on Windows (v7 x64) but the installation for your OS shouldn’t be much different. I am assuming you have Ruby installed already. To begin, download the development kit from RubyInstaller.org that applies to your system. If you are on either Linux or Mac OS X, refer the downloads page at Ruby-Lang.org. After download is complete, follow these steps:

  1. Unzip the file
  2. Open shell prompt (cmd.exe) and set the folder where you unzipped all files
  3. In shell, execute the following:
ruby dk.rb init
ruby dk.rb review
ruby dk.rb install
gem install watir-webdriver --platform=ruby

If you encounter the below error at step ruby dk.rb review:

Simply add the PATH to your ruby installation directory to your config.yml file. For example, Ruby is installed in C:\Ruby200-x64 on my system so the new content of the file becomes:

# This configuration file contains the absolute path locations of all
# installed Rubies to be enhanced to work with the DevKit. This config
# file is generated by the 'ruby dk.rb init' step and may be modified
# before running the 'ruby dk.rb install' step. To include any installed
# Rubies that were not automagically discovered, simply add a line below
# the triple hyphens with the absolute path to the Ruby root directory.
#
# Example:
#
# ---
# - C:/ruby19trunk
# - C:/ruby192dev
#
---
- c:/Ruby200-x64

Run step ruby dk.rb review again but this time it should pass.

You can resume from step ruby dk.rb install and finish installing Watir now.

Examples

Here are some examples demonstrating basic usage.

# create new firefox instance
b = Watir::Browser.new :firefox
 
# navigation
b.goto "http://newtours.demoaut.com"
 
# check if textbox exists using 
b.text_field(:name => "userName").exists?
 
# set value
b.text_field(:name => "userName").set "test"
b.text_field(:name => "password").set "test"
 
# click login
b.button(:name => "login").click
 
# radio
b.radio(:value => "oneway").set
# is this value selected?
puts b.radio(:value => "oneway").set?
 
# listbox
b.select_list(:name => "fromPort").select "Portland"
# verify if Portland selected
puts b.select_list(:name => "fromPort").selected?("Portland")
 
b.radio(:value => "Business").set
puts b.radio(:value => "Business").set?
 
# div: when its present, output text
puts b.div(:class => "footer").when_present.text
 
# screenshot
b.screenshot.png
b.screenshot.save "file.png"

Singular vs Plural

This is a very neat feature – use singular to find the first occurence of the object or use plural to find all its occurences.

b.div     # returns the first div
b.divs    # returns a collection of div
 
b.image   # returns the first img
b.images  # returns a collection of img
 
b.table(:id => "table").images  # images inside a table
 
b.divs(:class => "test")  # all divs with class => test

This was just an intro so I only covered the basic usage of the API. I will write a comprehensive post with all types of elements and their various methods in the upcoming post.

Subscribe for Relevant Codes newsletter to get awesome updates and articles delivered straight to your inbox.

Leave a Comment

{ 10 comments… add one }

  • Vinoth July 28, 2013, 1:45 am

    Hi Anshoo,
    I was able to install Ruby & watir webdriver and run the above script successfully.
    I like this. Thank you so much.

    But i feel, this watir is slow. Have you ever felt the same?

    Reply
    • Anshoo Arora July 28, 2013, 12:19 pm

      Yes, I also felt that its a little slow. What browser are you using? Its fine on Firefox and Chrome for me, but a little slow with IE.

    • Anonymous July 28, 2013, 12:59 pm

      yep..I should have mentioned the browser. I ran the scripts in IE. It is fine in Firefox.

  • Vinoth July 21, 2013, 5:19 pm

    Hi Anshoo,
    Could you please let us know what IDE you use for Ruby / Watir?

    Reply
    • Anshoo Arora July 22, 2013, 11:06 am

      Vinoth: I am using Eclipse.

    • Vinoth July 22, 2013, 4:29 pm

      Gr8.
      Thanks a lot Anshoo. Please keep up your excellent work.
      I am eagerly waiting for your Selenium and Watir Comparison.

  • Vinoth July 21, 2013, 11:56 am

    Hi Anshoo,

    Thanks for sharing. I have learnt a lot from your blog.
    I have been using QTP for 5 years. I love QTP except the fact that it does not support safari / mobile devices and new versions of Chrome / Firefox. So i recently started learning Selenium for this.

    Since you already have worked on both Watir and Selenium webdrivers, Do you think Watir-Webdriver is easier than Selenium-Webdriver. Could you please share your thoughts here? Is Ruby easy to learn?

    Reply
    • Anshoo Arora July 22, 2013, 11:07 am

      I have planned to write something to compare the two, but Watir-WebDriver in my opinion is much better. Its more flexible and uses Ruby – a really fun language to use.

      Ruby: surely easier than C# and Java.

  • Anshu Khandelwal July 20, 2013, 10:07 am

    Great Post Anshoo….:)

    Reply
    • Anshoo Arora July 22, 2013, 11:08 am

      Thanks Anshu. Good to see you here :)