Friday, August 1, 2008

Automation Testing with Watir

If you are looking for an open source tool for automating the testing of your web application, then let me suggest Watir for you...

Watir is a functional automation Testing tool for web based application. Watir stands for Web Application Testing In Ruby and it is pronounced as “Water”. Watir uses Ruby as the programming language, it is a full featured object oriented language. Ruby gives the power to connect to databases, read data files, export XML and structure your code into reusable libraries.

Watir is a Ruby library for writing test scripts for web applications. It works with Internet Explorer on Windows, via its COM interface. Watir is currently being ported to support Firefox and Safari. It is a Ruby library which drives Internet Explorer the same way people do, clicks links, fills in forms, and presses buttons etc...

Advantages of Watir:

  • Open Source Tool
  • Simple and easy to use/learn and powerful tool.
  • Good support for automating normal web based applications
  • Use Ruby - full featured object oriented language. Ruby gives the power to connect to databases, read data files, export XML and structure your code into reusable libraries.
  • Good tool for Data Driven or Keyword driven Test approach and it support accessing and reading from MS Excel or OpenOffice for Data driven tests.
  • Watir API is richer
  • Very Active development/enhancements happening for Tool with frequent releases; Also the Watir has a growing support/community on net.

Limitations of Watir:

  • The record and playback option is very limited with the Watir recorder. It does not support record and playback of web applications that has a frame.
  • Does not support Activex Plugin Components in your web pages.
  • Does not suppport JAVA Applets in web pages.
  • Does not suppport Macromedia Flash or other plugin applications

Installation :

First you will have to install Ruby 1.8.5 and then Watir. Refer the site http://wtr.rubyforge.org/install.html for finding the correct version of Ruby and Watir and installing it to the machine.

Normally for object recognisation, we use IE developers Tool bar. This also can be downloaded and installed from the same above link.

Start using Watir:

Watir has a simple syntax and that is one of the reason, I mantioned it is "Simple and easy to use/learn" tool. So of the basic syntax are given below...

  • ie=IE.new #-- To open a new IE window
  • ie.goto("http//www.google.com ") #-- To navigate to google page.
  • ie.text_field(:name,'name').set('value') #-- For accessing text field in a web page
  • ie.select_list(:name,'name').select('value') #-- For accessing dropdown list in a web page
  • ie.button(:name,'name').click #-- For clicking button in a web page
  • ie.link(:text,'text').click #-- For clicking link in a web page
  • ie.frame(:name,"someFrame").text_field(:name,'name').set('value') #-- For accessing text field inside a frame in a web page

One more thing, I would like to tell you when you start working with Watir is about IRB. You can try out test script ideas interactively with irb. To start IRB on Windows, select start > Run... and type irb in the Open field. Click OK, and a DOS console should open with a command prompt. Require the Watir library in IRB and enter commands to start driving a web browser interactively, it looks like this:





In Depth :
With the the above listed, you would be able to write test scripts for your web application.. and now, let us get a little in depth.. here few details on all you would require to design a complete automation suit with Watir. As we have very little documentation for Watir, probably the below details would reduce your time for doing research in finding out.

Screenshot capturing:

There is an inbuild method for capturing screenshot of web application and it is given below. This can be called when a test case fails, so that screenshot of failure will be captured and saved in the same directory from which you run the script.

screen_capture()

Generating Results Files:

There can be two kind of results files. Along with the default TXT logger, you can create an XML result file with some modification in the logger class as described below.

class XMLLogger < level =" Logger::INFO" datetime_format = "%d-%b-%Y %H:%M:%S" logfile =" File.new(xmlFileName," version="'1.0'" encoding="'ISO-8859-1'?">"
@logfile.puts ""
@logfile.puts ""
end

#hackish method to log results without aid of REXML or other Ruby XML tools
def log_results(number, tc_number, name, test_status, screen, notes)
log("XML file output: S.No: " + number + ", TC No: " + tc_number + ", Test Case Name: " + name + ", Status: " + test_status + ", Screen Shot: " + screen + ", Comments: " + notes + "")
@logfile.puts''
@logfile.puts''
@logfile.puts tc_number
@logfile.puts'
'
@logfile.puts''
@logfile.puts name
@logfile.puts'
'
@logfile.puts''
@logfile.puts test_status
@logfile.puts'
'
@logfile.puts''
@logfile.puts screen
@logfile.puts'
'
@logfile.puts''
@logfile.puts notes
@logfile.puts'
'
@logfile.puts"
"
end
#hack to close the tag and the file
def end_log
@logfile.puts"
"
@logfile.close
end

end

How to handle pop-up:

Here a sample script which handle the pop-up and click "ok" button in the pop-up that shows up when we click the button with index "1" in the ie.

$ie.button(:index,1).click_no_wait
click_popup_ok_button

def click_popup_ok_button
begin
hwnd = $ie.enabled_popup(10)
if(hwnd)
wc = WinClicker.new
wc.clickWindowsButton_hwnd(hwnd, "OK")
end
rescue Watir::Exception::TimeOutException
$logger.log("The Popup is not shown.")
result_fail
end
end

Data base connection:

Ruby has different methods of database connectivity.

Database driver:

The syntax for connecting mysql database with database driver is below.

require 'mysql'
mysql = Mysql.init()
mysql.connect('server','username','password')
mysql.select_db("schema")
a = mysql.query("query;")
a.each do record
b = record[0]
print b
end

Similary you can connect any other DB also.

DBI:

The DBI connectivity interface is a database independent wrapper to make SQL calls. DBI is an interface to the low level database drivers. DBI will NOT work unless you install the low level database drivers.

dbh = DBI.connect('DBI:Mysql:test', 'username', 'password')

If you face any trouble in making the DB connection works, get the mysql.so and libmysql.dll etc files as instructed in http://rubyforge.org/forum/message.php?msg_id=4293

Have a try with Watir and I hope you will enjoy it.