2013-03-03 14:04:39 +00:00
|
|
|
require "capybara/poltergeist/errors"
|
2013-01-26 15:45:56 +00:00
|
|
|
require 'json'
|
2012-10-06 22:56:51 +01:00
|
|
|
require 'time'
|
2011-10-27 23:34:14 +01:00
|
|
|
|
|
|
|
module Capybara::Poltergeist
|
|
|
|
class Browser
|
2013-03-03 14:04:39 +00:00
|
|
|
ERROR_MAPPINGS = {
|
|
|
|
"Poltergeist.JavascriptError" => JavascriptError,
|
|
|
|
"Poltergeist.FrameNotFound" => FrameNotFound
|
|
|
|
}
|
|
|
|
|
2013-01-26 21:11:44 +00:00
|
|
|
attr_reader :server, :client, :logger
|
2011-10-27 23:34:14 +01:00
|
|
|
|
2013-01-26 21:11:44 +00:00
|
|
|
def initialize(server, client, logger = nil)
|
|
|
|
@server = server
|
|
|
|
@client = client
|
|
|
|
@logger = logger
|
2012-01-13 13:10:30 +00:00
|
|
|
end
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
def restart
|
|
|
|
server.restart
|
|
|
|
client.restart
|
2013-01-31 21:45:10 +00:00
|
|
|
|
|
|
|
self.debug = @debug if @debug
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-10-06 18:49:22 +01:00
|
|
|
def visit(url)
|
|
|
|
command 'visit', url
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_url
|
|
|
|
command 'current_url'
|
|
|
|
end
|
|
|
|
|
2012-06-04 08:58:27 +03:00
|
|
|
def status_code
|
|
|
|
command 'status_code'
|
|
|
|
end
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
def body
|
|
|
|
command 'body'
|
|
|
|
end
|
|
|
|
|
|
|
|
def source
|
|
|
|
command 'source'
|
|
|
|
end
|
|
|
|
|
2013-03-03 14:21:12 +00:00
|
|
|
def title
|
|
|
|
command 'title'
|
|
|
|
end
|
|
|
|
|
2013-03-02 14:05:48 +00:00
|
|
|
def find(method, selector)
|
|
|
|
result = command('find', method, selector)
|
2012-03-10 13:51:59 +00:00
|
|
|
result['ids'].map { |id| [result['page_id'], id] }
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2013-03-02 14:50:22 +00:00
|
|
|
def find_within(page_id, id, method, selector)
|
|
|
|
command 'find_within', page_id, id, method, selector
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2013-03-03 14:21:12 +00:00
|
|
|
def all_text(page_id, id)
|
|
|
|
command 'all_text', page_id, id
|
|
|
|
end
|
|
|
|
|
|
|
|
def visible_text(page_id, id)
|
|
|
|
command 'visible_text', page_id, id
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def attribute(page_id, id, name)
|
2012-03-11 11:39:53 +00:00
|
|
|
command 'attribute', page_id, id, name.to_s
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def value(page_id, id)
|
|
|
|
command 'value', page_id, id
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def set(page_id, id, value)
|
|
|
|
command 'set', page_id, id, value
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def select_file(page_id, id, value)
|
|
|
|
command 'select_file', page_id, id, value
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def tag_name(page_id, id)
|
|
|
|
command('tag_name', page_id, id).downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def visible?(page_id, id)
|
|
|
|
command 'visible', page_id, id
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-10-08 13:57:31 -07:00
|
|
|
def click_coordinates(x, y)
|
|
|
|
command 'click_coordinates', x, y
|
|
|
|
end
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
def evaluate(script)
|
|
|
|
command 'evaluate', script
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(script)
|
|
|
|
command 'execute', script
|
|
|
|
end
|
|
|
|
|
2013-03-03 15:42:40 +00:00
|
|
|
def within_frame(handle, &block)
|
|
|
|
if handle.is_a?(Capybara::Node::Base)
|
|
|
|
command 'push_frame', handle['id']
|
|
|
|
else
|
|
|
|
command 'push_frame', handle
|
|
|
|
end
|
|
|
|
|
2012-07-11 00:16:50 +01:00
|
|
|
yield
|
|
|
|
ensure
|
2011-10-27 23:34:14 +01:00
|
|
|
command 'pop_frame'
|
|
|
|
end
|
|
|
|
|
2012-10-07 00:19:52 +01:00
|
|
|
def within_window(name, &block)
|
|
|
|
command 'push_window', name
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
command 'pop_window'
|
|
|
|
end
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def click(page_id, id)
|
|
|
|
command 'click', page_id, id
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2013-01-05 22:25:51 +11:00
|
|
|
def double_click(page_id, id)
|
|
|
|
command 'double_click', page_id, id
|
|
|
|
end
|
|
|
|
|
2013-03-03 15:18:05 +00:00
|
|
|
def hover(page_id, id)
|
|
|
|
command 'hover', page_id, id
|
|
|
|
end
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def drag(page_id, id, other_id)
|
|
|
|
command 'drag', page_id, id, other_id
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def select(page_id, id, value)
|
|
|
|
command 'select', page_id, id, value
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def trigger(page_id, id, event)
|
2012-03-11 11:47:40 +00:00
|
|
|
command 'trigger', page_id, id, event.to_s
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
command 'reset'
|
|
|
|
end
|
|
|
|
|
2011-10-31 23:04:02 +00:00
|
|
|
def render(path, options = {})
|
2012-06-14 14:27:51 -04:00
|
|
|
command 'render', path.to_s, !!options[:full]
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2011-10-31 22:23:52 +00:00
|
|
|
def resize(width, height)
|
|
|
|
command 'resize', width, height
|
|
|
|
end
|
|
|
|
|
2012-06-14 11:50:26 -07:00
|
|
|
def network_traffic
|
2012-07-02 23:56:56 +01:00
|
|
|
command('network_traffic').values.map do |event|
|
|
|
|
NetworkTraffic::Request.new(
|
|
|
|
event['request'],
|
|
|
|
event['responseParts'].map { |response| NetworkTraffic::Response.new(response) }
|
|
|
|
)
|
2012-06-14 11:50:26 -07:00
|
|
|
end
|
2012-06-06 10:57:49 -07:00
|
|
|
end
|
|
|
|
|
2012-07-12 23:33:45 +01:00
|
|
|
def equals(page_id, id, other_id)
|
|
|
|
command('equals', page_id, id, other_id)
|
|
|
|
end
|
|
|
|
|
2012-10-06 18:49:22 +01:00
|
|
|
def set_headers(headers)
|
|
|
|
command 'set_headers', headers
|
|
|
|
end
|
|
|
|
|
2012-10-06 19:27:35 +01:00
|
|
|
def response_headers
|
|
|
|
command 'response_headers'
|
|
|
|
end
|
|
|
|
|
2012-10-06 22:56:51 +01:00
|
|
|
def cookies
|
|
|
|
Hash[command('cookies').map { |cookie| [cookie['name'], Cookie.new(cookie)] }]
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_cookie(cookie)
|
2013-01-26 21:38:19 +00:00
|
|
|
if cookie[:expires]
|
|
|
|
cookie[:expires] = cookie[:expires].to_i * 1000
|
2012-10-06 22:56:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
command 'set_cookie', cookie
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_cookie(name)
|
|
|
|
command 'remove_cookie', name
|
|
|
|
end
|
|
|
|
|
2013-01-26 21:11:44 +00:00
|
|
|
def js_errors=(val)
|
|
|
|
command 'set_js_errors', !!val
|
|
|
|
end
|
|
|
|
|
2012-11-01 12:32:40 +00:00
|
|
|
def extensions=(names)
|
|
|
|
Array(names).each do |name|
|
|
|
|
command 'add_extension', name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-31 21:45:10 +00:00
|
|
|
def debug=(val)
|
|
|
|
@debug = val
|
|
|
|
command 'set_debug', !!val
|
|
|
|
end
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
def command(name, *args)
|
|
|
|
message = { 'name' => name, 'args' => args }
|
|
|
|
log message.inspect
|
|
|
|
|
2013-01-26 15:45:56 +00:00
|
|
|
json = JSON.load(server.send(JSON.generate(message)))
|
2011-10-27 23:34:14 +01:00
|
|
|
log json.inspect
|
|
|
|
|
|
|
|
if json['error']
|
2013-03-03 14:04:39 +00:00
|
|
|
klass = ERROR_MAPPINGS[json['error']['name']] || BrowserError
|
|
|
|
raise klass.new(json['error'])
|
|
|
|
else
|
|
|
|
json['response']
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
2011-10-30 22:48:56 +00:00
|
|
|
rescue DeadClient
|
|
|
|
restart
|
|
|
|
raise
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
2012-02-29 12:32:13 +00:00
|
|
|
|
|
|
|
private
|
2012-07-12 23:33:45 +01:00
|
|
|
|
2012-02-29 12:32:13 +00:00
|
|
|
def log(message)
|
|
|
|
logger.puts message if logger
|
|
|
|
end
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
end
|