When your working with
selenium tests in
rspec you might want to run them in multiple browsers. The basic code generated by the selenium ide tool doesn't quite cover it and I couldn't find a good solution on google or
stackoverflow, so here's mine:
First you need a Rakefile that can start & stop
selenium-rc and you need a task to run all the spec files. Start and stop tasks are covered in the documentation for the
selenium-client gem. To that I've added a list of browsers to test, in my file FireFox, Google Chrome & Internet Explorer, and a rake task that runs the same specs for all browsers, the current settings for cucumber and spec generates a lot of text in it's output so you might want to clean it up a bit.
Also I pass the browsers to use to the spec files in a environment variable, there might be better ways of doing it but it works and was the fastest way of getting it done.
#Rakefile
require 'selenium/rake/tasks'
require 'rake'
require 'rake/testtask'
require 'spec/rake/spectask'
selenium_jar_file_path = './tools/test/selenium-server-1.0.3/selenium-server.jar'
browsers = ["*firefox", "*googlechrome", "*iexplore"]
desc "start the selenium-rc server"
Selenium::Rake::RemoteControlStartTask.new("rc") do |rc|
rc.port = 4444
rc.timeout_in_seconds = 2 * 60
rc.background = true
rc.wait_until_up_and_running = true
rc.jar_file = selenium_jar_file_path
end
Selenium::Rake::RemoteControlStopTask.new("rc:stop") do |rc|
rc.host = "localhost"
rc.port = 4444
rc.timeout_in_seconds = 3 * 60
end
task :selenium_spec_wrapper do
browsers.each do |browser|
ENV['s_browser'] = browser
rake_system("spec spec/*_spec.rb")
end
end
task :default => [:rc, :selenium_spec_wrapper, "rc:stop"]
In the before(:all) there the client driver is setup I get the environment variable set by the buildscript and use it, the fallback to *firefox is there to use when the script i run from my
ide or just by spec.
#spec file
before(:all) do
@verification_errors = []
@selenium_driver = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => if ENV['s_browser'].nil? == false
ENV['s_browser']
else
"*firefox"
end,
:url => 'http://localhost:3000/sommarkort/',
:timeout_in_second => 20
end