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.



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

def initialize(driver)
  @driver = driver
end

Instance Attribute Details

#current_hostObject

Returns the value of attribute current_host.



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

def current_host
  @current_host
end

#driverObject (readonly)

Returns the value of attribute driver.



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

def driver
  @driver
end

Instance Method Details

#appObject



11
12
13
# File 'lib/capybara/rack_test/browser.rb', line 11

def app
  driver.app
end

#current_urlObject



63
64
65
66
67
# File 'lib/capybara/rack_test/browser.rb', line 63

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

#domObject



80
81
82
# File 'lib/capybara/rack_test/browser.rb', line 80

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

#find(format, selector) ⇒ Object



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

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



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

def follow(method, path, attributes = {})
  return if path.gsub(/^#{Regexp.escape(request_path)}/, '').start_with?('#') || path.downcase.start_with?('javascript:')
  process_and_follow_redirects(method, path, attributes, {'HTTP_REFERER' => current_url})
end

#htmlObject



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

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

#optionsObject



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

def options
  driver.options
end

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



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

def process(method, path, attributes = {}, env = {})
  new_uri = URI.parse(path)
  method.downcase! unless method.is_a? Symbol

  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?('/')
  new_uri.scheme ||= @current_scheme
  new_uri.host ||= @current_host
  new_uri.port ||= @current_port unless new_uri.default_port == @current_port

  @current_scheme = new_uri.scheme
  @current_host = new_uri.host
  @current_port = new_uri.port

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

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



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

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

#reset_cache!Object



76
77
78
# File 'lib/capybara/rack_test/browser.rb', line 76

def reset_cache!
  @dom = nil
end

#reset_host!Object



69
70
71
72
73
74
# File 'lib/capybara/rack_test/browser.rb', line 69

def reset_host!
  uri = URI.parse(Capybara.app_host || Capybara.default_host)
  @current_scheme = uri.scheme
  @current_host = uri.host
  @current_port = uri.port
end

#submit(method, path, attributes) ⇒ Object



24
25
26
27
# File 'lib/capybara/rack_test/browser.rb', line 24

def submit(method, path, attributes)
  path = request_path if not path or path.empty?
  process_and_follow_redirects(method, path, attributes, {'HTTP_REFERER' => current_url})
end

#titleObject



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

def title
  if dom.respond_to? :title
    dom.title
  else
    #old versions of nokogiri don't have #title - remove in 3.0
    dom.xpath('/html/head/title | /html/title').first.text
  end
end

#visit(path, attributes = {}) ⇒ Object



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

def visit(path, attributes = {})
  reset_host!
  process_and_follow_redirects(:get, path, attributes)
end