What RSpec hook shall be used to perform clean up task after all tests are finished?
rake, rspec, rspec2, ruby
Solution
You don't want to do these things in Rake tasks. If you just want one browser instance to start at the beginning and persist for all your tests, you can do that in `spec/spec_helper.rb` using a `before :suite` hook. Something like:
RSpec.configure do |config|
# (other RSpec config)
config.before(:suite) do
Browser.initialize # or whatever
end
config.after(:suite) do
Browser.close
end
end
This will launch the browser once at the beginning and close it at the end. It's more likely that you'll want to use before and after hooks only for certain test files, or collections of tests. Start with the RSpec docs.
You might want to consider using Capybara instead of Watir; it integrates well with RSpec and you won't have to do this stuff manually. There's also watir-rspec, but I've never used it myself. This is a solved problem, in any case. It'll be easier to find something to help you than to set this up on your own.
Problem
I have this situation in my project - I have a Singleton class representing browser used during the test: ``` class Browser include Singleton def initialize @browser = Watir::Browser.new :ff end def goto url @browser.goto url end def close @browser.close end end ``` With this rakefile I wanted to make sure browser gets closed when the tests are finished: ``` desc "default test task" task :test_all do Rake::Task[:all_rspec_tests].invoke Rake::Task[:close_browser].invoke end desc "runs all rspec tests" RSpec::Core::RakeTask.new(:all_rspec_tests) do |t| # run all rspec tests according to pattern for filename # tests make use of Singleton class representing browser end desc "closes browser used during tests" task :close_browser do Browser.instance.close end ``` But this doesnt work as expected - RSpec runner instantiates it's own singleton object instance which the close_browser task does not see. So when the close_browser task gets scheduled another browser instance is instantiated and closed right away but the one used during the test remains open. How can I achieve that after all RSpec tests have ran the browser gets closed?? I guess this must be done with some configuration of the RSpec global hooks?? Would someone point me to an example of such hooks? Thank you!