2011-02-26 13:02:43 -05:00
|
|
|
require 'json'
|
2011-02-18 22:53:06 -05:00
|
|
|
|
|
|
|
class Capybara::Driver::Webkit
|
|
|
|
class Browser
|
2012-05-04 15:50:30 -04:00
|
|
|
def initialize(connection)
|
|
|
|
@connection = connection
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def visit(url)
|
2011-02-24 23:48:18 -05:00
|
|
|
command "Visit", url
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
|
|
|
|
2011-06-08 05:36:45 -04:00
|
|
|
def header(key, value)
|
|
|
|
command("Header", key, value)
|
|
|
|
end
|
|
|
|
|
2011-02-18 22:53:06 -05:00
|
|
|
def find(query)
|
2011-02-24 23:48:18 -05:00
|
|
|
command("Find", query).split(",")
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset!
|
2011-02-24 23:48:18 -05:00
|
|
|
command("Reset")
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
|
|
|
|
2011-07-29 15:00:13 -04:00
|
|
|
def body
|
|
|
|
command("Body")
|
|
|
|
end
|
|
|
|
|
2011-02-25 18:04:23 -05:00
|
|
|
def source
|
|
|
|
command("Source")
|
|
|
|
end
|
|
|
|
|
2011-08-12 16:50:43 -04:00
|
|
|
def status_code
|
|
|
|
command("Status").to_i
|
|
|
|
end
|
|
|
|
|
2011-10-31 13:53:57 -04:00
|
|
|
def console_messages
|
|
|
|
command("ConsoleMessages").split("\n").map do |messages|
|
|
|
|
parts = messages.split("|", 3)
|
2011-12-06 19:30:04 -05:00
|
|
|
{ :source => parts.first, :line_number => Integer(parts[1]), :message => parts.last }
|
2011-10-31 13:53:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_messages
|
|
|
|
console_messages.select do |message|
|
|
|
|
message[:message] =~ /Error:/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-19 11:57:39 -04:00
|
|
|
def response_headers
|
|
|
|
Hash[command("Headers").split("\n").map { |header| header.split(": ") }]
|
|
|
|
end
|
|
|
|
|
2011-02-25 17:53:36 -05:00
|
|
|
def url
|
|
|
|
command("Url")
|
|
|
|
end
|
|
|
|
|
2011-11-15 21:11:37 -05:00
|
|
|
def requested_url
|
|
|
|
command("RequestedUrl")
|
|
|
|
end
|
|
|
|
|
2012-02-05 11:43:48 -05:00
|
|
|
def current_url
|
|
|
|
command("CurrentUrl")
|
|
|
|
end
|
|
|
|
|
2011-04-20 01:42:20 -04:00
|
|
|
def frame_focus(frame_id_or_index=nil)
|
|
|
|
if frame_id_or_index.is_a? Fixnum
|
|
|
|
command("FrameFocus", "", frame_id_or_index.to_s)
|
|
|
|
elsif frame_id_or_index
|
|
|
|
command("FrameFocus", frame_id_or_index)
|
|
|
|
else
|
|
|
|
command("FrameFocus")
|
|
|
|
end
|
2011-04-19 03:16:25 -04:00
|
|
|
end
|
|
|
|
|
2012-05-04 13:56:21 -04:00
|
|
|
def ignore_ssl_errors
|
|
|
|
command("IgnoreSslErrors")
|
|
|
|
end
|
|
|
|
|
2012-03-29 21:38:34 -04:00
|
|
|
def set_skip_image_loading(skip_image_loading)
|
|
|
|
command("SetSkipImageLoading", skip_image_loading)
|
|
|
|
end
|
|
|
|
|
2011-02-25 00:15:08 -05:00
|
|
|
def command(name, *args)
|
2012-05-04 15:50:30 -04:00
|
|
|
@connection.puts name
|
|
|
|
@connection.puts args.size
|
2011-02-26 13:38:10 -05:00
|
|
|
args.each do |arg|
|
2012-05-04 15:50:30 -04:00
|
|
|
@connection.puts arg.to_s.bytesize
|
|
|
|
@connection.print arg.to_s
|
2011-02-26 13:38:10 -05:00
|
|
|
end
|
2011-02-25 00:15:08 -05:00
|
|
|
check
|
|
|
|
read_response
|
|
|
|
end
|
|
|
|
|
2011-02-26 13:02:43 -05:00
|
|
|
def evaluate_script(script)
|
|
|
|
json = command('Evaluate', script)
|
|
|
|
JSON.parse("[#{json}]").first
|
|
|
|
end
|
|
|
|
|
2011-02-26 14:03:30 -05:00
|
|
|
def execute_script(script)
|
|
|
|
command('Execute', script)
|
|
|
|
end
|
|
|
|
|
2011-05-26 15:05:16 -04:00
|
|
|
def render(path, width, height)
|
|
|
|
command "Render", path, width, height
|
|
|
|
end
|
|
|
|
|
2011-09-24 17:25:15 -04:00
|
|
|
def set_cookie(cookie)
|
|
|
|
command "SetCookie", cookie
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cookies
|
|
|
|
command "ClearCookies"
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_cookies
|
2011-09-30 11:41:59 -04:00
|
|
|
command("GetCookies").lines.map{ |line| line.strip }.select{ |line| !line.empty? }
|
2011-09-24 17:25:15 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 17:13:06 -04:00
|
|
|
def set_proxy(options = {})
|
|
|
|
options = default_proxy_options.merge(options)
|
|
|
|
command("SetProxy", options[:host], options[:port], options[:user], options[:pass])
|
|
|
|
end
|
2011-09-24 17:55:18 -04:00
|
|
|
|
2011-10-14 17:13:06 -04:00
|
|
|
def clear_proxy
|
|
|
|
command("SetProxy")
|
2011-09-24 17:55:18 -04:00
|
|
|
end
|
|
|
|
|
2012-03-29 12:59:43 -04:00
|
|
|
def resize_window(width, height)
|
|
|
|
command("ResizeWindow", width.to_i, height.to_i)
|
|
|
|
end
|
|
|
|
|
2011-02-18 22:53:06 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def check
|
2012-05-04 15:50:30 -04:00
|
|
|
result = @connection.gets
|
2011-07-12 15:31:10 -04:00
|
|
|
result.strip! if result
|
2011-02-25 17:44:16 -05:00
|
|
|
|
2011-07-12 15:31:10 -04:00
|
|
|
if result.nil?
|
|
|
|
raise WebkitNoResponseError, "No response received from the server."
|
2012-03-29 21:38:34 -04:00
|
|
|
elsif result != 'ok'
|
2011-07-12 15:31:10 -04:00
|
|
|
raise WebkitInvalidResponseError, read_response
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
2011-07-12 15:31:10 -04:00
|
|
|
|
|
|
|
result
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_response
|
2012-05-04 15:50:30 -04:00
|
|
|
response_length = @connection.gets.to_i
|
2012-05-04 16:00:21 -04:00
|
|
|
if response_length > 0
|
|
|
|
response = @connection.read(response_length)
|
|
|
|
response.force_encoding("UTF-8") if response.respond_to?(:force_encoding)
|
|
|
|
response
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
2011-10-14 17:13:06 -04:00
|
|
|
|
|
|
|
def default_proxy_options
|
|
|
|
{
|
|
|
|
:host => "localhost",
|
|
|
|
:port => "0",
|
|
|
|
:user => "",
|
|
|
|
:pass => ""
|
|
|
|
}
|
|
|
|
end
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
|
|
|
end
|