2012-10-06 22:56:51 +01:00
|
|
|
require 'uri'
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
module Capybara::Poltergeist
|
|
|
|
class Driver < Capybara::Driver::Base
|
2012-02-29 12:32:13 +00:00
|
|
|
DEFAULT_TIMEOUT = 30
|
|
|
|
|
|
|
|
attr_reader :app, :app_server, :server, :client, :browser, :options
|
2011-10-27 23:34:14 +01:00
|
|
|
|
|
|
|
def initialize(app, options = {})
|
2012-02-29 12:32:13 +00:00
|
|
|
@app = app
|
|
|
|
@options = options
|
|
|
|
@browser = nil
|
|
|
|
@inspector = nil
|
|
|
|
@server = nil
|
|
|
|
@client = nil
|
2011-10-27 23:34:14 +01:00
|
|
|
|
2012-02-29 12:32:13 +00:00
|
|
|
@app_server = Capybara::Server.new(app)
|
|
|
|
@app_server.boot if Capybara.run_server
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def browser
|
2012-05-31 13:36:31 +01:00
|
|
|
@browser ||= Browser.new(server, client, logger, js_errors)
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-02-29 00:02:43 +00:00
|
|
|
def inspector
|
|
|
|
@inspector ||= options[:inspector] && Inspector.new(options[:inspector])
|
|
|
|
end
|
|
|
|
|
2012-02-29 12:32:13 +00:00
|
|
|
def server
|
2012-10-06 16:57:46 +01:00
|
|
|
@server ||= Server.new(
|
2012-10-15 22:44:27 +01:00
|
|
|
options.fetch(:port) { Util.find_available_port },
|
|
|
|
options.fetch(:timeout) { DEFAULT_TIMEOUT }
|
2012-10-06 16:57:46 +01:00
|
|
|
)
|
2012-02-29 12:32:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def client
|
2012-07-11 20:25:17 +01:00
|
|
|
@client ||= Client.start(server.port,
|
|
|
|
:path => options[:phantomjs],
|
|
|
|
:window_size => options[:window_size],
|
|
|
|
:phantomjs_options => phantomjs_options
|
|
|
|
)
|
2012-07-11 20:14:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def phantomjs_options
|
|
|
|
list = options[:phantomjs_options] || []
|
|
|
|
list += ["--remote-debugger-port=#{inspector.port}", "--remote-debugger-autorun=yes"] if inspector
|
|
|
|
list
|
2012-02-29 12:32:13 +00:00
|
|
|
end
|
|
|
|
|
2012-02-29 13:51:11 +00:00
|
|
|
def client_pid
|
|
|
|
client.pid
|
|
|
|
end
|
|
|
|
|
2012-02-29 12:32:13 +00:00
|
|
|
def timeout
|
|
|
|
server.timeout
|
|
|
|
end
|
|
|
|
|
|
|
|
def timeout=(sec)
|
|
|
|
server.timeout = sec
|
|
|
|
end
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
def restart
|
|
|
|
browser.restart
|
|
|
|
end
|
|
|
|
|
2012-02-29 13:51:11 +00:00
|
|
|
def quit
|
|
|
|
server.stop
|
|
|
|
client.stop
|
|
|
|
end
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
# logger should be an object that responds to puts, or nil
|
|
|
|
def logger
|
|
|
|
options[:logger] || (options[:debug] && STDERR)
|
|
|
|
end
|
|
|
|
|
2012-05-31 13:36:31 +01:00
|
|
|
def js_errors
|
|
|
|
options.fetch(:js_errors, true)
|
2012-05-12 18:03:39 +01:00
|
|
|
end
|
|
|
|
|
2012-02-29 12:32:13 +00:00
|
|
|
def visit(path)
|
2012-10-06 18:49:22 +01:00
|
|
|
browser.visit app_server.url(path)
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_url
|
|
|
|
browser.current_url
|
|
|
|
end
|
|
|
|
|
2012-06-04 08:58:27 +03:00
|
|
|
def status_code
|
|
|
|
browser.status_code
|
|
|
|
end
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
def body
|
|
|
|
browser.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def source
|
2012-01-10 00:25:30 +00:00
|
|
|
browser.source.to_s
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def find(selector)
|
2012-03-10 13:51:59 +00:00
|
|
|
browser.find(selector).map { |page_id, id| Capybara::Poltergeist::Node.new(self, page_id, id) }
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def evaluate_script(script)
|
|
|
|
browser.evaluate(script)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_script(script)
|
|
|
|
browser.execute(script)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2012-10-07 00:19:52 +01:00
|
|
|
def within_frame(name, &block)
|
|
|
|
browser.within_frame(name, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def within_window(name, &block)
|
|
|
|
browser.within_window(name, &block)
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset!
|
|
|
|
browser.reset
|
|
|
|
end
|
|
|
|
|
2011-10-31 23:04:02 +00:00
|
|
|
def render(path, options = {})
|
|
|
|
browser.render(path, options)
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2011-10-31 22:23:52 +00:00
|
|
|
def resize(width, height)
|
|
|
|
browser.resize(width, height)
|
|
|
|
end
|
2012-09-22 19:54:48 -07:00
|
|
|
alias_method :resize_window, :resize
|
2011-10-31 22:23:52 +00:00
|
|
|
|
2012-06-14 11:50:26 -07:00
|
|
|
def network_traffic
|
|
|
|
browser.network_traffic
|
2012-06-06 10:57:49 -07:00
|
|
|
end
|
|
|
|
|
2012-10-06 18:49:22 +01:00
|
|
|
def headers=(headers)
|
|
|
|
browser.set_headers(headers)
|
|
|
|
end
|
|
|
|
|
2012-10-06 19:27:35 +01:00
|
|
|
def response_headers
|
|
|
|
browser.response_headers
|
|
|
|
end
|
|
|
|
|
2012-10-06 22:56:51 +01:00
|
|
|
def cookies
|
|
|
|
browser.cookies
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_cookie(name, value, options = {})
|
|
|
|
browser.set_cookie({
|
2012-10-06 23:06:39 +01:00
|
|
|
:name => name,
|
|
|
|
:value => value,
|
|
|
|
:domain => URI.parse(app_server.url('')).host
|
2012-10-06 22:56:51 +01:00
|
|
|
}.merge(options))
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_cookie(name)
|
|
|
|
browser.remove_cookie(name)
|
|
|
|
end
|
|
|
|
|
2012-02-29 00:02:43 +00:00
|
|
|
def debug
|
2012-07-24 23:37:07 +03:00
|
|
|
if @options[:inspector]
|
|
|
|
inspector.open
|
|
|
|
pause
|
|
|
|
else
|
|
|
|
raise Error, "To use the remote debugging, you have to launch the driver " \
|
|
|
|
"with `:inspector => true` configuration option"
|
|
|
|
end
|
2012-02-29 00:02:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def pause
|
|
|
|
STDERR.puts "Poltergeist execution paused. Press enter to continue."
|
|
|
|
STDIN.gets
|
|
|
|
end
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
def wait?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_element_errors
|
2012-06-07 09:41:15 +01:00
|
|
|
[Capybara::Poltergeist::ObsoleteNode, Capybara::Poltergeist::ClickFailed]
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|