teamcapybara--capybara/lib/capybara/rack_test/browser.rb

127 lines
2.5 KiB
Ruby
Raw Normal View History

class Capybara::RackTest::Browser
include ::Rack::Test::Methods
attr_reader :driver
attr_accessor :current_host
def initialize(driver)
@driver = driver
end
def app
driver.app
end
def options
driver.options
end
def visit(path, attributes = {})
reset_host!
process(:get, path, attributes)
follow_redirects!
end
def submit(method, path, attributes)
path = request_path if not path or path.empty?
process(method, path, attributes)
follow_redirects!
end
def follow(method, path, attributes = {})
return if path.gsub(/^#{request_path}/, '').start_with?('#')
process(method, path, attributes)
follow_redirects!
end
def follow_redirects!
5.times do
process(:get, last_response["Location"]) if last_response.redirect?
end
raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect?
end
def process(method, path, attributes = {})
new_uri = URI.parse(path)
method.downcase! unless method.is_a? Symbol
if new_uri.host
@current_host = "#{new_uri.scheme}://#{new_uri.host}"
@current_host << ":#{new_uri.port}" if new_uri.port != new_uri.default_port
end
2011-07-18 19:09:44 +00:00
if new_uri.relative?
if path.start_with?('?')
path = request_path + path
elsif not path.start_with?('/')
path = request_path.sub(%r(/[^/]*$), '/') + path
end
path = current_host + path
end
2011-07-18 19:09:44 +00:00
reset_cache!
send(method, path, attributes, env)
end
def current_url
2011-07-29 23:14:31 +00:00
last_request.url
2011-04-07 14:17:51 +00:00
rescue Rack::Test::Error
""
end
def reset_host!
@current_host = (Capybara.app_host || Capybara.default_host)
end
def reset_cache!
@dom = nil
end
def body
dom.to_xml
end
def dom
@dom ||= Nokogiri::HTML(source)
end
def find(selector)
dom.xpath(selector).map { |node| Capybara::RackTest::Node.new(self, node) }
end
def source
last_response.body
rescue Rack::Test::Error
""
end
protected
def build_rack_mock_session
reset_host! unless current_host
Rack::MockSession.new(app, URI.parse(current_host).host)
end
def request_path
2011-04-06 07:36:05 +00:00
last_request.path
rescue Rack::Test::Error
""
end
def env
env = {}
begin
if last_response.redirect?
env["HTTP_REFERER"] = last_request.env["HTTP_REFERER"]
else
env["HTTP_REFERER"] = last_request.url
end
rescue Rack::Test::Error
# no request yet
end
env.merge!(options[:headers]) if options[:headers]
env
end
end