1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00
capybara-webkit/lib/capybara/driver/webkit/browser.rb

62 lines
1.1 KiB
Ruby
Raw Normal View History

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)
command "Visit", url
2011-02-18 22:53:06 -05:00
end
def find(query)
command("Find", query).split(",")
2011-02-18 22:53:06 -05:00
end
def reset!
command("Reset")
2011-02-18 22:53:06 -05:00
end
2011-02-25 00:15:08 -05:00
def command(name, *args)
@socket.puts name
@socket.puts args.size.to_s
2011-02-25 00:15:08 -05:00
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
Capybara.timeout(5) do
attempt_connect
!@socket.nil?
end
end
def attempt_connect
@socket = TCPSocket.open("localhost", 9200)
rescue Errno::ECONNREFUSED
end
def check
result = @socket.gets.strip
unless result == 'ok'
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