1
0
Fork 0
mirror of https://github.com/teampoltergeist/poltergeist.git synced 2022-11-09 12:05:00 -05:00
teampoltergeist--poltergeist/lib/capybara/poltergeist/client.rb

50 lines
969 B
Ruby
Raw Normal View History

require 'sfl'
2011-10-27 23:34:14 +01:00
module Capybara::Poltergeist
class Client
PHANTOMJS_SCRIPT = File.expand_path('../client/compiled/main.js', __FILE__)
2012-02-25 11:38:13 +00:00
PHANTOMJS_VERSION = '1.4.1'
PHANTOMJS_NAME = 'phantomjs'
2011-10-27 23:34:14 +01:00
attr_reader :pid, :port, :path
def self.start(*args)
client = new(*args)
client.start
client
end
2011-10-27 23:34:14 +01:00
def initialize(port, path = nil)
@port = port
2012-02-25 11:38:13 +00:00
@path = path || PHANTOMJS_NAME
2011-10-27 23:34:14 +01:00
at_exit { stop }
end
def start
check_phantomjs_version
@pid = Kernel.spawn("#{path} #{PHANTOMJS_SCRIPT} #{port}")
2011-10-27 23:34:14 +01:00
end
def stop
Process.kill('TERM', pid) if pid
2011-10-27 23:34:14 +01:00
end
def restart
stop
start
end
private
def check_phantomjs_version
return if @phantomjs_version_checked
version = `#{path} --version`.chomp
if version < PHANTOMJS_VERSION
raise PhantomJSTooOld.new(version)
end
@phantomjs_version_checked = true
end
2011-10-27 23:34:14 +01:00
end
end