lib/capybara/poltergeist/browser.rb in poltergeist-0.6.0 vs lib/capybara/poltergeist/browser.rb in poltergeist-0.7.0
- old
+ new
@@ -1,30 +1,35 @@
require 'multi_json'
module Capybara::Poltergeist
class Browser
- attr_reader :server, :client, :logger
+ attr_reader :server, :client, :logger, :js_errors
- def initialize(server, client, logger = nil)
- @server = server
- @client = client
- @logger = logger
+ def initialize(server, client, logger = nil, js_errors = true)
+ @server = server
+ @client = client
+ @logger = logger
+ @js_errors = js_errors
end
def restart
server.restart
client.restart
end
- def visit(url)
- command 'visit', url
+ def visit(url, headers)
+ command 'visit', url, headers
end
def current_url
command 'current_url'
end
+ def status_code
+ command 'status_code'
+ end
+
def body
command 'body'
end
def source
@@ -77,10 +82,11 @@
end
def within_frame(id, &block)
command 'push_frame', id
yield
+ ensure
command 'pop_frame'
end
def click(page_id, id)
command 'click', page_id, id
@@ -101,32 +107,49 @@
def reset
command 'reset'
end
def render(path, options = {})
- command 'render', path, !!options[:full]
+ command 'render', path.to_s, !!options[:full]
end
def resize(width, height)
command 'resize', width, height
end
+ def network_traffic
+ command('network_traffic').values.map do |event|
+ NetworkTraffic::Request.new(
+ event['request'],
+ event['responseParts'].map { |response| NetworkTraffic::Response.new(response) }
+ )
+ end
+ end
+
+ def equals(page_id, id, other_id)
+ command('equals', page_id, id, other_id)
+ end
+
def command(name, *args)
message = { 'name' => name, 'args' => args }
log message.inspect
- json = MultiJson.decode(server.send(MultiJson.encode(message)))
+ json = JSON.load(server.send(JSON.dump(message)))
log json.inspect
if json['error']
if json['error']['name'] == 'Poltergeist.JavascriptError'
- raise JavascriptError.new(json['error'])
+ error = JavascriptError.new(json['error'])
+ if js_errors
+ raise error
+ else
+ log error
+ end
else
raise BrowserError.new(json['error'])
end
- else
- json['response']
end
+ json['response']
rescue DeadClient
restart
raise
end