2016-03-08 00:52:19 +00:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 20:23:54 +00:00
|
|
|
|
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
|
|
|
|
|
2017-05-02 01:39:08 +00:00
|
|
|
def visit(path, **attributes)
|
2011-04-05 15:42:12 +00:00
|
|
|
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
|
|
|
|
|
2017-07-04 22:14:55 +00:00
|
|
|
def refresh
|
|
|
|
reset_cache!
|
|
|
|
request(last_request.fullpath, last_request.env)
|
|
|
|
end
|
|
|
|
|
2011-04-05 15:42:12 +00:00
|
|
|
def submit(method, path, attributes)
|
2018-01-09 22:05:50 +00:00
|
|
|
path = request_path if path.nil? || path.empty?
|
|
|
|
process_and_follow_redirects(method, path, attributes, 'HTTP_REFERER' => current_url)
|
2011-04-05 15:42:12 +00:00
|
|
|
end
|
|
|
|
|
2017-05-02 01:39:08 +00:00
|
|
|
def follow(method, path, **attributes)
|
2018-05-16 19:47:08 +00:00
|
|
|
return if fragment_or_script?(path)
|
2018-09-24 16:43:46 +00:00
|
|
|
|
2018-01-09 22:05:50 +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)
|
2018-01-09 22:05:50 +00:00
|
|
|
|
|
|
|
return unless driver.follow_redirects?
|
|
|
|
|
|
|
|
driver.redirect_limit.times do
|
2018-06-07 17:05:42 +00:00
|
|
|
if last_response.redirect?
|
|
|
|
if [307, 308].include? last_response.status
|
2018-07-10 21:18:39 +00:00
|
|
|
process(last_request.request_method, last_response['Location'], last_request.params, env)
|
2018-06-07 17:05:42 +00:00
|
|
|
else
|
2018-07-10 21:18:39 +00:00
|
|
|
process(:get, last_response['Location'], {}, env)
|
2018-06-07 17:05:42 +00:00
|
|
|
end
|
|
|
|
end
|
2011-04-05 15:42:12 +00:00
|
|
|
end
|
2018-01-09 22:05:50 +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
|
|
|
|
|
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
|
2017-03-24 00:26:13 +00:00
|
|
|
if path.empty?
|
|
|
|
new_uri.path = request_path
|
|
|
|
else
|
2018-07-10 21:18:39 +00:00
|
|
|
new_uri.path = request_path if path.start_with?('?')
|
|
|
|
new_uri.path = '/' if new_uri.path.empty?
|
2018-01-09 22:05:50 +00:00
|
|
|
new_uri.path = request_path.sub(%r{/[^/]*$}, '/') + new_uri.path unless new_uri.path.start_with?('/')
|
2017-03-24 00:26:13 +00:00
|
|
|
end
|
2012-09-06 08:46:54 +00:00
|
|
|
new_uri.scheme ||= @current_scheme
|
|
|
|
new_uri.host ||= @current_host
|
|
|
|
new_uri.port ||= @current_port unless new_uri.default_port == @current_port
|
2011-07-18 19:09:44 +00:00
|
|
|
|
2018-05-16 19:47:08 +00:00
|
|
|
@current_scheme, @current_host, @current_port = new_uri.select(:scheme, :host, :port)
|
2011-07-18 19:09:44 +00:00
|
|
|
|
2011-04-05 15:42:12 +00:00
|
|
|
reset_cache!
|
2012-09-06 08:46:54 +00:00
|
|
|
send(method, new_uri.to_s, 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
|
2018-07-10 21:18:39 +00:00
|
|
|
''
|
2011-04-05 15:42:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_host!
|
2016-12-15 17:04:01 +00:00
|
|
|
uri = URI.parse(driver.session_options.app_host || driver.session_options.default_host)
|
2018-05-16 19:47:08 +00:00
|
|
|
@current_scheme, @current_host, @current_port = uri.select(:scheme, :host, :port)
|
2011-04-05 15:42:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_cache!
|
|
|
|
@dom = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def dom
|
2013-04-29 20:50:14 +00:00
|
|
|
@dom ||= Capybara::HTML(html)
|
2011-04-05 15:42:12 +00:00
|
|
|
end
|
|
|
|
|
2013-02-19 17:03:26 +00:00
|
|
|
def find(format, selector)
|
2018-01-09 22:05:50 +00:00
|
|
|
if format == :css
|
2013-02-18 22:41:44 +00:00
|
|
|
dom.css(selector, Capybara::RackTest::CSSHandlers.new)
|
2013-02-16 21:26:01 +00:00
|
|
|
else
|
|
|
|
dom.xpath(selector)
|
|
|
|
end.map { |node| Capybara::RackTest::Node.new(self, node) }
|
2011-04-05 15:42:12 +00:00
|
|
|
end
|
|
|
|
|
2012-11-20 13:22:19 +00:00
|
|
|
def html
|
2011-04-05 15:42:12 +00:00
|
|
|
last_response.body
|
|
|
|
rescue Rack::Test::Error
|
2018-07-10 21:18:39 +00:00
|
|
|
''
|
2011-04-05 15:42:12 +00:00
|
|
|
end
|
2013-11-10 13:31:08 +00:00
|
|
|
|
2013-02-06 19:36:55 +00:00
|
|
|
def title
|
2016-08-18 03:16:32 +00:00
|
|
|
dom.title
|
2013-02-06 19:36:55 +00:00
|
|
|
end
|
2013-11-10 13:31:08 +00:00
|
|
|
|
2011-04-05 15:42:12 +00:00
|
|
|
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
|
2012-12-03 01:51:50 +00:00
|
|
|
Rack::MockSession.new(app, current_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
|
2018-07-10 21:18:39 +00:00
|
|
|
'/'
|
2011-04-05 15:42:12 +00:00
|
|
|
end
|
2018-05-16 19:47:08 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fragment_or_script?(path)
|
|
|
|
path.gsub(/^#{Regexp.escape(request_path)}/, '').start_with?('#') || path.downcase.start_with?('javascript:')
|
|
|
|
end
|
2011-04-05 15:42:12 +00:00
|
|
|
end
|