Watir (pronounced as ‘Water’) stands for ‘Web Automation Testing in Ruby’. Watir is a Ruby library which drives the Internet browsers the same way we people do. It clicks the links, fills the forms, and presses the buttons. It also checks results, such as whether expected text appears on the page. As Watir is built on Ruby, you have all the Ruby power to connect to databases, read data from files, import and export XML and structure your code into reusable libraries. Watir is available as a Ruby Gem.
Ruby has built-in support for OLE. And most of the browsers can be driven programmatically. Watir makes use of this very fact. Watir is different than other HTTP based test tools – instead of simulating the browser, it actually drives the browser through Object Linking and Embedding protocol, which is implemented on top of COM architecture.
How do I get started?
First you will need to install Ruby (if not already installed). You can use One-click Ruby Installer for Windows.
Then, install latest Watir gem.
C:\Watir>gem install watir
Successfully installed watir, version 1.5.1.1100
Installing ri documentation for watir-1.5.1.1100...
Installing RDoc documentation for watir-1.5.1.1100...
Once you install it, using this gem is very simple.
Some useful tools
- Scite is a good, light-weight editor bundled along with Ruby one-click installer. You can find it at <Ruby install dir>\scite\scite.exe.
- you can also use RDT – an Eclipse plugin for Ruby. RDT is great if you need a graphical debugger.
- IE developer toolbar lets you explore the HTML elements behind the visible elements on a web page, simply by clicking them. Useful to know the elements to automate.
- Firebug is also an excelent FireFox extension for identifying HTML elements.
Navigating the browser
Navigating the browser using Watir is very easy. After all, its a Ruby script so it has to be pretty short!
# load the watir library
require 'watir'
# get the browser
b = Watir::Browser.new
# navigate to URL you wish to visit
b.goto('http://www.google.com')
# close the browser
b.close
Sweet and Simple, isn’t it?
Finding the HTML elements
There are many common functions available with Watir to find HTML elements. Some of these are listed below:
- Browser.text_field(how, what) for text boxes
- Browser.button(how, what) for buttons
- Browser.select_list(how, what) for drop down lists
- Browser.checkbox(how, what) for check boxes
- Browser.radio(how, what) for radio buttons
- Browser.link(how, what) for hyperlinks
- Browser.form(how, what) for forms
- Browser.frame(how, what) for frames
- … and many more for div, label, image etc. tags.
“how”s tell your method how to find the element you are looking for, whereas “what”s tell your method the expected value of “how”. “what” can be a string or a regular expression. For example, “how”s can be :name or :id or :index or :value or :text or :title, and “what”s can be string like “buttonG” or regular expression like /^Spam/.
Interacting with elements
With watir, interacting with HTML elements is easy and straight-forward. We have set method to set values in input boxes, select method to select values from dropdown lists and click method to click on hyperlinks and buttons. Even accessing elements inside frame or iframe is simple.
Browser.text_field(:name, 'name').set('value')
Browser.select_list(:name, 'name').select('value')
Browser.button(:value, 'value').click
Browser.link(:text, 'text').click
Browser.frame(:name, 'some_frame').text_field(:name, 'name').set('value')
Checking the output
You can even check the output of web browser navigation. You can use Browser.title to read title set in the browser, you can use Browser.text to get all the text displayed on the page or you can even use Browser.html to get all the HTML source code inside body tag.
If you need to check if particular text or text matching given regular expression, you can use Browser.contains_text(‘text’) or Browser.contains_text(/pattern/), respectively.
Sample code
Below is a very simple ruby watir script, which is used to delete all the spam mails in your Gmail inbox.
require 'watir'
gmail = 'http://www.gmail.com'
username = '<your Gmail username>'
password = '<your Gmail password>'
browser = Watir::Browser.new
browser.goto(gmail)
# enter username, password and click 'Sign In' button
browser.text_field(:id, "Email").set(username)
browser.text_field(:id, "Passwd").set(password)
browser.button(:name, "signIn").click
# by default, we enter new UI which is tough to deal with.
# hence lets load simple HTML UI by entering its link directly into browser.
browser.goto('http://mail.google.com/mail/?ui=html')
# iterate through all checkboxes for spam mails, and check mark them
browser.link(:text, /^Spam/).click
if browser.checkboxes.length > 0 then
browser.checkboxes.each { | cb |
cb.set
}
# in gmail html ui, 'delete forever' button's name is 'nvp_a_dl'
browser.button(:name, "nvp_a_dl").click
end
# finally, sign out and close the browser.
browser.link(:text, "Sign out").click
browser.close
require 'watir'
gmail = 'http://www.gmail.com'
username = 'gprabhas@gmail.com'
password = 'mh12ev0509'
browser = Watir::Browser.new
browser.goto(gmail)
# enter username, password and click 'Sign In' button
browser.text_field(:id, "Email").set(username)
browser.text_field(:id, "Passwd").set(password)
browser.button(:name, "signIn").click
# by default, we enter new UI which is tough to deal with. hence lets load simple HTML UI by entering its link directly into browser.
browser.goto('http://mail.google.com/mail/?ui=html')
# iterate through all checkboxes for spam mails, and check mark them
browser.link(:text, /^Spam/).click
if browser.checkboxes.length > 0 then
browser.checkboxes.each { | cb |
cb.set
}
# in gmail html ui, 'delete forever' button's name is 'nvp_a_dl'
browser.button(:name, "nvp_a_dl").click
end
# finally, sign out and close the browser.
browser.link(:text, "Sign out").click
browser.close
This sample code is quite self-explanatory. It logs into Gmail, avoids stylish and heavy UI by navigating to HTML UI and then goes to Spam box, check marks all mails and clicks on ‘Delete Forever’ button. I wrote this script and created a scheduled task on my laptop, which runs every time I logs into my laptop account. Thus, I get rid of spam mails as soon as I start working!
Over the time, Watir have evolved to a larger extent. Initially it was able only to work with Internet Explorer. But now, latest versions of Watir are capable of working with other browsers as well, including Firefox, Safari and even Chrome.