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

#cleanup!, #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)


177
178
179
180
# File 'lib/capybara/driver/rack_test_driver.rb', line 177

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.



172
173
174
# File 'lib/capybara/driver/rack_test_driver.rb', line 172

def app
  @app
end

Instance Method Details

#bodyObject Also known as: source



229
230
231
# File 'lib/capybara/driver/rack_test_driver.rb', line 229

def body
  @body ||= response.body
end

#current_urlObject



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

def current_url
  request.url rescue ""
end

#delete(*args, &block) ⇒ Object



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

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

#find(selector) ⇒ Object



225
226
227
# File 'lib/capybara/driver/rack_test_driver.rb', line 225

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

#follow_redirects!Object



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

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



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

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

#htmlObject



233
234
235
# File 'lib/capybara/driver/rack_test_driver.rb', line 233

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

#post(*args, &block) ⇒ Object



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

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

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



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

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



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

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

#reset!Object



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

def reset!
  clear_cookies
end

#response_headersObject



197
198
199
# File 'lib/capybara/driver/rack_test_driver.rb', line 197

def response_headers
  response.headers
end

#status_codeObject



201
202
203
# File 'lib/capybara/driver/rack_test_driver.rb', line 201

def status_code
  response.status
end

#submit(method, path, attributes) ⇒ Object



219
220
221
222
223
# File 'lib/capybara/driver/rack_test_driver.rb', line 219

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



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/capybara/driver/rack_test_driver.rb', line 205

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



182
183
184
# File 'lib/capybara/driver/rack_test_driver.rb', line 182

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