Watir on Rails
Watir is a tool for user acceptance testing and in general can be used to control a browser to do automated task. Since I am using the standard Rails's testing framework, and I run my tests with rake test, I thought why not to use watir test in a similar way. There is a plugin, Watir on Rails, that integrates Ruby on Rails with Waitr. You only need to run rake test:watir to test your application with rake. The test can be written in a similar way of the other Rails' tests. For instance:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.dirname(__FILE__) + '/../test_helper' | |
class PickaxeBookGooggleSearchTest < ActiveSupport::TestCase | |
include WatirOnRails | |
# Uncomment the following lines to specify a test server. | |
# WatirOnRails defaults to http://localhost:3000 | |
# | |
# server "localhost" | |
# port 3001 | |
# fixtures :foos, :bars | |
def test_pickaxe_google_search | |
test_site = 'http://www.google.com' | |
ie = open_browser | |
puts "Beginning of test: Google search." | |
puts " Step 1: go to the test site: " + test_site | |
ie.goto test_site | |
puts " Step 2: enter 'pickaxe' in the search text field." | |
ie.text_field(:name, "q").set "pickaxe" # "q" is the name of the search field | |
puts " Step 3: click the 'Google Search' button." | |
ie.button(:name, "btnG").click # "btnG" is the name of the Search button | |
puts " Expected Result:" | |
puts " A Google page with results should be shown. 'Programming Ruby' should be high on the list." | |
puts " Actual Result:" | |
if ie.text.include? "Programming Ruby" | |
puts " Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results." | |
else | |
puts " Test Failed! Could not find: 'Programming Ruby'." | |
end | |
ie.link(:text, "Pickaxe - Wikipedia, the free encyclopedia").click | |
if ie.text.include? "From Wikipedia, the free encyclopedia" | |
puts "The browser is inside Wikipedia" | |
else | |
put "Error: could not reach Wikipedia web site" | |
end | |
if ie.text.include? "Some people make the distinction that a pickaxe has a head with a pointed end and a flat end, and a pick has both ends pointed, or only one end; but most people use the words to mean the same thing." | |
puts "Yes, I can read the first paragraph" | |
else | |
puts "Error: I cannot read the first paragraph" | |
end | |
puts "Since I am not in the Pickaxe book web site, I am going back to the previous visited page (back button)" | |
ie.back | |
puts "I am following the link for the Pickaxe book..." | |
ie.link(:text, "The Pragmatic Bookshelf | Programming Ruby").click | |
ie.image(:src, "http://assets2.pragprog.com/images/logo.gif?1264475979").exists? | |
puts "Yes! I am inside the pragprog web site." | |
puts "End of test: Google search." | |
end | |
end |
How to use the example:
- gem install watir # install watir
- script/plugin install git://github.com/chip/watir-on-rails.git
- put the file in test/watir or script/generate watir SuccessfulLogin to generate a test
- run: rake test:watir
- watch the browser doing your job ;-)
- Start learning from the cheat sheet:http://wiki.openqa.org/display/WTR/Ruby+Cheat+Sheet
Watir need your web server on, if you are testing a local application, so you should run:
mongrel_rails start -e test # if you are using mongrel
Links:
Watir on Rails - Watir on Rails web page
Watir on Rails at Github
Watir - the official web site
1 Comments:
You can use my gem.
just put
gem 'watir-webdriver-rails' in your Gemfile
Here is how you can use it:
https://github.com/tanin47/watir-webdriver-rails
Post a Comment
Subscribe to Post Comments [Atom]
<< Home