lib/capybara/poltergeist/browser.rb in poltergeist-1.5.1 vs lib/capybara/poltergeist/browser.rb in poltergeist-1.6.0
- old
+ new
@@ -3,13 +3,15 @@
require 'time'
module Capybara::Poltergeist
class Browser
ERROR_MAPPINGS = {
- "Poltergeist.JavascriptError" => JavascriptError,
- "Poltergeist.FrameNotFound" => FrameNotFound,
- "Poltergeist.InvalidSelector" => InvalidSelector
+ 'Poltergeist.JavascriptError' => JavascriptError,
+ 'Poltergeist.FrameNotFound' => FrameNotFound,
+ 'Poltergeist.InvalidSelector' => InvalidSelector,
+ 'Poltergeist.StatusFailError' => StatusFailError,
+ 'Poltergeist.NoSuchWindowError' => NoSuchWindowError
}
attr_reader :server, :client, :logger
def initialize(server, client, logger = nil)
@@ -47,10 +49,14 @@
def title
command 'title'
end
+ def parents(page_id, id)
+ command 'parents', page_id, id
+ end
+
def find(method, selector)
result = command('find', method, selector)
result['ids'].map { |id| [result['page_id'], id] }
end
@@ -68,10 +74,14 @@
def delete_text(page_id, id)
command 'delete_text', page_id, id
end
+ def attributes(page_id, id)
+ command 'attributes', page_id, id
+ end
+
def attribute(page_id, id, name)
command 'attribute', page_id, id, name.to_s
end
def value(page_id, id)
@@ -120,25 +130,49 @@
yield
ensure
command 'pop_frame'
end
+ def window_handle
+ command 'window_handle'
+ end
+
def window_handles
- command 'pages'
+ command 'window_handles'
end
+ def switch_to_window(handle)
+ command 'switch_to_window', handle
+ end
+
+ def open_new_window
+ command 'open_new_window'
+ end
+
+ def close_window(handle)
+ command 'close_window', handle
+ end
+
def within_window(name, &block)
- command 'push_window', name
+ original = window_handle
+ handle = command 'window_handle', name
+ handle = name if handle.nil? && window_handles.include?(name)
+ raise NoSuchWindowError unless handle
+ switch_to_window(handle)
yield
ensure
- command 'pop_window'
+ switch_to_window(original)
end
def click(page_id, id)
command 'click', page_id, id
end
+ def right_click(page_id, id)
+ command 'right_click', page_id, id
+ end
+
def double_click(page_id, id)
command 'double_click', page_id, id
end
def hover(page_id, id)
@@ -173,10 +207,14 @@
def render_base64(format, options = {})
check_render_options!(options)
command 'render_base64', format.to_s, !!options[:full], options[:selector]
end
+ def set_zoom_factor(zoom_factor)
+ command 'set_zoom_factor', zoom_factor
+ end
+
def set_paper_size(size)
command 'set_paper_size', size
end
def resize(width, height)
@@ -238,10 +276,14 @@
def remove_cookie(name)
command 'remove_cookie', name
end
+ def clear_cookies
+ command 'clear_cookies'
+ end
+
def cookies_enabled=(flag)
command 'cookies_enabled', !!flag
end
def set_http_auth(user, password)
@@ -256,20 +298,26 @@
Array(names).each do |name|
command 'add_extension', name
end
end
+ def url_blacklist=(blacklist)
+ command 'set_url_blacklist', *blacklist
+ end
+
def debug=(val)
@debug = val
command 'set_debug', !!val
end
def command(name, *args)
- message = { 'name' => name, 'args' => args }
- log message.inspect
+ message = JSON.dump({ 'name' => name, 'args' => args })
+ log message
- json = JSON.load(server.send(JSON.dump(message)))
- log json.inspect
+ response = server.send(message)
+ log response
+
+ json = JSON.load(response)
if json['error']
klass = ERROR_MAPPINGS[json['error']['name']] || BrowserError
raise klass.new(json['error'])
else