class Selenium::WebDriver::PhantomJS::Service

@api private

Constants

DEFAULT_PORT
MISSING_TEXT
SOCKET_LOCK_TIMEOUT
START_TIMEOUT
STOP_TIMEOUT

Public Class Methods

default_service(port = nil) click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 44
def self.default_service(port = nil)
  new executable_path, DEFAULT_PORT
end
executable_path() click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 34
def self.executable_path
  @executable_path ||= (
    path = PhantomJS.path
    path or raise Error::WebDriverError, MISSING_TEXT
    Platform.assert_executable path

    path
  )
end
new(executable_path, port) click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 48
def initialize(executable_path, port)
  @host       = Platform.localhost
  @executable = executable_path
  @port       = Integer(port)
end

Public Instance Methods

find_free_port() click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 84
def find_free_port
  @port = PortProber.above @port
end
start(args = []) click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 54
def start(args = [])
  if @process && @process.alive?
    raise "already started: #{uri.inspect} #{@executable.inspect}"
  end

  Platform.exit_hook { stop } # make sure we don't leave the server running

  socket_lock.locked do
    find_free_port
    start_process(args)
    connect_until_stable
  end
end
stop() click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 68
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
  if Platform.jruby? && !$DEBUG
    @process.io.close rescue nil
  end
end
uri() click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 88
def uri
  URI.parse "http://#{@host}:#{@port}"
end

Private Instance Methods

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

  unless socket_poller.connected?
    raise Error::WebDriverError, "unable to connect to phantomjs @ #{uri} after #{START_TIMEOUT} seconds"
  end
end
socket_lock() click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 122
def socket_lock
  @socket_lock ||= SocketLock.new(@port - 1, SOCKET_LOCK_TIMEOUT)
end
start_process(args) click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 94
def start_process(args)
  server_command = [@executable, "--webdriver=#{@port}", *args]
  @process = ChildProcess.build(*server_command.compact)

  if $DEBUG == true
    @process.io.inherit!
  elsif Platform.jruby?
    # apparently we need to read the output for phantomjs to work on jruby
    @process.io.stdout = @process.io.stderr = File.new(Platform.null_device, 'w')
  end

  @process.start
end
stop_process() click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 108
def stop_process
  @process.poll_for_exit STOP_TIMEOUT
rescue ChildProcess::TimeoutError
  @process.stop STOP_TIMEOUT
end