To run the test, look at all the things we had to do:
- update the source code
- start the server (script/server)
- start the hub
- start each of the remote controls and let them register with the hub
- finally! run the tests
This weekend I worked on scripting those dependencies (hey, the less I have to mess up, the better). Here's what I wrote:
- an init script for selenium-hub that starts the Ruby server and the hub
- a new environment for the grid (not strictly necessary but it gave me a place for some URLs for third-party dependencies that change by environment in the same place)
- a rake task to actually do the work of updating source code, etc.
So how does it all work now?
- Log in to the hub box
- set RAILS_ENV to the environment I'm interested in
- call the rake task
- Wait until it prompts me
- Start the remote controls
- Press enter and let the tests run
The trick is to make rake do all your prep work for you. Here's the interesting part of my task:
unless ENV.include?("platform") && @platforms.has_key?(ENV['platform'])
raise "Command line parameters incorrect! \nusage: rake test:run_browsers platform=[win|osx]"
else
platform = ENV['platform']
case platform
when 'win' then
browsers = @win_browsers
when 'osx' then
browsers = @osx_browsers
end
# Write out our environment
puts "Our environment is #{ENV['RAILS_ENV']}"
# Set up our db
puts "Migrating database and loading fixtures"
begin
Rake::Task["db:drop"].invoke
rescue
"Database does not exist"
end
Rake::Task["db:create"].invoke
Rake::Task["db:migrate"].invoke
Rake::Task["db:fixtures:load"].invoke
# Restart the environment
prepEnvironment(browsers)
# Run tests
puts "Running tests for the #{@platforms[platform]} platform"
browsers.each do |browser|
puts "Running tests in the #{browser} browser"
ENV["Browser"] = browser
ENV["TEST_DIR"] = 'test/selenium'
puts "RUN TESTS HERE"
Rake::Task["test:selenium"].invoke #run all the selenium tests
end
def prepEnvironment(browsers)
puts "Restarting Selenium Grid Hub and Ruby server"
system("/etc/init.d/selenium-hub restart")
sleep 5
print "\n"
print "\n"
print "\n"
print "Go start your Remote Controls. Then press enter to continue."
continue = STDIN.gets
host = "localhost"
port = "4444"
path = "/console"
data = checkSeleniumHub(host,port,path) # Hub is running on port 4444
checkNeededRCs(data,browsers) # All needed RCs for the platform are running
puts "Remote controls are running. Proceeding to test."
end
All I really did was take the rake task and make it do its own preparation. Saves time, and makes my tests run a lot more consistently. Hope it helps anyone else who is trying to get Selenium Grid running without human intervention.
Oh, and may favorite part of this is the STDIN.gets call (translation: wait for the human to go do the part she hasn't automated yet!).
10 comments: