Controlling the Selenium server from in Rails
I realized as I was about to check-in the Selenium stuff I had been working on for a Rails app for work (yes, I know, frequent check-ins, etc.) that there would have to be a dependency on having the server running somewhere. That’s a dependency fraught with danger so I looked at removing it.
Turns out that it is remarkably easy-ish with the selenium-client gem which I was using anyways. It comes with some rake tasks to control the server if you have the server jar tucked away in the project.
- Extract selenium-server.jar from the Selenium release and put it somewhere in your Rails project. I put it in vendor/selenium-remote-control. I also renamed it to selenium-server-1.0.1.jar so I can tell at a glance which version I have.
- At time of writing, the current version of the gem is 1.2.15 and it has a incompatibility with version 1.0.1 of Selenium RC with regards to shutting it down. To fix it, change shutDown to shutDownSeleniumServer in lib/selenium/remote-control/remote-control.rb in your local copy of the gem.
- In Rake and Selenium I posted my test:selenium Rake task. Well, I’ve changed it a bit so it starts the server before running the test(s) and shuts it down afterwards.
require 'selenium/rake/tasks' namespace :test do task :selenium do Rake::Task["selenium:rc:start"].execute begin Rake::Task["selenium:rc:tests"].execute ensure Rake::Task["selenium:rc:stop"].execute end end end Rake::TestTask.new(:'selenium:rc:tests') do |t| t.libs <p> To replicate the old, non-server-controlling behavior the task is now selenium:rc:tests.</p>
Of course, this assumes that you are doing it all on localhost. If you wanted to get really trick you could take this idea and implement it with capistrano or vlad or even puppet.