Click here to Skip to main content
Click here to Skip to main content

Tagged as

Test Automation using Watir and Power of Watir with Ruby Libraries

, 21 Jul 2009 CPOL
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

Introduction

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 = Watir::IE.new
	
	def setup
	$ie.goto("http://gmail.com")
	
	end

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

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 = Watir::IE.new
	
def setup
    
$ie.goto("http://gmail.com")	
	
end

def test_login
    
#Define the excel file
excel= WIN32OLE::new("excel.Application")
wrkbook=excel.Workbooks.Open("C:\\test.xls")
wrksheet = wrkbook.worksheets(1)
wrksheet.select     
 
#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.text_field(:id,"Email").set($username)
	$ie.text_field(:id,"Passwd").set($password)
	$ie.button(:name, 'signIn' ).click
  
  rows=rows+1
end
end

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 = Logger.new 'mylog'
       $mylog.outputters = Outputter.stdout
       $file = FileOutputter.new(
        'fileOutputter', :filename => 'D:\WATIRScript\Log\filename',:trunc => false)
       $format = PatternFormatter.new( :pattern => "[%l] %d :: %m")

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

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))  
		 $mylog.info("Test Result \n\n")
		 else
		  $mylog.info("Test Result \n\n")        
               end

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 = WIN32OLE.new('Outlook.Application')
message = outlook.CreateItem(0)
message.Subject = ' Log file'
message.Body = 'body'
message.To = 'xyz@yahoo.com'
message.Attachments.Add('path:\filename',1)
message.Save
message.Send

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

History

  • 21st July, 2009: Initial post

License

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

Share

About the Author

khaja786
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.

Comments and Discussions

 
QuestionCould you please explain how to use this class? PinmemberSasha197211-Jan-11 20:13 
GeneralWriting back to Excel PinmemberSushmithCode23-Nov-09 21:11 

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

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

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