Class: Capybara::RackTest::Browser
- Inherits:
-
Object
- Object
- Capybara::RackTest::Browser
- Includes:
- Rack::Test::Methods
- Defined in:
- lib/capybara/rack_test/browser.rb
Instance Attribute Summary collapse
-
#current_host ⇒ Object
Returns the value of attribute current_host.
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
Instance Method Summary collapse
- #app ⇒ Object
- #current_url ⇒ Object
- #dom ⇒ Object
- #find(format, selector) ⇒ Object
- #follow(method, path, **attributes) ⇒ Object
- #html ⇒ Object
-
#initialize(driver) ⇒ Browser
constructor
A new instance of Browser.
- #options ⇒ Object
- #process(method, path, attributes = {}, env = {}) ⇒ Object
- #process_and_follow_redirects(method, path, attributes = {}, env = {}) ⇒ Object
- #refresh ⇒ Object
- #reset_cache! ⇒ Object
- #reset_host! ⇒ Object
- #submit(method, path, attributes) ⇒ Object
- #title ⇒ Object
- #visit(path, **attributes) ⇒ Object
Constructor Details
#initialize(driver) ⇒ Browser
Returns a new instance of Browser.
9 10 11 |
# File 'lib/capybara/rack_test/browser.rb', line 9 def initialize(driver) @driver = driver end |
Instance Attribute Details
#current_host ⇒ Object
Returns the value of attribute current_host.
7 8 9 |
# File 'lib/capybara/rack_test/browser.rb', line 7 def current_host @current_host end |
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
6 7 8 |
# File 'lib/capybara/rack_test/browser.rb', line 6 def driver @driver end |
Instance Method Details
#app ⇒ Object
13 14 15 |
# File 'lib/capybara/rack_test/browser.rb', line 13 def app driver.app end |
#current_url ⇒ Object
72 73 74 75 76 |
# File 'lib/capybara/rack_test/browser.rb', line 72 def current_url last_request.url rescue Rack::Test::Error "" end |
#dom ⇒ Object
87 88 89 |
# File 'lib/capybara/rack_test/browser.rb', line 87 def dom @dom ||= Capybara::HTML(html) end |
#find(format, selector) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/capybara/rack_test/browser.rb', line 91 def find(format, selector) if format == :css dom.css(selector, Capybara::RackTest::CSSHandlers.new) else dom.xpath(selector) end.map { |node| Capybara::RackTest::Node.new(self, node) } end |
#follow(method, path, **attributes) ⇒ Object
36 37 38 39 |
# File 'lib/capybara/rack_test/browser.rb', line 36 def follow(method, path, **attributes) return if fragment_or_script?(path) process_and_follow_redirects(method, path, attributes, 'HTTP_REFERER' => current_url) end |
#html ⇒ Object
99 100 101 102 103 |
# File 'lib/capybara/rack_test/browser.rb', line 99 def html last_response.body rescue Rack::Test::Error "" end |
#options ⇒ Object
17 18 19 |
# File 'lib/capybara/rack_test/browser.rb', line 17 def driver. end |
#process(method, path, attributes = {}, env = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/capybara/rack_test/browser.rb', line 52 def process(method, path, attributes = {}, env = {}) new_uri = URI.parse(path) method.downcase! unless method.is_a? Symbol 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 new_uri.scheme ||= @current_scheme new_uri.host ||= @current_host new_uri.port ||= @current_port unless new_uri.default_port == @current_port @current_scheme, @current_host, @current_port = new_uri.select(:scheme, :host, :port) reset_cache! send(method, new_uri.to_s, attributes, env.merge([:headers] || {})) end |
#process_and_follow_redirects(method, path, attributes = {}, env = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/capybara/rack_test/browser.rb', line 41 def process_and_follow_redirects(method, path, attributes = {}, env = {}) process(method, path, attributes, env) return unless driver.follow_redirects? driver.redirect_limit.times do process(:get, last_response["Location"], {}, env) if last_response.redirect? end raise Capybara::InfiniteRedirectError, "redirected more than #{driver.redirect_limit} times, check for infinite redirects." if last_response.redirect? end |
#refresh ⇒ Object
26 27 28 29 |
# File 'lib/capybara/rack_test/browser.rb', line 26 def refresh reset_cache! request(last_request.fullpath, last_request.env) end |
#reset_cache! ⇒ Object
83 84 85 |
# File 'lib/capybara/rack_test/browser.rb', line 83 def reset_cache! @dom = nil end |
#reset_host! ⇒ Object
78 79 80 81 |
# File 'lib/capybara/rack_test/browser.rb', line 78 def reset_host! uri = URI.parse(driver..app_host || driver..default_host) @current_scheme, @current_host, @current_port = uri.select(:scheme, :host, :port) end |
#submit(method, path, attributes) ⇒ Object
31 32 33 34 |
# File 'lib/capybara/rack_test/browser.rb', line 31 def submit(method, path, attributes) path = request_path if path.nil? || path.empty? process_and_follow_redirects(method, path, attributes, 'HTTP_REFERER' => current_url) end |
#title ⇒ Object
105 106 107 |
# File 'lib/capybara/rack_test/browser.rb', line 105 def title dom.title end |
#visit(path, **attributes) ⇒ Object
21 22 23 24 |
# File 'lib/capybara/rack_test/browser.rb', line 21 def visit(path, **attributes) reset_host! process_and_follow_redirects(:get, path, attributes) end |