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