class Selenium::WebDriver::PhantomJS::Service

@api private

Constants

DEFAULT_PORT
MISSING_TEXT
START_TIMEOUT
STOP_TIMEOUT

Attributes

uri[R]

Public Class Methods

default_service(port = nil) click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 27
def self.default_service(port = nil)
  new executable_path, port || PortProber.above(DEFAULT_PORT)
end
executable_path() click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 17
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 31
def initialize(executable_path, port)
  @uri        = URI.parse "http://#{Platform.localhost}:#{port}"
  @executable = executable_path
end

Public Instance Methods

create_process(args) click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 73
def create_process(args)
  server_command = [@executable, "--webdriver=#{@uri.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
end
start(args = []) click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 36
def start(args = [])
  if @process && @process.alive?
    raise "already started: #{@uri.inspect} #{@executable.inspect}"
  end

  @process = create_process(args)
  @process.start

  socket_poller = SocketPoller.new Platform.localhost, @uri.port, START_TIMEOUT

  unless socket_poller.connected?
    raise Error::WebDriverError, "unable to connect to phantomjs @ #{@uri} after #{START_TIMEOUT} seconds"
  end

  Platform.exit_hook { stop } # make sure we don't leave the server running
end
stop() click to toggle source
# File lib/selenium/webdriver/phantomjs/service.rb, line 53
def stop
  return if @process.nil? || @process.exited?

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

    http.get("/shutdown")
  end

  @process.poll_for_exit STOP_TIMEOUT
rescue ChildProcess::TimeoutError
  # ok, force quit
  @process.stop STOP_TIMEOUT

  if Platform.jruby? && !$DEBUG
    @process.io.close rescue nil
  end
end