Implementation:Teamcapybara Capybara Session Visit
| Knowledge Sources | |
|---|---|
| Domains | Testing, Navigation |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for navigating the browser to a URL provided by Capybara::Session#visit.
Description
Session#visit parses the given URI using Addressable::URI, resolves it against the configured app_host or the session's server_url, and delegates to driver.visit. Before navigating, it raises any pending server errors and marks the session as @touched (which triggers cleanup on reset). Relative URLs have the base URI's path prepended, and the server port is adjusted when always_include_port is enabled.
Usage
Call visit as the first action in a test to navigate to the page under test. Accepts relative paths (/login) or absolute URLs (http://example.com).
Code Reference
Source Location
- Repository: capybara
- File: lib/capybara/session.rb
- Lines: L261-282
Signature
def visit(visit_uri)
# @param visit_uri [#to_s] The URL to navigate to
# @return [nil]
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
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
Import
require 'capybara'
# visit is available via Capybara::DSL or directly on a Session instance
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| visit_uri | #to_s | Yes | URL to navigate to (relative or absolute) |
Outputs
| Name | Type | Description |
|---|---|---|
| (nil) | nil | Browser navigated to the specified URL; session marked as touched |
Usage Examples
# Relative path (resolved against server URL)
visit '/login'
visit '/users/new'
# Absolute URL
visit 'http://example.com/api'
# With app_host configured
Capybara.app_host = 'http://staging.example.com'
visit '/dashboard' # navigates to http://staging.example.com/dashboard