Click here to Skip to main content
13,200,729 members (52,342 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 21 Jul 2009

Test Automation using Watir and Power of Watir with Ruby Libraries

, 21 Jul 2009
Rate this:
Please Sign up or sign in to vote.
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 the powerful features of watir. Testing a web based application using different sets of data can be a 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 a good tool for their test automation needs. There are many powerful tools available in the market which would be costlier. But watir comes as an open source framework to simulate all the browser activities.

First of all, a brief introduction of the 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 simulates user action in the browsers.
  4. It supports all major browsers (Internet Explorer 6.0 and above and Firefox 2.0 and above versions)
  5. Ideal for smoke and Regression testing (Automated test cases which we need to execute repeatedly)
  6. Ability to use the full power of Ruby in test scripts (Ruby is pure OOPs based language and it has the power to read the data from Excel files and write the data to Excel files, XML files.)
  7. Running Automated test cases helps to achieve good test coverage

Setting Up the Environment

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 existing libraries:

  1. Network access, ftp, http, SMTP, POP
  2. Test::Unit
  3. Database connectivity
  4. 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 the below mentioned watir examples:

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

1. How to Automate the Simple Login Scenario

require "D:\\WATIRScript\\Log\\logger.rb"
require "watir"
require "test/unit"
require "win32ole"

class TC_Login < Test::Unit::TestCase

	$ie =
	def setup

	def test_login
	$ie.button(:id, /signIn/ ).click

Key things we notice here:

  1. We define a class that derives from the Ruby Test::Unit::TestCase class.
  2. We also defined a win32 ole API for windows and it provides the ability to access
  3. To read the data from Excel files and to write the data into Excel files
  4. In setup, I am creating an instance of Internet Explorer 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 will read the data from Excel file and for this purpose, we need to include the win 32 libraries. Check the below mentioned script:

require "D:\\WATIRScript\\Log\\logger.rb"
require "watir"
require "test/unit"
require "win32ole"

class TC_Login < Test::Unit::TestCase

	$ie =
def setup

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 <= 6
        $username=wrksheet.cells(rows,"A") ['text']
        puts $field
        $password=wrksheet.cells(rows,"B") ['text']
	$ie.button(:name, 'signIn' ).click

Key things we notice here:

  1. The 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 a local machine on the C Drive.
  2. Now using win 32 ole libraries, this script will read the data from the Excel file and it will test the Google login functionality with different user logins. We can have the ability to define n number of users in an Excel sheet
  3. I have defined rows in def test login. It will help to navigate all the values in the source Excel file.

3. How to Save the results in a Log File

We have to include the logger.rb path in the above script:

sample logger code:
require "log4r"
include Log4r
       $mylog = 'mylog'
       $mylog.outputters = Outputter.stdout
       $file =
        'fileOutputter', :filename => 'D:\WATIRScript\Log\filename',:trunc => false)
       $format = :pattern => "[%l] %d :: %m")

       $file.formatter = $format
       # log level order is DEBUG < INFO < WARN < ERROR < FATAL
       $mylog.level = Log4r::INFO

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

if   ($ie.contains_text(Google))
    $"Test Result \n\n")
     $"Test Result \n\n")

Key things we notice here:

Using the above verification code, we can compare the target text or URL/image on the target page. It will save the test result in the provided Excel file or Notepad.

4. How to Work with Outlook (How to Send an Email to Other People)

outlook ='Outlook.Application')
message = outlook.CreateItem(0)
message.Subject = ' Log file'
message.Body = 'body'
message.To = ''

With the above code, we can attach any screen shot and we can send any document to any person without opening Microsoft Outlook. Here I didn't go into details about all the Ruby libraries and Watir and Ruby both are open source. Watir has a good open source community and a lot of good documentation available online. We can get good help from them.

Points of Interest

  • Learning new Test techniques and implementing them in Software Test life cycle


  • 21st July, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tester / Quality Assurance Proteans Software Solutions
India India
Sofware Test Engineer at Proteans Software Solutions.

Proteans a CAMO group company is an outsourcing company focusing on software product development and business application development on Microsoft Technology Platform. "Committed to consistently deliver high-quality software products and services through continual improvement of our knowledge and practices focused on increased customer satisfaction.

You may also be interested in...


Comments and Discussions

QuestionCould you please explain how to use this class? Pin
Sasha197211-Jan-11 19:13
memberSasha197211-Jan-11 19:13 
GeneralWriting back to Excel Pin
SushmithCode23-Nov-09 20:11
memberSushmithCode23-Nov-09 20:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 21 Jul 2009
Article Copyright 2009 by khaja786
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid