Overview
Capybara::Queries::CurrentPathQuery matches the current browser URL or path against an expected value, supporting both exact string comparison and regular expression matching.
Description
CurrentPathQuery extends BaseQuery to verify that a session's current URL or request path matches an expected path or pattern. It uses the Addressable::URI library for robust URL parsing and comparison. When the expected path is a Regexp, the query tests with match?; when it is a String, it performs a structural URI comparison via Addressable::URI.parse. The query automatically detects whether to compare full URLs or just request URIs based on whether the expected path contains a hostname. The :ignore_query option strips the query string before comparison, and an optional filter block can be passed for custom URL validation logic. Valid option keys are :wait, :url, and :ignore_query.
Usage
Use CurrentPathQuery through Capybara's path assertions such as have_current_path and assert_current_path. It is the underlying query for all current-path matching in Capybara sessions.
Code Reference
Source Location
Signature
class Capybara::Queries::CurrentPathQuery < Capybara::Queries::BaseQuery
def initialize(expected_path, **options, &optional_filter_block)
def resolves_for?(session)
def failure_message
def negative_failure_message
end
Import
require 'addressable/uri'
require 'capybara/queries/current_path_query'
I/O Contract
| initialize(expected_path, **options, &optional_filter_block)
|
| Parameter |
Type |
Required |
Description
|
expected_path |
String or Regexp |
Yes |
The expected path, URL, or regular expression to match against
|
options[:url] |
Boolean |
No |
Compare full URL instead of request URI; auto-detected if expected_path contains a hostname
|
options[:ignore_query] |
Boolean |
No |
Strip the query string before comparison (default: false)
|
options[:wait] |
Numeric |
No |
Maximum wait time in seconds
|
&optional_filter_block |
Block |
No |
Optional block receiving the parsed URI for custom validation; must return truthy
|
Returns: CurrentPathQuery instance
|
| resolves_for?(session)
|
| Parameter |
Type |
Required |
Description
|
session |
Capybara::Session |
Yes |
The session whose current_url will be tested
|
Returns: Boolean -- true if the session's current URL matches the expected path and filter block
|
| failure_message / negative_failure_message
|
| Parameter |
Type |
Required |
Description
|
| (none)
|
Returns: String -- e.g., "expected \"/users\" to equal \"/posts\"" or "expected \"/users\" to match /\\/posts/"
|
Usage Examples
# Exact path matching (via Capybara matchers):
expect(page).to have_current_path('/dashboard')
# Full URL matching:
expect(page).to have_current_path('https://example.com/dashboard', url: true)
# Regexp matching:
expect(page).to have_current_path(/\/users\/\d+/)
# Ignoring query string:
expect(page).to have_current_path('/search', ignore_query: true)
# With a filter block for custom validation:
expect(page).to have_current_path('/items') { |uri|
uri.query_values&.key?('page')
}
# Underlying query usage (internal):
query = Capybara::Queries::CurrentPathQuery.new('/dashboard', wait: 3)
query.resolves_for?(page) # => true/false
query.failure_message # => "expected \"/other\" to equal \"/dashboard\""
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.