lib/capybara/poltergeist/browser.rb in poltergeist-0.4.0 vs lib/capybara/poltergeist/browser.rb in poltergeist-0.5.0

- old
+ new

@@ -1,33 +1,23 @@ -require 'json' +require 'multi_json' module Capybara::Poltergeist class Browser - attr_reader :options, :server, :client + attr_reader :server, :client, :logger - DEFAULT_TIMEOUT = 30 - - def initialize(options = {}) - @options = options - @server = Server.new(options.fetch(:timeout, DEFAULT_TIMEOUT)) - @client = Client.start(server.port, options[:phantomjs]) + def initialize(server, client, logger = nil) + @server = server + @client = client + @logger = logger end - def timeout - server.timeout - end - - def timeout=(sec) - server.timeout = sec - end - def restart server.restart client.restart end - def visit(url, attributes = {}) + def visit(url) command 'visit', url end def current_url command 'current_url' @@ -39,42 +29,47 @@ def source command 'source' end - def find(selector, id = nil) - command 'find', selector, id + def find(selector) + result = command('find', selector) + result['ids'].map { |id| [result['page_id'], id] } end - def text(id) - command 'text', id + def find_within(page_id, id, selector) + command 'find_within', page_id, id, selector end - def attribute(id, name) - command 'attribute', id, name + def text(page_id, id) + command 'text', page_id, id end - def value(id) - command 'value', id + def attribute(page_id, id, name) + command 'attribute', page_id, id, name.to_s end - def set(id, value) - command 'set', id, value + def value(page_id, id) + command 'value', page_id, id end - def select_file(id, value) - command 'select_file', id, value + def set(page_id, id, value) + command 'set', page_id, id, value end - def tag_name(id) - command('tag_name', id).downcase + def select_file(page_id, id, value) + command 'select_file', page_id, id, value end - def visible?(id) - command 'visible', id + def tag_name(page_id, id) + command('tag_name', page_id, id).downcase end + def visible?(page_id, id) + command 'visible', page_id, id + end + def evaluate(script) command 'evaluate', script end def execute(script) @@ -85,30 +80,26 @@ command 'push_frame', id yield command 'pop_frame' end - def reset - visit('about:blank') + def click(page_id, id) + command 'click', page_id, id end - def click(id) - command 'click', id + def drag(page_id, id, other_id) + command 'drag', page_id, id, other_id end - def drag(id, other_id) - command 'drag', id, other_id + def select(page_id, id, value) + command 'select', page_id, id, value end - def select(id, value) - command 'select', id, value + def trigger(page_id, id, event) + command 'trigger', page_id, id, event.to_s end - def trigger(id, event) - command 'trigger', id, event - end - def reset command 'reset' end def render(path, options = {}) @@ -117,32 +108,34 @@ def resize(width, height) command 'resize', width, height end - def logger - options[:logger] - end - - def log(message) - logger.puts message if logger - end - def command(name, *args) message = { 'name' => name, 'args' => args } log message.inspect - json = JSON.parse(server.send(JSON.generate(message))) + json = MultiJson.decode(server.send(MultiJson.encode(message))) log json.inspect if json['error'] - raise BrowserError.new(json['error']) + if json['error']['name'] == 'Poltergeist.JavascriptError' + raise JavascriptError.new(json['error']) + else + raise BrowserError.new(json['error']) + end else json['response'] end rescue DeadClient restart raise + end + + private + + def log(message) + logger.puts message if logger end end end