lib/selenium_rc/server.rb in selenium-rc-2.2.4 vs lib/selenium_rc/server.rb in selenium-rc-2.3.0

- old
+ new

@@ -4,98 +4,91 @@ attr_accessor :host attr_accessor :port attr_accessor :args attr_accessor :timeout - def self.boot(*args) - new(*args).boot + class << self + def boot(*args) + new(*args).boot + end + + def jar_path + File.expand_path("#{File.dirname(__FILE__)}/../../vendor/selenium-server.jar") + end end def initialize(host, port = nil, options = {}) @host = host @port = port || 4444 @args = options[:args] || [] - @timeout = options[:timeout] + @timeout = options[:timeout] || 60 end def boot start wait - stop_at_exit + at_exit { stop } self end - def log(string) - puts string - end - def start - command = "java -jar \"#{jar_path}\"" + command = "java -jar \"#{self.class.jar_path}\"" command << " -port #{port}" command << " #{args.join(' ')}" unless args.empty? - log "Running: #{command}" begin fork do system(command) + at_exit { exit!(0) } end rescue NotImplementedError Thread.start do system(command) end end end - def stop_at_exit - at_exit do - stop - end - end - - def jar_path - File.expand_path("#{File.dirname(__FILE__)}/../../vendor/selenium-server.jar") - end - def wait - $stderr.print "==> Waiting for Selenium RC server on port #{port}... " + $stderr.print "==> Waiting for Selenium Server on port #{port}... " wait_for_service_with_timeout $stderr.print "Ready!\n" - rescue SocketError + rescue ServerNotStarted fail end def fail $stderr.puts $stderr.puts - $stderr.puts "==> Failed to boot the Selenium RC server... exiting!" + $stderr.puts "==> Failed to boot the Selenium Server... exiting!" exit end def stop - Net::HTTP.get(host, '/selenium-server/driver/?cmd=shutDownSeleniumServer', port) + selenium_command('shutDownSeleniumServer') end - def service_is_running? + def ready? begin - socket = TCPSocket.new(host, port) - socket.close unless socket.nil? - true - rescue Errno::ECONNREFUSED, - Errno::EBADF, # Windows - Errno::EADDRNOTAVAIL # Windows + selenium_command('testComplete') == 'OK' + rescue Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::EPIPE, Net::HTTPBadResponse false end end protected + + def selenium_command(command) + Net::HTTP.get(host, "/selenium-server/driver/?cmd=#{command}", port) + end + def wait_for_service_with_timeout start_time = Time.now - timeout = 60 - - until service_is_running? - if timeout && (Time.now > (start_time + timeout)) - raise SocketError.new("Socket did not open within #{timeout} seconds") + until ready? + if @timeout && (Time.now > (start_time + @timeout)) + raise ServerNotStarted.new("Selenium Server was not ready for connections after #{@timeout} seconds") end end end end + class ServerNotStarted < Exception + end end