Basic example of using Watir and RSpec

Watir is a browser automating tool written in Ruby. It has been working mainly with Internet Explorer, but all of Watir’s contributors are working hard to make it fully compatible with Firefox and other browsers. Hell, they’ve just released a version 1.6.5, which aims a lot of Firefox inconsistencies. So, there is Watir (IE), Firewatir (Firefox), ChromeWatir and SafariWatir.

Let’s see how to use Watir (using other browsers should work quite similarly) then. There have been many examples how to use Watir to test your web application. For example, you can do something like this (all of my code samples assume that you have Ruby and Watir installed already):

There is even one similar example at Watir’s wiki. Pretty easy and straightforward test - open a browser, go to some page, verify some things and close the browser. Okay, this solution might seem to work at first, but come on - why not make your life easier? I can’t even imagine when you have some proper tests written already and you have to track all the failing and passing ones from the console window by looking for printed output and you have to do it manually. Why to have automated tests at all then? Why not use some proper testing framework instead so you can focus only on testing itself and let everything else handle by it automatically?

Here’s the place where RSpec comes into play. If you didn’t know already about RSpec then it is a (one of the many) testing framework written in Ruby. It is not meant specifically to be used with Watir or web applications but you can use it with (almost) whatever application you like. Since you are using Watir then the web application you are going to test can be even written in some cumbersome language like Java - in short your web application doesn’t have to be written in Ruby at all! So, let’s make the same test (actually tests are called as a specifications also known as specs in RSpec) using RSpec:

So, what do you think? Which one seems a little better? For me, RSpec seems a little more natural. Since RSpec has made nice usage of DSL then it even resembles natural English and the describe/it parts represent the specification of your application - that’s why they’re called specs. In a good situation you do not need any separate documentation at all, because your tests are specifying all the behavior of your application already.

When comparing RSpec against, let’s say Test::Unit, then the biggest difference is indeed it’s DSL and matchers. That’s the reason you can write browser.text.should include('string') instead of browser.text.include?('string').should be_true or even browser.text.include?('string').should == true.

And it also shows you a nice report of the results:

This output is just one of the possible outputs. You can have it in many different formats even in html. By executing

spec --format html:output.html watir_with_rspec.rb

you will get something like this. It is not all - you can write very easily your very own formatters also! I’m going to go into more detail about it in some of my future posts - i’m going to make one even better, fancier, faster (ok, maybe not faster) RSpec formatter for all of your web application user interface specifications you can imagine! I’m going to also tinker the way you have to write your specs by using default RSpec and Watir environment to make it even easier as it already is for some random human to understand and write their own specs.

Stay tuned.. we are going to have some fun :-)