I am watching the Dreams DVD from Fleetwood Mac and writing the last chapter for Professional Community Server when it hit me, you can combine Watir and RSpec. RSpec is a "framework for practicing Behavior Driven Development in Ruby (BDD)." Meanwhile, Watir is a framework for testing the presentation of a site using Internet Explorer. Whenever you combine the two you get something that is nothing short of magic (Sarah Silverman could have called it Watir + RSpec is Magic). I am exaggerating this point slightly, but it is still a very nice outcome.
To run my code you will need Ruby + Watir + RSpec.
The main point is to be able to declare simple action statements using Watir for a site and then define some behavior with expectations for those Watir actions. For testing the behavior of the Google site I have created the following Ruby code:
require 'watir'
include Watirclass GoogleSearch
def startup
@@ie = Watir::IE.start("http://google.com")
end
def ie
if defined? @@ie
@@ie
else
startup
end
enddef containingSearchBox?
ie.text_field(:name, /q$/).exists?
enddef containingSearchButton?
ie.button(:name, /btnG$/).exists?
enddef performSearch search
ie.text_field(:name, /q$/).set(search)
ie.button(:name, /btnG$/).click
enddef containingSearchItem? search
ie.contains_text(search)
end
endcontext "The Google Homepage" do
setup do
@googleSearch = GoogleSearch.new
end
specify "should contain search box" do
@googleSearch.should_be_containingSearchBox
end
specify "should contain search button" do
@googleSearch.should_be_containingSearchButton
end
endcontext "Google search results" do
setup do
@googleSearch = GoogleSearch.new
end
specify "should contain search item that was searched" do
@googleSearch.performSearch 'wyatt preul'
@googleSearch.should_be_containingSearchItem 'wyatt preul'
end
end
As you can see, the Watir code is contained in the top class and the code that defines this behavior is contained in the bottom context sections. It is also important to note that all of this code is contained in the same file, which I named WatirSpec_Google_test.rb.
After running the code using the spec command I get the following output:
Also, it is important to note that IE fires up a new window and performs the search when I run this specification. This is a new way that you can perform Watir testing without building actual test cases. There is a lot of potential usefulness out of using BDD as well, especially because of how readable it makes your results :)