2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 15:23:54 -05:00
|
|
|
|
2011-04-05 11:42:12 -04:00
|
|
|
require 'rack/test'
|
|
|
|
require 'rack/utils'
|
2017-06-28 14:37:20 -04:00
|
|
|
require 'mini_mime'
|
2011-04-05 11:42:12 -04:00
|
|
|
require 'nokogiri'
|
|
|
|
require 'cgi'
|
|
|
|
|
|
|
|
class Capybara::RackTest::Driver < Capybara::Driver::Base
|
2011-09-04 14:26:14 -04:00
|
|
|
DEFAULT_OPTIONS = {
|
2016-10-04 14:10:29 -04:00
|
|
|
respect_data_method: false,
|
|
|
|
follow_redirects: true,
|
|
|
|
redirect_limit: 5
|
2018-01-08 15:23:54 -05:00
|
|
|
}.freeze
|
2011-04-25 05:35:57 -04:00
|
|
|
attr_reader :app, :options
|
2011-04-05 11:42:12 -04:00
|
|
|
|
2016-08-17 19:14:39 -04:00
|
|
|
def initialize(app, **options)
|
2018-07-10 17:18:39 -04:00
|
|
|
raise ArgumentError, 'rack-test requires a rack application, but none was given' unless app
|
2018-09-24 12:43:46 -04:00
|
|
|
|
2011-04-05 11:42:12 -04:00
|
|
|
@app = app
|
2011-09-04 14:26:14 -04:00
|
|
|
@options = DEFAULT_OPTIONS.merge(options)
|
2011-04-05 11:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def browser
|
2011-08-30 05:20:29 -04:00
|
|
|
@browser ||= Capybara::RackTest::Browser.new(self)
|
2011-04-05 11:42:12 -04:00
|
|
|
end
|
|
|
|
|
2012-07-12 10:57:27 -04:00
|
|
|
def follow_redirects?
|
|
|
|
@options[:follow_redirects]
|
|
|
|
end
|
|
|
|
|
2012-07-12 11:03:28 -04:00
|
|
|
def redirect_limit
|
|
|
|
@options[:redirect_limit]
|
|
|
|
end
|
|
|
|
|
2011-04-05 11:42:12 -04:00
|
|
|
def response
|
|
|
|
browser.last_response
|
|
|
|
end
|
|
|
|
|
|
|
|
def request
|
|
|
|
browser.last_request
|
|
|
|
end
|
|
|
|
|
2017-05-01 21:39:08 -04:00
|
|
|
def visit(path, **attributes)
|
2011-04-05 11:42:12 -04:00
|
|
|
browser.visit(path, attributes)
|
|
|
|
end
|
2017-07-04 18:14:55 -04:00
|
|
|
|
|
|
|
def refresh
|
|
|
|
browser.refresh
|
|
|
|
end
|
2011-04-05 11:42:12 -04:00
|
|
|
|
|
|
|
def submit(method, path, attributes)
|
|
|
|
browser.submit(method, path, attributes)
|
|
|
|
end
|
|
|
|
|
2017-05-01 21:39:08 -04:00
|
|
|
def follow(method, path, **attributes)
|
2011-04-05 11:42:12 -04:00
|
|
|
browser.follow(method, path, attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_url
|
|
|
|
browser.current_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_headers
|
|
|
|
response.headers
|
|
|
|
end
|
|
|
|
|
|
|
|
def status_code
|
|
|
|
response.status
|
|
|
|
end
|
|
|
|
|
2013-02-19 12:03:26 -05:00
|
|
|
def find_xpath(selector)
|
|
|
|
browser.find(:xpath, selector)
|
|
|
|
end
|
2016-03-30 14:27:29 -04:00
|
|
|
|
2013-02-19 12:03:26 -05:00
|
|
|
def find_css(selector)
|
2018-01-09 17:05:50 -05:00
|
|
|
browser.find(:css, selector)
|
2018-10-01 16:15:00 -04:00
|
|
|
rescue Nokogiri::CSS::SyntaxError
|
|
|
|
raise unless selector.include?(' i]')
|
|
|
|
|
|
|
|
raise ArgumentError, "This driver doesn't support case insensitive attribute matching when using CSS base selectors"
|
2011-04-05 11:42:12 -04:00
|
|
|
end
|
2016-03-30 14:27:29 -04:00
|
|
|
|
2012-09-09 21:05:17 -04:00
|
|
|
def html
|
|
|
|
browser.html
|
2011-04-05 11:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def dom
|
|
|
|
browser.dom
|
|
|
|
end
|
2016-03-30 14:27:29 -04:00
|
|
|
|
2013-02-06 14:36:55 -05:00
|
|
|
def title
|
|
|
|
browser.title
|
|
|
|
end
|
2011-04-05 11:42:12 -04:00
|
|
|
|
|
|
|
def reset!
|
|
|
|
@browser = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(*args, &block); browser.get(*args, &block); end
|
|
|
|
def post(*args, &block); browser.post(*args, &block); end
|
|
|
|
def put(*args, &block); browser.put(*args, &block); end
|
|
|
|
def delete(*args, &block); browser.delete(*args, &block); end
|
2011-04-08 11:14:05 -04:00
|
|
|
def header(key, value); browser.header(key, value); end
|
2019-02-15 15:58:45 -05:00
|
|
|
|
|
|
|
def invalid_element_errors
|
2019-02-18 19:40:27 -05:00
|
|
|
[Capybara::RackTest::Errors::StaleElementReferenceError]
|
2019-02-15 15:58:45 -05:00
|
|
|
end
|
2011-04-05 11:42:12 -04:00
|
|
|
end
|