Implementation:Teamcapybara Capybara Session Matchers Assert Current Path
| Knowledge Sources | |
|---|---|
| Domains | Testing, Assertions |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for asserting the browser's current path or URL provided by Capybara::SessionMatchers#assert_current_path.
Description
SessionMatchers#assert_current_path constructs a CurrentPathQuery and resolves it within synchronize. The query compares the expected value against the session's current_url (when url: true or a full URL is given) or current_path + query string. has_current_path? is the predicate form that returns Boolean.
Usage
Use with RSpec: expect(page).to have_current_path('/dashboard') or call directly: page.assert_current_path('/dashboard').
Code Reference
Source Location
- Repository: capybara
- File: lib/capybara/session/matchers.rb
- Lines: L22-26
Signature
def assert_current_path(path, **options, &optional_filter_block)
# @param path [String, Regexp] Expected path or URL
# @option options [Boolean] url Compare against full URL (auto-detected)
# @option options [Boolean] ignore_query Ignore query string
# @option options [Numeric] wait Override wait time
# @return [true]
# @raise [Capybara::ExpectationNotMet]
_verify_current_path(path, optional_filter_block, **options) do |query|
raise Capybara::ExpectationNotMet, query.failure_message unless query.resolves_for?(self)
end
end
def has_current_path?(path, **options, &optional_filter_block)
# @return [Boolean]
make_predicate(options) { assert_current_path(path, **options, &optional_filter_block) }
end
Import
require 'capybara'
# Available on Capybara::Session instances
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| path | String/Regexp | Yes | Expected path, URL, or pattern |
| url | Boolean | No | Force full URL comparison (auto-detected from path format) |
| ignore_query | Boolean | No | Ignore query string in comparison |
| wait | Numeric | No | Override default wait time |
Outputs
| Name | Type | Description |
|---|---|---|
| assert_current_path | true | Returns true or raises ExpectationNotMet |
| has_current_path? | Boolean | Returns true/false |
Usage Examples
Path Assertions
# Path comparison
expect(page).to have_current_path('/dashboard')
expect(page).to have_current_path('/users/1/edit')
# Full URL comparison (auto-detected)
expect(page).to have_current_path('http://example.com/login')
# Regex matching
expect(page).to have_current_path(/\/users\/\d+/)
# Ignore query string
expect(page).to have_current_path('/search', ignore_query: true)
# With wait time override
expect(page).to have_current_path('/result', wait: 10)