Web application testing in Ruby(Watir) - 2 minutes guide

Watir (pronounced as Water) is a free open source tool which can be used to automate web applications. It is an extension of Ruby programming language. Unlike most of the other testing tools it gains the advantage of powerful features of Ruby and simulate browser interactions in very simple manner.
Lets see how a simple google search is automated using Watir in few steps.

Pre-requisites
Install Ruby (1.8.5-24 or later)

Step 1

Install Watir. Open a command window or shell and issue the following commands
gem update --system
gem install watir

The above two commands update gem installer and then install watir in your system.

Step 2

Open SciTE ruby editor or notepad and start to create the following script.

require "watir"

ie = Watir::IE.new

ie.goto("http://www.google.com")

ie.text_field(:name, "q").set("WSO2 WSAS")

ie.button(:name, "btnG").click


Step 3

Save the above file as SimpleTest.rb and run it from the command line by typing SimpleTest.rb
You will see that an Internet Explorer browser instance will automatically be popped up, access google, type "WSO2 WSAS" text and click on search button as a user interacts with the web site.

Step 4

Lets see what each of the above statements of our test script do.

require "watir" - This is similar to an import statement. This tells ruby script to use Watir as an extension library

ie = Watir::IE.new - Instantiate a new IE browser instance and open it

ie.goto("http://www.google.com") - Instructs IE instance to access google.com

ie.text_field(:name, "q").set("WSO2 WSAS") - Set text, "WSO2 WSAS" as the search query

ie.button(:name, "btnG").click - Click the "search" button

If you need to simulate web interaction with Firefox, you can use FireWatir, which allows to write test scripts for Firefox browser.

Comments

Unknown said…
Hi, I have to test a Ruby application and Eclipse is the IDE I am using. So what are the steps required to do the testing on Eclipse?
Mandeep
Charitha said…
Please read http://wiki.openqa.org/display/WTR/Setting+up+Eclipse+for+Ruby

Regards
Charitha
Unknown said…
Thanks, can you suggest me that I have to do the testing of a migration utility from a file to MySQL database on the Rails platform. I am using Ruby to code the utility. So is Watir is a good tool to do such testing and how can I measure the performance of those utilites e.g time taken from starting to finishing etc..etc
I will be really thankful to you for this guidance.
Charitha said…
Though I haven't got any experience with Rails, Watir is a tool for web application testing. As its name implied, Watir is a tool to test web apps using Ruby. Your scenario does not seem to have typical web interactions. Therefore, I think you may use a tool like Apache Jmeter.
Rajasekaran said…
I have some doubt in script of Text field...

http://www.google.com/analytics/ -> Click on Access Analytics -> Unable to enter data in Email and Password field using IRB command window....

I entered command as
ie.text_field(:name, "Email").set "raja"
ie.text_field(:name, "passwrd").set "raja"

Kindly help me to write the script for above senerio......
Eric Fried said…
Hi,

I have been using Ruby/WATIR to automate our application here, but I have run into a roadblock.

On one of the views in our web app, they have implemented an SVG object, representing a clickable grid of data. WATIR cannot read the data or the cells within this object. Is there some way I can read it?
Unknown said…
Hi Charitha,

I am new Watir. I have following all the steps. I am tring to execute your example. I am not able to start new IE instance. Can you please help me here.

================================
C:\Ruby>"C:\Documents and Settings\spanchumarthy\Desktop\Project_info\Watir\Simp
leTest.rb"
C:/Ruby/lib/ruby/gems/1.8/gems/windows-pr-0.9.3/lib/windows/thread.rb:59: uninit
ialized constant Windows::API::Error (NameError)
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `ge
m_original_require'
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `re
quire'
from C:/Ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.9/lib/win32/proces
s.rb:3
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `ge
m_original_require'
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `re
quire'
from C:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/ie-process.rb:
1
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `ge
m_original_require'
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `re
quire'
from C:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/ie.rb:45
from C:/Documents and Settings/spanchumarthy/Desktop/Project_info/Watir/
SimpleTest.rb:3
Anonymous said…
Thank you so much!!polo shirt men'ssweate,Burberry Polo Shirts lacoste sweater, ralph lauren Columbia Jackets,ski clothing. Free Shipping, PayPal Payment. Enjoy your shopping experience on mensclothingus.com.You can find the father who desire fashionable, intellectual mens clothing simultaneously.
http://community.fox2now.com/venusjj
http://cheappolos.blog.drecom.jp
http://d.hatena.ne.jp/crystal666
http://www2.atword.jp/pumaspeed
http://www.seriousblogging.com/basketspuma
Anonymous said…
Perfect!!You are a outstanding person!Have you ever wore chaussures puma,Here are the most popular puma CAT,Puma shoes store gives some preview of puma speed cat,and casual but no sweat puma basket.
http://blog.livedoor.jp/lljj332
http://shoes-puma.jugem.jp
http://poloshirts--myfashion.blogspot.com
http://blades.blogsome.com
http://gillettefusion.edublogs.org

Popular posts from this blog

Common mistakes to avoid in WSO2 ESB - 1 - "org.apache.axis2.AxisFault: The system cannot infer the transport information from the URL"

Working with HTTP multipart requests in soapUI

WS-Reliable messaging with WSO2 WSAS -1