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-02-29 12:32:13 +00:00
|
|
|
attr_reader :server, :client, :logger
|
2011-10-27 23:34:14 +01:00
|
|
|
|
2012-02-29 12:32:13 +00:00
|
|
|
def initialize(server, client, logger = nil)
|
|
|
|
@server = server
|
|
|
|
@client = client
|
|
|
|
@logger = logger
|
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-02-29 12:32:13 +00:00
|
|
|
def visit(url)
|
2011-10-27 23:34:14 +01:00
|
|
|
command 'visit', url
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_url
|
|
|
|
command 'current_url'
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
yield
|
|
|
|
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)
|
|
|
|
command 'trigger', page_id, id, event
|
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 = {})
|
|
|
|
command 'render', path, !!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
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
def command(name, *args)
|
|
|
|
message = { 'name' => name, 'args' => args }
|
|
|
|
log message.inspect
|
|
|
|
|
2012-03-11 10:56:19 +00:00
|
|
|
json = MultiJson.decode(server.send(MultiJson.encode(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'
|
|
|
|
raise JavascriptError.new(json['error'])
|
|
|
|
else
|
|
|
|
raise BrowserError.new(json['error'])
|
|
|
|
end
|
2011-10-27 23:34:14 +01:00
|
|
|
else
|
|
|
|
json['response']
|
|
|
|
end
|
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
|
|
|
|
|
|
|
|
def log(message)
|
|
|
|
logger.puts message if logger
|
|
|
|
end
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
end
|