2011-02-18 22:53:06 -05:00
|
|
|
require 'socket'
|
|
|
|
|
|
|
|
class Capybara::Driver::Webkit
|
|
|
|
class Browser
|
|
|
|
def initialize
|
|
|
|
start_server
|
|
|
|
connect
|
|
|
|
end
|
|
|
|
|
|
|
|
def visit(url)
|
2011-02-24 23:48:18 -05:00
|
|
|
command "Visit", url
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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-02-25 00:15:08 -05:00
|
|
|
def command(name, *args)
|
|
|
|
puts ">> Sending #{name}"
|
|
|
|
@socket.puts name
|
|
|
|
args.each { |arg| @socket.puts arg }
|
|
|
|
check
|
|
|
|
read_response
|
|
|
|
end
|
|
|
|
|
2011-02-18 22:53:06 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def start_server
|
|
|
|
@pid = fork { exec("webkit_server") }
|
|
|
|
at_exit { Process.kill("INT", @pid) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def connect
|
|
|
|
puts ">> Connecting"
|
|
|
|
Capybara.timeout(5) do
|
|
|
|
attempt_connect
|
|
|
|
!@socket.nil?
|
|
|
|
end
|
|
|
|
puts ">> Connected"
|
|
|
|
end
|
|
|
|
|
|
|
|
def attempt_connect
|
|
|
|
@socket = TCPSocket.open("localhost", 9200)
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
end
|
|
|
|
|
|
|
|
def check
|
|
|
|
result = @socket.gets.strip
|
|
|
|
puts ">> #{result}"
|
|
|
|
unless result == 'ok'
|
2011-02-24 23:22:56 -05:00
|
|
|
raise WebkitError, read_response
|
2011-02-18 22:53:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_response
|
|
|
|
response_length = @socket.gets.to_i
|
|
|
|
@socket.read(response_length)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|