class Selenium::Server

Wraps the remote server jar

Public Class Methods

new(jar, opts = {}) click to toggle source
# File lib/selenium/server.rb, line 13
def initialize(jar, opts = {})
  raise Errno::ENOENT, jar unless File.exist?(jar)

  @jar        = jar
  @host       = "127.0.0.1"
  @port       = opts.fetch(:port, 4444)
  @timeout    = opts.fetch(:timeout, 30)
  @background = opts.fetch(:background, false)
  @log        = opts[:log]

  @additional_args = []
end

Public Instance Methods

<<(arg) click to toggle source
# File lib/selenium/server.rb, line 55
def <<(arg)
  if arg.kind_of?(Array)
    @additional_args += arg
  else
    @additional_args << arg.to_s
  end
end
start() click to toggle source
# File lib/selenium/server.rb, line 26
def start
  process.start
  poll_for_service

  unless @background
    begin
      sleep 1 while process.alive?
    rescue Errno::ECHILD
      # no longer alive
    end
  end
end
stop() click to toggle source
# File lib/selenium/server.rb, line 39
def stop
  begin
    Net::HTTP.get(@host, "/selenium-server/driver/?cmd=shutDownSeleniumServer", @port)
  rescue Errno::ECONNREFUSED
  end

  stop_process if @process
  poll_for_shutdown

  @log_file.close if @log_file
end
webdriver_url() click to toggle source
# File lib/selenium/server.rb, line 51
def webdriver_url
  "http://#{@host}:#{@port}/wd/hub"
end

Private Instance Methods

poll_for_service() click to toggle source
# File lib/selenium/server.rb, line 97
def poll_for_service
  unless socket.connected?
    raise "remote server not launched in #{@timeout} seconds"
  end
end
poll_for_shutdown() click to toggle source
# File lib/selenium/server.rb, line 103
def poll_for_shutdown
  unless socket.closed?
    raise "remote server not stopped in #{@timeout} seconds"
  end
end
process() click to toggle source
# File lib/selenium/server.rb, line 79
def process
  @process ||= (
    cp = ChildProcess.build("java", "-jar", @jar, "-port", @port.to_s, *@additional_args)
    io = cp.io

    if @log.kind_of?(String) && !@background
      @log_file = File.open(@log, "w")
      io.stdout = io.stderr = @log_file
    elsif @log
      io.inherit!
    end

    cp.detach = @background

    cp
  )
end
socket() click to toggle source
# File lib/selenium/server.rb, line 109
def socket
  @socket ||= WebDriver::SocketPoller.new(@host, @port, @timeout)
end
stop_process() click to toggle source
# File lib/selenium/server.rb, line 65
def stop_process
  return unless @process.alive?

  begin
    @process.poll_for_exit(5)
  rescue ChildProcess::TimeoutError
    @process.stop
  end
rescue Errno::ECHILD
  # already dead
ensure
  @process = nil
end