Still Life

A Series of Mental Snapshots

Driving IE With Watir and Ruby – An Automation Adventure

Posted by Steve on July 14, 2008

In my current employment I will be building an automation framework for a web application that we have. The front door approach to this is to simulate a user, this means the need of a way to drive Internet Explorer. This is where Watir comes in.

I figured I could not explain watir as succinctly as those who developed it, therefore here is a little blurb from the ruby web page (http://wtr.rubyforge.org/):

Watir is a simple open-source library for automating web browsers. It allows you to write tests that are easy to read and easy to maintain. It is optimized for simplicity and flexibility.

Watir drives browsers the same way people do. It clicks links, fills in forms, presses buttons. Watir also checks results, such as whether expected text appears on the page.

Watir is a Ruby library that works with Internet Explorer on Windows. Watir is currently being ported to support Firefox and Safari.

Like other programming languages, Ruby gives you the power to connect to databases, read data files, export XML and structure your code into reusable libraries. Unlike other programing languages, Ruby is concise and often a joy to read.

Watir stands for “Web Application Testing in Ruby”. It is pronounced water.

So there you have Watir is the exact tool that I will be needing. The first thing to get up and running is to install ruby, which is easily done from the ruby home page. Once ruby is installed, the Watir ‘gem’ can be installed form a command line prompt by typing: “gem install watir” . You will get a prompt at the end telling you that the install was succesful. You will need one final tool,the internet developer tool bar. This is a tool bar that gets installed in IE that allows you to inspect almost every element on a web page, this is not only useful but close to necessary for the scripting work.

Alright, ruby and watir are in and we’re ready to go. There are two ways to get going, an interactive way and a scripting way. The interactive ruby can be started from the command line by entering: irb . The second way is by  creating a *.rb file and then running that file from the command line by entering: ruby *.rb . I personally find that I use the irb interactive form to test things, which I then put into the *.rb scripts.

Below is a very simple watir script that will open an IE browser, navigate to google, click a link and then close the browser:

require ‘watir’

ie = Watir::IE.new
ie.goto(“www.google.ca”)
ie.button(:name, “btnG”).click
ie.close

The first line tells ruby that we are using the watir gem. We then set a variable, ie, to a new instance of an internet explorer browser the .goto command then navigates to a given web page. The syntax for manipulating an element on a page is as follows, ie.ElementType(:Attribute, ValueOfAttribute).action . Finally ie.close, closes the browser. This may seem a little confusing at first, but there of course is a good cheat sheet and that is located here: http://pettichord.com/watirtutorial/docs/watir_cheat_sheet/WTR/Cheat%20Sheet.html

There we have it, a brief introduction to watir, enough to get you up and going. I will be posting more as I learn, I have a few things to back post as I have already begun my automation framework. If you have any questions about ruby/watir, feel free to ask and I will do my best to help you out.

–Steve

Was this quick tutorial useful? Is there anyway that I could improve the way the information is presented? If so I’d love to know, drop me a comment and I’ll try to incorporated it into future posts!

Advertisements

One Response to “Driving IE With Watir and Ruby – An Automation Adventure”

  1. ftorres said

    For functional web testing in C# or VB.NET integrated with Visual Studio, check out InCisif.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: