Test Automation using Watir and Power of Watir with Ruby Libraries
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:
- Watir Stands for “Web Application Testing In Ruby”
- Open-source Framework for browser-based automated testing
- Test scripts are written in the Ruby programming language and Watir is a Ruby library that simulates user action in the browsers.
- It supports all major browsers (Internet Explorer 6.0 and above and Firefox 2.0 and above versions)
- Ideal for smoke and Regression testing (Automated test cases which we need to execute repeatedly)
- 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.)
- 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:
- Network access, ftp, http, SMTP, POP
- Test::Unit
- Database connectivity
- 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:
- How to automate the simple login scenario.
- How to work with Excel files (how to read the data from an Excel file)
- How to store the test results in a log file.
- 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:
- We define a class that derives from the Ruby
Test::Unit::TestCase
class. - We also defined a win32 ole API for windows and it provides the ability to access
- To read the data from Excel files and to write the data into Excel files
- 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:
- 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.
- 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
- 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