2011-04-05 11:42:12 -04:00
|
|
|
class Capybara::RackTest::Browser
|
|
|
|
include ::Rack::Test::Methods
|
|
|
|
|
2011-08-30 05:20:29 -04:00
|
|
|
attr_reader :driver
|
2011-04-05 11:42:12 -04:00
|
|
|
attr_accessor :current_host
|
|
|
|
|
2011-08-30 05:20:29 -04:00
|
|
|
def initialize(driver)
|
|
|
|
@driver = driver
|
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
|
|
|
driver.app
|
|
|
|
end
|
|
|
|
|
|
|
|
def options
|
|
|
|
driver.options
|
2011-04-05 11:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def visit(path, attributes = {})
|
|
|
|
reset_host!
|
|
|
|
process(:get, path, attributes)
|
2011-08-30 04:19:52 -04:00
|
|
|
follow_redirects!
|
2011-04-05 11:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def submit(method, path, attributes)
|
|
|
|
path = request_path if not path or path.empty?
|
|
|
|
process(method, path, attributes)
|
2011-08-30 04:19:52 -04:00
|
|
|
follow_redirects!
|
2011-04-05 11:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def follow(method, path, attributes = {})
|
|
|
|
return if path.gsub(/^#{request_path}/, '').start_with?('#')
|
|
|
|
process(method, path, attributes)
|
2011-08-30 04:19:52 -04:00
|
|
|
follow_redirects!
|
2011-04-05 11:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def follow_redirects!
|
|
|
|
5.times do
|
2011-08-30 04:19:52 -04:00
|
|
|
process(:get, last_response["Location"]) if last_response.redirect?
|
2011-04-05 11:42:12 -04:00
|
|
|
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)
|
|
|
|
current_uri = URI.parse(current_url)
|
2011-11-02 12:24:54 -04:00
|
|
|
method.downcase! unless method.is_a? Symbol
|
2011-04-05 11:42:12 -04:00
|
|
|
|
|
|
|
if new_uri.host
|
|
|
|
@current_host = new_uri.scheme + '://' + new_uri.host
|
|
|
|
end
|
2011-07-18 15:09:44 -04:00
|
|
|
|
2011-05-22 04:37:02 -04:00
|
|
|
if new_uri.relative?
|
2011-08-30 04:54:20 -04:00
|
|
|
if path.start_with?('?')
|
|
|
|
path = request_path + path
|
|
|
|
elsif not path.start_with?('/')
|
|
|
|
path = request_path.sub(%r(/[^/]*$), '/') + path
|
2011-05-22 04:37:02 -04:00
|
|
|
end
|
2011-05-22 03:34:46 -04:00
|
|
|
path = current_host + path
|
|
|
|
end
|
2011-07-18 15:09:44 -04:00
|
|
|
|
2011-04-05 11:42:12 -04:00
|
|
|
reset_cache!
|
2011-11-02 12:24:54 -04:00
|
|
|
send(method, path, attributes, env)
|
2011-04-05 11:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_url
|
2011-07-29 19:14:31 -04:00
|
|
|
last_request.url
|
2011-04-07 10:17:51 -04:00
|
|
|
rescue Rack::Test::Error
|
|
|
|
""
|
2011-04-05 11:42:12 -04:00
|
|
|
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
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-05-19 11:50:06 -04:00
|
|
|
def build_rack_mock_session
|
2011-05-26 06:16:28 -04:00
|
|
|
reset_host! unless current_host
|
|
|
|
Rack::MockSession.new(app, URI.parse(current_host).host)
|
2011-05-19 11:50:06 -04:00
|
|
|
end
|
|
|
|
|
2011-04-05 11:42:12 -04:00
|
|
|
def request_path
|
2011-04-06 03:36:05 -04:00
|
|
|
last_request.path
|
|
|
|
rescue Rack::Test::Error
|
|
|
|
""
|
2011-04-05 11:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def env
|
|
|
|
env = {}
|
|
|
|
begin
|
|
|
|
env["HTTP_REFERER"] = last_request.url
|
|
|
|
rescue Rack::Test::Error
|
|
|
|
# no request yet
|
|
|
|
end
|
2011-04-25 05:35:57 -04:00
|
|
|
env.merge!(options[:headers]) if options[:headers]
|
2011-04-05 11:42:12 -04:00
|
|
|
env
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|