RSpec your functional tests
The release of RSpec 0.9.4 can be considered a landmark release of sorts. With the inclusion of Spec::Ui 0.2.0, RSpec can now perform functional testing of web applications within a browser. Out of the box support is limited to Watir/Internet Explorer for now as the Selenium RC custom matchers are not implemented yet. includes WATIR/Windows and Watirsafari/OSX. The custom matchers for Selenium RC are not implemented yet but its certainly possible to use Selenium with Spec::Ui, it just won't read as nicely compared to WATIR. Spec::Ui also comes with a custom result formatter that packages a screenshot of the browser on spec failure, along with its HTML source in the report.
To use Watirsafari in your functional specs, you'll need to install the gem for it: gem install watirsafari
spec_helper.rb should be updated to include the snippet below (code lifted from the original release announcement):
-
Spec::Runner.configure do |config|
-
config.include Spec::Matchers::Watir
-
end
The specification will need before and after blocks like this:
-
before(:all) do
-
@browser = Watir::Browser.new
-
end
-
-
after(:each) do
-
# This is needed to make screenshots work
-
Spec::Ui::ScreenshotFormatter.browser = @browser
-
end
-
-
after(:all) do
-
@browser.kill! rescue nil
-
end
There are a lot more useful information available in the examples supplied with Spec::Ui. Take a look at the samples to get up and running.
Update
My thanks to Aslak Hellesoy for correcting me on support for WATIR and Selenium.
Aslak Hellesøy said,
May 11th, 2007 at 4:39 am
The out of the box Watir support is not limited to IE. You can also use it with Safariwatir, which implements the same API as the original Watir.
Further, you can use it with Selenium-RC with Spec::Ui 0.2.0 - but without the custom matchers, your expectations just won't read as well as for watir (yet):
# Selenium-RC
@browser.is_text_present("Ali G").should be_false
# Watir
@browser.should_not have_text("Ali G")