lib/capybara/poltergeist/browser.rb in poltergeist-1.1.2 vs lib/capybara/poltergeist/browser.rb in poltergeist-1.2.0

- old
+ new

@@ -1,10 +1,16 @@ +require "capybara/poltergeist/errors" require 'json' require 'time' module Capybara::Poltergeist class Browser + ERROR_MAPPINGS = { + "Poltergeist.JavascriptError" => JavascriptError, + "Poltergeist.FrameNotFound" => FrameNotFound + } + attr_reader :server, :client, :logger def initialize(server, client, logger = nil) @server = server @client = client @@ -36,23 +42,31 @@ def source command 'source' end - def find(selector) - result = command('find', selector) + def title + command 'title' + end + + def find(method, selector) + result = command('find', method, selector) result['ids'].map { |id| [result['page_id'], id] } end - def find_within(page_id, id, selector) - command 'find_within', page_id, id, selector + def find_within(page_id, id, method, selector) + command 'find_within', page_id, id, method, selector end - def text(page_id, id) - command 'text', page_id, id + 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 + end + def attribute(page_id, id, name) command 'attribute', page_id, id, name.to_s end def value(page_id, id) @@ -73,10 +87,14 @@ def visible?(page_id, id) command 'visible', page_id, id end + def disabled?(page_id, id) + command 'disabled', page_id, id + end + def click_coordinates(x, y) command 'click_coordinates', x, y end def evaluate(script) @@ -85,12 +103,17 @@ def execute(script) command 'execute', script end - def within_frame(name, &block) - command 'push_frame', name + def within_frame(handle, &block) + if handle.is_a?(Capybara::Node::Base) + command 'push_frame', handle['id'] + else + command 'push_frame', handle + end + yield ensure command 'pop_frame' end @@ -107,10 +130,14 @@ def double_click(page_id, id) command 'double_click', page_id, id end + def hover(page_id, id) + command 'hover', page_id, id + end + def drag(page_id, id, other_id) command 'drag', page_id, id, other_id end def select(page_id, id, value) @@ -191,17 +218,14 @@ json = JSON.load(server.send(JSON.generate(message))) log json.inspect if json['error'] - if json['error']['name'] == 'Poltergeist.JavascriptError' - raise JavascriptError.new(json['error']) - else - raise BrowserError.new(json['error']) - end + klass = ERROR_MAPPINGS[json['error']['name']] || BrowserError + raise klass.new(json['error']) + else + json['response'] end - json['response'] - rescue DeadClient restart raise end