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(app, options = {}) ⇒ Browser



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

def initialize(app, options={})
  @app = app
  @options = options
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app



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

def app
  @app
end

#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

#optionsObject (readonly)

Returns the value of attribute options



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

def options
  @options
end

Instance Method Details

#bodyObject



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

def body
  dom.to_xml
end

#current_urlObject



57
58
59
60
61
# File 'lib/capybara/rack_test/browser.rb', line 57

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

#domObject



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

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

#find(selector) ⇒ Object



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

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

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



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

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

#follow_redirects!Object



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

def follow_redirects!
  5.times do
    follow_redirect! if last_response.redirect?
  end
  raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect?
end

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/capybara/rack_test/browser.rb', line 34

def process(method, path, attributes = {})
  new_uri = URI.parse(path)
  current_uri = URI.parse(current_url)

  if new_uri.host
    @current_host = new_uri.scheme + '://' + new_uri.host
  end
  
  if new_uri.relative?
    path = request_path + path if path.start_with?('?')
    
    unless path.start_with?('/')
      folders = request_path.split('/')
      path = (folders[0, folders.size - 1] << path).join('/')
    end
    path = current_host + path
  end
  
  reset_cache!
  send(method, path, attributes, env)
  follow_redirects!
end

#reset_cache!Object



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

def reset_cache!
  @dom = nil
end

#reset_host!Object



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

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

#sourceObject



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

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

#submit(method, path, attributes) ⇒ Object



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

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

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



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

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