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
12
# File 'lib/capybara/rack_test/browser.rb', line 9

def initialize(driver)
  @driver = driver
  @current_fragment = nil
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



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

def app
  driver.app
end

#build_uri(path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/capybara/rack_test/browser.rb', line 83

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_urlObject



99
100
101
102
103
104
105
# File 'lib/capybara/rack_test/browser.rb', line 99

def current_url
  uri = build_uri(last_request.url)
  uri.fragment = @current_fragment if @current_fragment
  uri.to_s
rescue Rack::Test::Error
  ''
end

#domObject



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

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

#find(format, selector) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/capybara/rack_test/browser.rb', line 120

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



47
48
49
50
51
# File 'lib/capybara/rack_test/browser.rb', line 47

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

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

#htmlObject



128
129
130
131
132
# File 'lib/capybara/rack_test/browser.rb', line 128

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

#last_requestObject

Raises:

  • (Rack::Test::Error)


138
139
140
141
142
# File 'lib/capybara/rack_test/browser.rb', line 138

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

  super
end

#last_responseObject

Raises:

  • (Rack::Test::Error)


144
145
146
147
148
# File 'lib/capybara/rack_test/browser.rb', line 144

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

  super
end

#optionsObject



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

def options
  driver.options
end

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



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

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 = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/capybara/rack_test/browser.rb', line 53

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

#refreshObject



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

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

#reset_cache!Object



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

def reset_cache!
  @dom = nil
end

#reset_host!Object



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

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



34
35
36
37
38
39
40
41
42
43
44
45
# 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?
  process_and_follow_redirects(
    method,
    uri.to_s,
    attributes,
    'HTTP_REFERER' => referer_url,
    'CONTENT_TYPE' => content_type
  )
end

#titleObject



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

def title
  dom.title
end

#visit(path, **attributes) ⇒ Object



22
23
24
25
26
27
# 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