Class: Capybara::Driver::RackTest

Inherits:
Base
  • Object
show all
Includes:
Rack::Test::Methods
Defined in:
lib/capybara/driver/rack_test_driver.rb

Defined Under Namespace

Classes: Form, Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#evaluate_script, #execute_script, #has_shortcircuit_timeout?, #wait?, #wait_until, #within_frame, #within_window

Constructor Details

#initialize(app) ⇒ RackTest

Returns a new instance of RackTest.

Raises:

  • (ArgumentError)


194
195
196
197
# File 'lib/capybara/driver/rack_test_driver.rb', line 194

def initialize(app)
  raise ArgumentError, "rack-test requires a rack application, but none was given" unless app
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



189
190
191
# File 'lib/capybara/driver/rack_test_driver.rb', line 189

def app
  @app
end

Instance Method Details

#bodyObject Also known as: source



246
247
248
# File 'lib/capybara/driver/rack_test_driver.rb', line 246

def body
  @body ||= response.body
end

#current_urlObject



210
211
212
# File 'lib/capybara/driver/rack_test_driver.rb', line 210

def current_url
  request.url rescue ""
end

#delete(*args, &block) ⇒ Object



262
# File 'lib/capybara/driver/rack_test_driver.rb', line 262

def delete(*args, &block); reset_cache; super; end

#find(selector) ⇒ Object



242
243
244
# File 'lib/capybara/driver/rack_test_driver.rb', line 242

def find(selector)
  html.xpath(selector).map { |node| Node.new(self, node) }
end

#follow_redirects!Object



264
265
266
267
268
269
# File 'lib/capybara/driver/rack_test_driver.rb', line 264

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

#get(*args, &block) ⇒ Object



259
# File 'lib/capybara/driver/rack_test_driver.rb', line 259

def get(*args, &block); reset_cache; super; end

#htmlObject



250
251
252
# File 'lib/capybara/driver/rack_test_driver.rb', line 250

def html
  @html ||= Nokogiri::HTML(body)
end

#post(*args, &block) ⇒ Object



260
# File 'lib/capybara/driver/rack_test_driver.rb', line 260

def post(*args, &block); reset_cache; super; end

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



203
204
205
206
207
208
# File 'lib/capybara/driver/rack_test_driver.rb', line 203

def process(method, path, attributes = {})
  return if path.gsub(/^#{request_path}/, '') =~ /^#/
  path = request_path + path if path =~ /^\?/
  send(method, to_binary(path), to_binary( attributes ), env)
  follow_redirects!
end

#put(*args, &block) ⇒ Object



261
# File 'lib/capybara/driver/rack_test_driver.rb', line 261

def put(*args, &block); reset_cache; super; end

#reset!Object



255
256
257
# File 'lib/capybara/driver/rack_test_driver.rb', line 255

def reset!
  clear_rack_mock_session
end

#response_headersObject



214
215
216
# File 'lib/capybara/driver/rack_test_driver.rb', line 214

def response_headers
  response.headers
end

#status_codeObject



218
219
220
# File 'lib/capybara/driver/rack_test_driver.rb', line 218

def status_code
  response.status
end

#submit(method, path, attributes) ⇒ Object



236
237
238
239
240
# File 'lib/capybara/driver/rack_test_driver.rb', line 236

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

#to_binary(object) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/capybara/driver/rack_test_driver.rb', line 222

def to_binary(object)
  return object unless Kernel.const_defined?(:Encoding)

  if object.respond_to?(:force_encoding)
    object.dup.force_encoding(Encoding::ASCII_8BIT)
  elsif object.respond_to?(:each_pair) #Hash
    {}.tap { |x| object.each_pair {|k,v| x[to_binary(k)] = to_binary(v) } }
  elsif object.respond_to?(:each) #Array
    object.map{|x| to_binary(x)}
  else
    object
  end
end

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



199
200
201
# File 'lib/capybara/driver/rack_test_driver.rb', line 199

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