Method: Capybara::Session#visit

Defined in:
lib/capybara/session.rb

#visit(visit_uri) ⇒ Object

Navigate to the given URL. The URL can either be a relative URL or an absolute URL The behaviour of either depends on the driver.

session.visit('/foo')
session.visit('http://google.com')

For drivers which can run against an external application, such as the selenium driver giving an absolute URL will navigate to that page. This allows testing applications running on remote servers. For these drivers, setting app_host will make the remote server the default. For example:

Capybara.app_host = 'http://google.com'
session.visit('/') # visits the google homepage

If always_include_port is set to true and this session is running against a rack application, then the port that the rack application is running on will automatically be inserted into the URL. Supposing the app is running on port 4567, doing something like:

visit("http://google.com/test")

Will actually navigate to http://google.com:4567/test.

Parameters:

  • visit_uri (#to_s)

    The URL to navigate to. The parameter will be cast to a String.


258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/capybara/session.rb', line 258

def visit(visit_uri)
  raise_server_error!
  @touched = true

  visit_uri = ::Addressable::URI.parse(visit_uri.to_s)
  base_uri = ::Addressable::URI.parse(config.app_host || server_url)

  if base_uri && [nil, 'http', 'https'].include?(visit_uri.scheme)
    if visit_uri.relative?
      visit_uri_parts = visit_uri.to_hash.compact

      # Useful to people deploying to a subdirectory
      # and/or single page apps where only the url fragment changes
      visit_uri_parts[:path] = base_uri.path + visit_uri.path

      visit_uri = base_uri.merge(visit_uri_parts)
    end
    adjust_server_port(visit_uri)
  end

  driver.visit(visit_uri.to_s)
end