07 June 2007

Selenium reports with screenshots using RSpec and imagemagick on ubuntu


After having read Aslak Hellesoy's post Watir reports with screenshots using RSpec and Win32::Screenshot, I got very excited because this was exactly the kind of thing I had been daydreaming while taking my morning shower (don't you?).
Aslak's article explains how to use Watir and Internet Explorer on Windows to generate rspec reports with screenshots. Garps! I am working on linux (ubuntu Feisty) as I can't afford a mac (yet) and can't stand Windows. I got a bit frustrated because I wanted to do the same thing on my ubuntu.

So, I decided to do a bit of researches and x*y*z hours later (sic), I got my selenium+rspec taking screenshots! My intention is to contribute to the RSpec sub-project spec_ui.


How does it work?


To get a 30s answer, please watch the flah screencast I have made of netbeans executing rake spec:selenium, running the selenium server, Webrick, lanching firefox and taking a screenshot after having maximized the screen.

Here is the code in spec_helper.rb :



config.before(:all, :behaviour_type => :view) do
selenium_remote_control_manager = SeleniumRemoteControlManager.new
selenium_remote_control_manager.start_webrick
selenium_remote_control_manager.start_selenium_server
@browser = selenium_remote_control_manager.get_firefox
@browser.start
end

config.after(:each, :behaviour_type => :view) do
Spec::Ui::ScreenshotFormatter.take_screenshot_of(@browser)
end

config.after(:all, :behaviour_type => :view) do
@browser.kill! rescue nil
end


And the code of contact_list_spec.rb:



describe "/contact/list", :behaviour_type => :view do
include SeleniumSpecHelper

it "should provide a description automatically (TODO extract)" do
open "/contact/list"
@browser.is_text_present("Frequently Texted").should be_true
# This line will fail :-)
@browser.is_text_present("All Contact Tak tak").should be_true
end

end




I have used imagemagick to capture the screenshot.



What's next?


I have had the chance to meet Aslak at an irb meeting in the beautiful Oslo and we talked about rspec and selenium. He told me about the idea of generating screencast from the execution of a selenium rspec and I have started to implement it. I am really close to get a solution but I will publish it by the end of july.

Here are a few features I'd like to have / implement for testing Rails Views:


  • a nice BNL compatible with watir and selenium
  • take a screenshot from the specs in order to generate nice documentation, not only when a spec fails
  • generate a good looking html doc with a sidebar - a la javadoc. And an AJAX search a la gotapi.com ;-) In a funky world, this would be connected to subversion and allow specs to be updated
  • to be completed ...

To conclude, I hope that tools such as greenpepper and RSpec are announcing the beginning of the end of "Microsoft Word" and "PrintScreen" as tools for writing specifications. Obviously, there will be some serious Manager / Customer / Developper resistance before accepting that tests are part of the delivery and THE only accurate specifications but let's wait for 5-10 years and see in 2012 how people work ;-)

No comments: