Make selenium process management code more robust and informative

This commit is contained in:
Bryan Helmkamp 2009-04-06 12:14:14 -04:00
parent 304baeb754
commit 91ea8cfa54
3 changed files with 95 additions and 19 deletions

View File

@ -0,0 +1,27 @@
class TCPSocket
def self.wait_for_service_with_timeout(options)
start_time = Time.now
until listening_service?(options)
verbose_wait
if options[:timeout] && (Time.now > start_time + options[:timeout])
raise SocketError.new("Socket did not open within #{options[:timeout]} seconds")
end
end
end
def self.wait_for_service_termination_with_timeout(options)
start_time = Time.now
while listening_service?(options)
verbose_wait
if options[:timeout] && (Time.now > start_time + options[:timeout])
raise SocketError.new("Socket did not terminate within #{options[:timeout]} seconds")
end
end
end
end

View File

@ -3,6 +3,7 @@ gem "selenium-client", ">=1.2.14"
require "selenium/client"
require "webrat/selenium/selenium_session"
require "webrat/selenium/matchers"
require "webrat/core_extensions/tcp_socket"
module Webrat
@ -16,9 +17,31 @@ module Webrat
unless Webrat.configuration.selenium_server_address
remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", Webrat.configuration.selenium_server_port, 5)
remote_control.jar_file = File.expand_path(__FILE__ + "../../../../vendor/selenium-server.jar")
silence_stream(STDOUT) do
remote_control.start :background => true
end
TCPSocket.wait_for_service :host => (Webrat.configuration.selenium_server_address || "0.0.0.0"), :port => Webrat.configuration.selenium_server_port
end
$stderr.print "==> Started selenium RC server. Waiting for it to boot... "
silence_stream(STDOUT) do
TCPSocket.wait_for_service_with_timeout \
:host => (Webrat.configuration.selenium_server_address || "0.0.0.0"),
:port => Webrat.configuration.selenium_server_port,
:timeout => 15 # seconds
end
$stderr.print "Ready!\n"
at_exit do
silence_stream(STDOUT) do
Webrat.stop_selenium_server
end
end
rescue SocketError
$stderr.puts
$stderr.puts
$stderr.puts "==> Failed to boot the Selenium RC server... exiting!"
exit
end
def self.stop_selenium_server #:nodoc:
@ -45,15 +68,50 @@ module Webrat
exec 'rackup', File.expand_path(Dir.pwd + '/config.ru'), '-p', Webrat.configuration.application_port.to_s
end
when :merb
cmd = 'merb'
merb_cmd = 'merb'
if File.exist?('bin/merb')
cmd = 'bin/merb'
merb_cmd = 'bin/merb'
end
system("#{cmd} -d -p #{Webrat.configuration.application_port} -e #{Webrat.configuration.application_environment}")
cmd = "#{merb_cmd} -d -p #{Webrat.configuration.application_port} -e #{Webrat.configuration.application_environment}"
system cmd
else # rails
system("mongrel_rails start -d --chdir='#{RAILS_ROOT}' --port=#{Webrat.configuration.application_port} --environment=#{Webrat.configuration.application_environment} --pid #{pid_file} &")
cmd = "mongrel_rails start -d --chdir='#{RAILS_ROOT}' --port=#{Webrat.configuration.application_port} --environment=#{Webrat.configuration.application_environment} --pid #{pid_file} &"
system cmd
end
TCPSocket.wait_for_service :host => Webrat.configuration.application_address, :port => Webrat.configuration.application_port.to_i
$stderr.print "==> Started #{Webrat.configuration.application_framework} application server for Selenium. Waiting for it to boot... "
silence_stream(STDOUT) do
TCPSocket.wait_for_service_with_timeout \
:host => Webrat.configuration.application_address,
:port => Webrat.configuration.application_port.to_i,
:timeout => 30 # seconds
end
$stderr.print "Ready!\n"
at_exit do
silence_stream(STDOUT) do
Webrat.stop_app_server
end
end
rescue SocketError
$stderr.puts
$stderr.puts
$stderr.puts "==> Failed to boot the application server for selenium... exiting!"
case Webrat.configuration.application_framework
when :sinatra
when :merb
$stderr.puts
$stderr.puts "Verify you can start a Merb server with the folliwing command:"
$stderr.puts
$stderr.puts " #{cmd}"
else # rails
$stderr.puts
$stderr.puts "Verify you can start a Rails server with the folliwing command:"
$stderr.puts
$stderr.puts " #{cmd}"
end
exit
end
# Stop the appserver for the underlying framework under test

View File

@ -189,16 +189,11 @@ module Webrat
end
def setup #:nodoc:
silence_stream(STDOUT) do
silence_stream(STDERR) do
Webrat.start_selenium_server
Webrat.start_app_server
end
end
create_browser
$browser.start
teardown_at_exit
extend_selenium
define_location_strategies
@ -210,14 +205,10 @@ module Webrat
$browser = ::Selenium::Client::Driver.new(Webrat.configuration.selenium_server_address || "localhost",
Webrat.configuration.selenium_server_port, Webrat.configuration.selenium_browser_key, "http://#{Webrat.configuration.application_address}:#{Webrat.configuration.application_port}")
$browser.set_speed(0) unless Webrat.configuration.selenium_server_address
end
def teardown_at_exit #:nodoc:
at_exit do
silence_stream(STDOUT) do
$browser.stop
Webrat.stop_app_server
Webrat.stop_selenium_server
end
end
end