2011-10-28 20:18:45 +01:00
|
|
|
require 'sfl'
|
2011-10-27 23:34:14 +01:00
|
|
|
|
|
|
|
module Capybara::Poltergeist
|
|
|
|
class Client
|
2012-02-25 12:47:18 +00:00
|
|
|
INSPECTOR_BROWSERS = %w(chromium chromium-browser google-chrome safari)
|
|
|
|
PHANTOMJS_SCRIPT = File.expand_path('../client/compiled/main.js', __FILE__)
|
|
|
|
PHANTOMJS_VERSION = '1.4.1'
|
|
|
|
PHANTOMJS_NAME = 'phantomjs'
|
2011-10-27 23:34:14 +01:00
|
|
|
|
2012-02-25 12:47:18 +00:00
|
|
|
def self.inspector_browser
|
|
|
|
@inspector_browser ||= INSPECTOR_BROWSERS.find do |name|
|
|
|
|
system "which #{name} &>/dev/null"
|
|
|
|
end
|
|
|
|
end
|
2012-01-27 17:31:21 +00:00
|
|
|
|
|
|
|
def self.start(*args)
|
|
|
|
client = new(*args)
|
|
|
|
client.start
|
|
|
|
client
|
|
|
|
end
|
2011-10-27 23:34:14 +01:00
|
|
|
|
2012-02-25 12:47:18 +00:00
|
|
|
attr_reader :pid, :port, :path, :inspector
|
|
|
|
|
|
|
|
def initialize(port, inspector = false, path = nil)
|
|
|
|
@port = port
|
|
|
|
@inspector = inspector
|
|
|
|
@path = path || PHANTOMJS_NAME
|
2011-10-27 23:34:14 +01:00
|
|
|
at_exit { stop }
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
2012-01-27 17:31:21 +00:00
|
|
|
check_phantomjs_version
|
2012-02-25 12:47:18 +00:00
|
|
|
@pid = Kernel.spawn(command)
|
|
|
|
|
|
|
|
# Opens a remote debugger for the phantomjs session. This feature
|
|
|
|
# is unfinished / experimental. When the debugger opens, you have
|
|
|
|
# to type __run() into the console to get it going.
|
|
|
|
Kernel.spawn(inspector_command) if inspector
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
2012-01-27 17:31:21 +00:00
|
|
|
Process.kill('TERM', pid) if pid
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def restart
|
|
|
|
stop
|
|
|
|
start
|
|
|
|
end
|
2012-01-27 17:31:21 +00:00
|
|
|
|
2012-02-25 12:47:18 +00:00
|
|
|
def command
|
|
|
|
@command ||= begin
|
|
|
|
parts = [path]
|
|
|
|
parts << "--remote-debugger-port=#{inspector_port}" if inspector
|
|
|
|
parts << PHANTOMJS_SCRIPT
|
|
|
|
parts << port
|
|
|
|
parts.join(" ")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspector_port
|
|
|
|
@inspector_port ||= Util.find_available_port
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspector_command
|
|
|
|
"#{inspector_browser} http://localhost:#{inspector_port}/webkit/inspector/inspector.html?page=1"
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspector_browser
|
|
|
|
if inspector == true
|
|
|
|
self.class.inspector_browser or raise "webkit browser not found; please specify it explicitly"
|
|
|
|
else
|
|
|
|
inspector
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-27 17:31:21 +00:00
|
|
|
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
|