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.
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
Mandeep
Regards
Charitha
I will be really thankful to you for this guidance.
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......
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?
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
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
http://blog.livedoor.jp/lljj332
http://shoes-puma.jugem.jp
http://poloshirts--myfashion.blogspot.com
http://blades.blogsome.com
http://gillettefusion.edublogs.org