class Capybara::RackTest::Browser

Attributes

current_host[RW]
driver[R]

Public Class Methods

new(driver) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 9
def initialize(driver)
  @driver = driver
  @current_fragment = nil
end

Public Instance Methods

app() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 14
def app
  driver.app
end
build_uri(path) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 75
def build_uri(path)
  URI.parse(path).tap do |uri|
    uri.path = request_path if path.empty? || path.start_with?('?')
    uri.path = '/' if uri.path.empty?
    uri.path = request_path.sub(%r{/[^/]*$}, '/') + uri.path unless uri.path.start_with?('/')

    uri.scheme ||= @current_scheme
    uri.host ||= @current_host
    uri.port ||= @current_port unless uri.default_port == @current_port
  end
end
current_url() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 87
def current_url
  uri = build_uri(last_request.url)
  uri.fragment = @current_fragment if @current_fragment
  uri.to_s
rescue Rack::Test::Error
  ''
end
dom() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 104
def dom
  @dom ||= Capybara::HTML(html)
end
find(format, selector) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 108
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) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 39
def follow(method, path, **attributes)
  return if fragment_or_script?(path)

  process_and_follow_redirects(method, path, attributes, 'HTTP_REFERER' => current_url)
end
html() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 116
def html
  last_response.body
rescue Rack::Test::Error
  ''
end
options() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 18
def options
  driver.options
end
process(method, path, attributes = {}, env = {}) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 66
def process(method, path, attributes = {}, env = {})
  method = method.downcase
  new_uri = build_uri(path)
  @current_scheme, @current_host, @current_port = new_uri.select(:scheme, :host, :port)
  @current_fragment = new_uri.fragment || @current_fragment
  reset_cache!
  send(method, new_uri.to_s, attributes, env.merge(options[:headers] || {}))
end
process_and_follow_redirects(method, path, attributes = {}, env = {}) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 45
def process_and_follow_redirects(method, path, attributes = {}, env = {})
  @current_fragment = build_uri(path).fragment
  process(method, path, attributes, env)

  return unless driver.follow_redirects?

  driver.redirect_limit.times do
    if last_response.redirect?
      if [307, 308].include? last_response.status
        process(last_request.request_method, last_response['Location'], last_request.params, env)
      else
        process(:get, last_response['Location'], {}, env)
      end
    end
  end

  if last_response.redirect? # rubocop:disable Style/GuardClause
    raise Capybara::InfiniteRedirectError, "redirected more than #{driver.redirect_limit} times, check for infinite redirects."
  end
end
refresh() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 27
def refresh
  reset_cache!
  request(last_request.fullpath, last_request.env)
end
reset_cache!() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 100
def reset_cache!
  @dom = nil
end
reset_host!() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 95
def reset_host!
  uri = URI.parse(driver.session_options.app_host || driver.session_options.default_host)
  @current_scheme, @current_host, @current_port = uri.select(:scheme, :host, :port)
end
submit(method, path, attributes) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 32
def submit(method, path, attributes)
  path = request_path if path.nil? || path.empty?
  uri = build_uri(path)
  uri.query = '' if method.to_s.casecmp('get').zero?
  process_and_follow_redirects(method, uri.to_s, attributes, 'HTTP_REFERER' => current_url)
end
title() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 122
def title
  dom.title
end
visit(path, **attributes) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 22
def visit(path, **attributes)
  reset_host!
  process_and_follow_redirects(:get, path, attributes)
end

Protected Instance Methods

build_rack_mock_session() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 128
def build_rack_mock_session
  reset_host! unless current_host
  Rack::MockSession.new(app, current_host)
end
request_path() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 133
def request_path
  last_request.path
rescue Rack::Test::Error
  '/'
end

Private Instance Methods

fragment_or_script?(path) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 141
def fragment_or_script?(path)
  path.gsub(/^#{Regexp.escape(request_path)}/, '').start_with?('#') || path.downcase.start_with?('javascript:')
end