Class: Capybara::RackTest::Browser

Inherits:
Object
  • Object
show all
Includes:
Rack::Test::Methods
Defined in:
lib/capybara/rack_test/browser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_hostObject

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

#driverObject (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

#appObject



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

def app
  driver.app
end

#build_uri(path) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/capybara/rack_test/browser.rb', line 73

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_urlObject



85
86
87
88
89
# File 'lib/capybara/rack_test/browser.rb', line 85

def current_url
  last_request.url
rescue Rack::Test::Error
  ''
end

#domObject



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

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

#find(format, selector) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/capybara/rack_test/browser.rb', line 104

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



38
39
40
41
42
# File 'lib/capybara/rack_test/browser.rb', line 38

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

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

#htmlObject



112
113
114
115
116
# File 'lib/capybara/rack_test/browser.rb', line 112

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

#optionsObject



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

def options
  driver.options
end

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



64
65
66
67
68
69
70
71
# File 'lib/capybara/rack_test/browser.rb', line 64

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)

  reset_cache!
  send(method, new_uri.to_s, attributes, env.merge(options[:headers] || {}))
end

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/capybara/rack_test/browser.rb', line 44

def process_and_follow_redirects(method, path, attributes = {}, env = {})
  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

#refreshObject



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



96
97
98
# File 'lib/capybara/rack_test/browser.rb', line 96

def reset_cache!
  @dom = nil
end

#reset_host!Object



91
92
93
94
# File 'lib/capybara/rack_test/browser.rb', line 91

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) ⇒ Object



31
32
33
34
35
36
# File 'lib/capybara/rack_test/browser.rb', line 31

def submit(method, path, attributes)
  path = request_path if path.nil? || path.empty?
  uri = build_uri(path)
  uri.query = '' if method&.to_s&.downcase == 'get'
  process_and_follow_redirects(method, uri.to_s, attributes, 'HTTP_REFERER' => current_url)
end

#titleObject



118
119
120
# File 'lib/capybara/rack_test/browser.rb', line 118

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