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

#bodyObject



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

def body
  dom.to_xml
end

#current_urlObject



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

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

#domObject



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

def dom
  @dom ||= Nokogiri::HTML(source)
end

#find(selector) ⇒ Object



88
89
90
# File 'lib/capybara/rack_test/browser.rb', line 88

def find(selector)
  dom.xpath(selector).map { |node| Capybara::RackTest::Node.new(self, node) }
end

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



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

def follow(method, path, attributes = {})
  return if path.gsub(/^#{request_path}/, '').start_with?('#')
  process(method, path, attributes)
  follow_redirects!
end

#follow_redirects!Object



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

def follow_redirects!
  5.times do
    process(:get, last_response["Location"]) if last_response.redirect?
  end
  raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect?
end

#optionsObject



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

def options
  driver.options
end

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



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

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

  if new_uri.host
    @current_host = "#{new_uri.scheme}://#{new_uri.host}"
    @current_host << ":#{new_uri.port}" if new_uri.port != new_uri.default_port
  end

  if new_uri.relative?
    if path.start_with?('?')
      path = request_path + path
    elsif not path.start_with?('/')
      path = request_path.sub(%r(/[^/]*$), '/') + path
    end
    path = current_host + path
  end

  reset_cache!
  send(method, path, attributes, env)
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



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

def reset_host!
  @current_host = (Capybara.app_host || Capybara.default_host)
end

#sourceObject



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

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

#submit(method, path, attributes) ⇒ Object



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

def submit(method, path, attributes)
  path = request_path if not path or path.empty?
  process(method, path, attributes)
  follow_redirects!
end

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



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

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