Sha256: 07de530109e76a0b04bd2d7c06b20df0af086ca6e3bc1a2c4305fec7d58ab30d

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

module SeleniumRC

  class Server
    class << self
      def boot(*argv)
        new.boot(argv)
      end

      def host
        ENV["SELENIUM_SERVER_HOST"] || "0.0.0.0"
      end

      def port
        ENV["SELENIUM_SERVER_PORT"] || "4444"
      end

      def service_is_running?
        begin
          socket = TCPSocket.new(host, port)
          socket.close unless socket.nil?
          true
        rescue Errno::ECONNREFUSED,
               Errno::EBADF           # Windows
          false
        end
      end
    end

    def boot(*argv)
      start(argv)
      wait
      stop_at_exit
    end

    def log(string)
      puts string
    end

    def start(*argv)
      command = "java -jar \"#{jar_path}\""
      command << " -port #{self.class.port}"
      command << " #{argv.join(' ')}" if argv.length > 0
      log "Running: #{command}"
      system(command)
    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}... "
      wait_for_service_with_timeout
      $stderr.print "Ready!\n"
    rescue SocketError
      fail
    end

    def fail
      $stderr.puts
      $stderr.puts
      $stderr.puts "==> Failed to boot the Selenium RC server... exiting!"
      exit
    end

    def stop
      Net::HTTP.get(host, '/selenium-server/driver/?cmd=shutDown', port)
    end

    def host
      self.class.host
    end

    def port
      self.class.port
    end

    def service_is_running?
      self.class.service_is_running?
    end

    protected
    def wait_for_service_with_timeout
      start_time = Time.now
      timeout = 15

      until self.class.service_is_running?
        if timeout && (Time.now > (start_time + timeout))
          raise SocketError.new("Socket did not open within #{timeout} seconds")
        end
      end
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pivotal-selenium-rc-1.4.20090112 lib/selenium_rc/server.rb
pivotal-selenium-rc-1.5.20090112 lib/selenium_rc/server.rb
pivotal-selenium-rc-1.5.20090512 lib/selenium_rc/server.rb