2012-03-11 10:56:19 +00:00
|
|
|
require 'multi_json'
|
2011-10-27 23:34:14 +01:00
|
|
|
|
|
|
|
module Capybara::Poltergeist
|
|
|
|
class Browser
|
2012-05-31 13:36:31 +01:00
|
|
|
attr_reader :server, :client, :logger, :js_errors
|
2011-10-27 23:34:14 +01:00
|
|
|
|
2012-05-31 13:36:31 +01:00
|
|
|
def initialize(server, client, logger = nil, js_errors = true)
|
2012-05-31 13:38:25 +01:00
|
|
|
@server = server
|
|
|
|
@client = client
|
|
|
|
@logger = logger
|
|
|
|
@js_errors = js_errors
|
2012-01-13 13:10:30 +00:00
|
|
|
end
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
def restart
|
|
|
|
server.restart
|
|
|
|
client.restart
|
|
|
|
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
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def find(selector)
|
|
|
|
result = command('find', selector)
|
|
|
|
result['ids'].map { |id| [result['page_id'], id] }
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def find_within(page_id, id, selector)
|
|
|
|
command 'find_within', page_id, id, selector
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
|
2012-03-10 13:51:59 +00:00
|
|
|
def text(page_id, id)
|
|
|
|
command '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
|
|
|
|
|
|
|
|
def evaluate(script)
|
|
|
|
command 'evaluate', script
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(script)
|
|
|
|
command 'execute', script
|
|
|
|
end
|
|
|
|
|
|
|
|
def within_frame(id, &block)
|
|
|
|
command 'push_frame', id
|
2012-07-11 00:16:50 +01:00
|
|
|
yield
|
|
|
|
ensure
|
2011-10-27 23:34:14 +01:00
|
|
|
command 'pop_frame'
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
def command(name, *args)
|
|
|
|
message = { 'name' => name, 'args' => args }
|
|
|
|
log message.inspect
|
|
|
|
|
2012-05-31 13:53:40 +01:00
|
|
|
json = JSON.load(server.send(JSON.dump(message)))
|
2011-10-27 23:34:14 +01:00
|
|
|
log json.inspect
|
|
|
|
|
|
|
|
if json['error']
|
2012-02-29 20:38:01 +00:00
|
|
|
if json['error']['name'] == 'Poltergeist.JavascriptError'
|
2012-05-31 12:48:24 +01:00
|
|
|
error = JavascriptError.new(json['error'])
|
2012-05-31 13:36:31 +01:00
|
|
|
if js_errors
|
2012-05-31 12:48:24 +01:00
|
|
|
raise error
|
|
|
|
else
|
|
|
|
log error
|
|
|
|
end
|
2012-02-29 20:38:01 +00:00
|
|
|
else
|
2012-05-31 12:48:24 +01:00
|
|
|
raise BrowserError.new(json['error'])
|
2012-02-29 20:38:01 +00:00
|
|
|
end
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
2012-05-12 18:03:39 +01:00
|
|
|
json['response']
|
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
|