2016-03-08 00:52:19 +00:00
|
|
|
# frozen_string_literal: true
|
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
|
|
|
|
|
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)
|
|
|
|
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 = {})
|
2015-04-14 19:46:00 +00:00
|
|
|
return if path.gsub(/^#{Regexp.escape(request_path)}/, '').start_with?('#') || path.downcase.start_with?('javascript:')
|
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
|
2017-03-24 00:26:13 +00:00
|
|
|
if path.empty?
|
|
|
|
new_uri.path = request_path
|
|
|
|
else
|
|
|
|
new_uri.path = request_path if path.start_with?("?")
|
|
|
|
new_uri.path = "/" if new_uri.path.empty?
|
|
|
|
new_uri.path = request_path.sub(%r(/[^/]*$), '/') + new_uri.path unless new_uri.path.start_with?('/')
|
|
|
|
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
|
|
|
|
2012-09-06 08:46:54 +00:00
|
|
|
@current_scheme = new_uri.scheme
|
|
|
|
@current_host = new_uri.host
|
|
|
|
@current_port = new_uri.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
|
|
|
|
""
|
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)
|
2012-09-06 08:46:54 +00:00
|
|
|
@current_scheme = uri.scheme
|
|
|
|
@current_host = uri.host
|
|
|
|
@current_port = uri.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)
|
2013-02-18 20:35:24 +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
|
2011-11-15 15:57:18 +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
|
2015-05-28 19:10:13 +00:00
|
|
|
if dom.respond_to? :title
|
|
|
|
dom.title
|
|
|
|
else
|
|
|
|
#old versions of nokogiri don't have #title - remove in 3.0
|
|
|
|
dom.xpath('/html/head/title | /html/title').first.text
|
|
|
|
end
|
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
|
2013-05-15 20:33:17 +00:00
|
|
|
"/"
|
2011-04-05 15:42:12 +00:00
|
|
|
end
|
|
|
|
end
|