123456789_123456789_123456789_123456789_123456789_

Class: Capybara::RackTest::Browser

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Rack::Test::Methods
Inherits: Object
Defined in: lib/capybara/rack_test/browser.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(driver) ⇒ Browser

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 9

def initialize(driver)
  @driver = driver
  @current_fragment = nil
end

Instance Attribute Details

#current_host (rw)

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 7

attr_accessor :current_host

#driver (readonly)

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 6

attr_reader :driver

Instance Method Details

#app

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 14

def app
  driver.app
end

#build_uri(path)

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 84

def build_uri(path)
  uri = URI.parse(path)
  base_uri = base_relative_uri_for(uri)

  uri.path = base_uri.path + uri.path unless uri.absolute? || uri.path.start_with?('/')

  if base_uri.absolute?
    base_uri.merge(uri)
  else
    uri.scheme ||= @current_scheme
    uri.host ||= @current_host
    uri.port ||= @current_port unless uri.default_port == @current_port
    uri
  end
end

#current_url

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 100

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

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 117

def dom
  @dom ||= Capybara::HTML(html)
end

#find(format, selector)

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 121

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)

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 48

def follow(method, path, **attributes)
  return if fragment_or_script?(path)

  process_and_follow_redirects(method, path, attributes, 'HTTP_REFERER' => referer_url)
end

#fragment_or_script?(path) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 191

def fragment_or_script?(path)
  path.gsub(/^#{Regexp.escape(request_path)}/, '').start_with?('#') || path.downcase.start_with?('javascript:')
end

#html

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 129

def html
  last_response.body
rescue Rack::Test::Error
  ''
end

#last_request

Raises:

  • (Rack::Test::Error)
[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 139

def last_request
  raise Rack::Test::Error if @new_visit_request

  super
end

#last_response

Raises:

  • (Rack::Test::Error)
[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 145

def last_response
  raise Rack::Test::Error if @new_visit_request

  super
end

#options

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 18

def options
  driver.options
end

#process(method, path, attributes = {}, env = {})

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 74

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!
  @new_visit_request = false
  send(method, new_uri.to_s, attributes, env.merge(options[:headers] || {}))
end

#process_and_follow_redirects(method, path, attributes = {}, env = {})

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 54

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

#referer_url (private)

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 195

def referer_url
  build_uri(last_request.url).to_s
rescue Rack::Test::Error
  ''
end

#refresh

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 29

def refresh
  reset_cache!
  request(last_request.fullpath, last_request.env)
end

#reset_cache!

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 113

def reset_cache!
  @dom = nil
end

#reset_host!

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 108

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, content_type: nil)

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 34

def submit(method, path, attributes, content_type: nil)
  path = request_path if path.nil? || path.empty?
  uri = build_uri(path)
  uri.query = '' if method.to_s.casecmp('get').zero?
  env = { 'HTTP_REFERER' => referer_url }
  env['CONTENT_TYPE'] = content_type if content_type
  process_and_follow_redirects(
    method,
    uri.to_s,
    attributes,
    env
  )
end

#title

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 135

def title
  dom.title
end

#visit(path, **attributes)

[ GitHub ]

  
# File 'lib/capybara/rack_test/browser.rb', line 22

def visit(path, **attributes)
  @new_visit_request = true
  reset_cache!
  reset_host!
  process_and_follow_redirects(:get, path, attributes)
end