Test Automation Using Watir and Power of Watir With Ruby Libraries

Description: An article on how watir helps Test automation and power of Watir by implementing Ruby libraries

I am using Watir for our product test automation and I want to share some of it’s powerful features of
Watir. Testing a web based application using different sets of data can be repeated task (Term called Regression testing). In this article we can walk through how data driven tests can be performed and how the test results can be saved.

Picking a Automation Tool:
Automation team can choose the good tool for their test automation needs. There are many powerful tools available in market which would be more costly. But watir comes as Open source frame work to simulate all the browser activities.

First of all brief introduction of about WATIR features:

1. Watir Stands for “Web Application Testing In Ruby”
2. Open-source Framework for browser-based automated testing.
3. Test scripts are written in the Ruby programming language and Watir is a ruby library that simulate user action in the browsers.
4. It Supports all major browsers. (IE 6.0 and above and Firefox 2.0 and above versions)
5. Ideal for smoke and Regression testing (Automated test cases which are we need execute repeatedly)
6. Ability to use the full power of Ruby in test scripts .(Ruby is pure oops based language
and it has power to read the data from excel files and to write the data to excel files, xml files.)
7. Running Automated test cases help to achieve good test coverage

Setting Up the Environment:
The latest versions of Ruby 1.8.2 and Watir 1.6 and install ruby using one-click installer.

Ruby libraries:
Watir uses ruby libraries and I am sharing some of the existed libraries:

a. Network access, ftp, http, SMTP, POP
b. Test::Unit
c. Database connectivity
d. win 32 ole

Ruby has access to the WIN32OLE library, which is basically like an API for windows applications. What you can do is use this library to catch these pop up windows. Below is the code that you’ll have to add and need to run in a Ruby script.

require ‘win32ole’ – It loads the win32ole library in the Watir Test scripts. We need to include ‘require’ statement to the Watir library to load the Ruby test libraries.

Running a Watir test:
I am sharing below mentioned Watir examples:

1. How to automate the simple login scenario.
2. How to work with excel file (how to read the data from Excel file)
3. How to store the test results in log file.
4. How to work with outlook (how to send an email to other persons)

1. How to automate the simple login scenario:

require “D:WATIRScriptLoglogger.rb”
require “watir”
require “test/unit”
require “win32ole”
class TC_Login

$ ie = Watir::IE.new
def setup
$ ie.goto(“http://gmail.com”)

def test_login
$ ie.text_field(:id,”Email”).set(“username”)
$ ie.text_field(:id,”Passwd”).set(“password”)
$ ie.button(:id, /signIn/ ).click


Key things we notice here:

a. We define a class the derives from the Ruby Test::Unit::TestCase class.
b. We also defined a win32 ole API for windows and it provides the ability to access to read
c. the data from Excel files and to write the data into Excel files.
d. In setup I am crating an instance of IE and it will open our test site in the browser.

2. How to work with excel file (how to read the data from Excel file).
Watir read the data from excel file and for this purpose we need to include the win 32 libraries. check below mentioned script.

require “D:WATIRScriptLoglogger.rb”
require “watir”
require “test/unit”
require “win32ole”
class TC_Login

$ ie = Watir::IE.new

def setup

$ ie.goto(“http://gmail.com”)

def test_login
#Define the excel file
excel= WIN32OLE::new(“excel.Application”)
wrksheet = wrkbook.worksheets(1)

#read the data from excel file

rows = 2

while rows

$ username=wrksheet.cells(rows,”A”) ['text']

puts $ field

$ password=wrksheet.cells(rows,”B”) ['text']

$ ie.text_field(:id,”Email”).set($ username)

$ ie.text_field(:id,”Passwd”).set($ password)

$ ie.button(:name, ‘signIn’ ).click


Key things we notice here:

a. Above mentioned script will test the Google site login functionality with different logins. we need to create one excel file with the columns: username and Password. and save it into local machine on C Drive.
b. Now using win 32 ole libraries this script will read the data from the excel file and it will test the Google login functioanlit with different user logins. We can have ability to define n no of users in excel sheet
c. I have defined rows in def test login it will help to navigate all the values in source excel file.

3. How to save the results in log file:
We have to inlude the the logger.rb path in the above script:

sample logger code:
require “log4r”
include Log4r

$ mylog = Logger.new ‘mylog’

$ mylog.outputters = Outputter.stdout

$ file = FileOutputter.new(‘fileOutputter’, :filename => ‘D:WATIRScriptLogfilename’,:trunc => false)

$ format = PatternFormatter.new( :pattern => “[%l] %d :: %m”)

$ file.formatter = $ format

# log level order is DEBUG

$ mylog.level = Log4r::INFO

$ mylog.add($ file)

We need to create one rb file and add it into our script, and need to write the verification (below mentioned) code and we can have the ability to store the results in target log file/notepad.

if ($ ie.contains_text(Object))

$ mylog.info(“Test Result nn”)


$ mylog.info(“Test Result nn”)


4. How to work with outlook (how to send an email to other persons)

outlook = WIN32OLE.new(‘Outlook.Application’)
message = outlook.CreateItem(0)
message.Subject = ‘ Log file’
message.Body = ‘body’
message.To = ‘xyz@yahoo.com’

With above code we can attach any screen shot and we can send it to any person without opening Microsoft Outlook.

Here I didn’t mention detailed information about all the Ruby libraries. Watir and Ruby both are open source. Watir has good open source community and lot of good documentation available online. We can get the good help from them.

All the best and Happy Testing.

I am Khaja shariff and ISTQB Certified Software Tester and having about five years of testing experience with various product domains such as Online data backup and School governance, Microsoft products.

Related Ebooks Library Articles

This entry was posted in Ebooks Library and tagged , , , , , , . Bookmark the permalink.