class Selenium::WebDriver::Chrome::Service

@api private

Constants

DEFAULT_PORT
MISSING_TEXT
SOCKET_LOCK_TIMEOUT
START_TIMEOUT
STOP_TIMEOUT

Public Class Methods

default_service(*extra_args) click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 49
def self.default_service(*extra_args)
  new executable_path, DEFAULT_PORT, *extra_args
end
executable_path() click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 34
def self.executable_path
  @executable_path ||= (
    path = Platform.find_binary "chromedriver"
    path or raise Error::WebDriverError, MISSING_TEXT
    Platform.assert_executable path

    path
  )
end
executable_path=(path) click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 44
def self.executable_path=(path)
  Platform.assert_executable path
  @executable_path = path
end
new(executable_path, port, *extra_args) click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 53
def initialize(executable_path, port, *extra_args)
  @executable_path = executable_path
  @host            = Platform.localhost
  @port            = Integer(port)

  raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1

  @extra_args = extra_args
end

Public Instance Methods

start() click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 63
def start
  Platform.exit_hook { stop } # make sure we don't leave the server running

  socket_lock.locked do
    find_free_port
    start_process
    connect_until_stable
  end
end
stop() click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 73
def stop
  return if @process.nil? || @process.exited?

  Net::HTTP.start(@host, @port) do |http|
    http.open_timeout = STOP_TIMEOUT / 2
    http.read_timeout = STOP_TIMEOUT / 2

    http.get("/shutdown")
  end
ensure
  stop_process
end
uri() click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 86
def uri
  URI.parse "http://#{@host}:#{@port}"
end

Private Instance Methods

connect_until_stable() click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 110
def connect_until_stable
  socket_poller = SocketPoller.new @host, @port, START_TIMEOUT

  unless socket_poller.connected?
    raise Error::WebDriverError, "unable to connect to chromedriver #{@host}:#{@port}"
  end
end
find_free_port() click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 92
def find_free_port
  @port = PortProber.above @port
end
socket_lock() click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 118
def socket_lock
  @socket_lock ||= SocketLock.new(@port - 1, SOCKET_LOCK_TIMEOUT)
end
start_process() click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 96
def start_process
  server_command = [@executable_path, "--port=#{@port}", *@extra_args]
  @process       = ChildProcess.build(*server_command)

  @process.io.inherit! if $DEBUG == true
  @process.start
end
stop_process() click to toggle source
# File lib/selenium/webdriver/chrome/service.rb, line 104
def stop_process
  @process.poll_for_exit STOP_TIMEOUT
rescue ChildProcess::TimeoutError
  @process.stop STOP_TIMEOUT
end