class Capybara::RackTest::Browser

Attributes

current_host[RW]
driver[R]

Public Class Methods

new(driver) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 7
def initialize(driver)
  @driver = driver
end

Public Instance Methods

app() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 11
def app
  driver.app
end
body() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 79
def body
  dom.to_xml
end
current_url() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 65
def current_url
  last_request.url
rescue Rack::Test::Error
  ""
end
dom() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 83
def dom
  @dom ||= Nokogiri::HTML(source)
end
find(selector) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 87
def find(selector)
  dom.xpath(selector).map { |node| Capybara::RackTest::Node.new(self, node) }
end
follow(method, path, attributes = {}) click to toggle source
# 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!() click to toggle source
# 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
options() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 15
def options
  driver.options
end
process(method, path, attributes = {}) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 44
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?
    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!() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 75
def reset_cache!
  @dom = nil
end
reset_host!() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 71
def reset_host!
  @current_host = (Capybara.app_host || Capybara.default_host)
end
source() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 91
def source
  last_response.body
rescue Rack::Test::Error
  nil
end
submit(method, path, attributes) click to toggle source
# 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 = {}) click to toggle source
# File lib/capybara/rack_test/browser.rb, line 19
def visit(path, attributes = {})
  reset_host!
  process(:get, path, attributes)
  follow_redirects!
end

Protected Instance Methods

build_rack_mock_session() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 99
def build_rack_mock_session
  reset_host! unless current_host
  Rack::MockSession.new(app, URI.parse(current_host).host)
end
env() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 110
def env
  env = {}
  begin
    env["HTTP_REFERER"] = last_request.url
  rescue Rack::Test::Error
    # no request yet
  end
  env.merge!(options[:headers]) if options[:headers]
  env
end
request_path() click to toggle source
# File lib/capybara/rack_test/browser.rb, line 104
def request_path
  last_request.path
rescue Rack::Test::Error
  ""
end