teamcapybara/capybara · error · Capybara::ExpectationNotMet
query.failure_message
Error message
query.failure_message
What it means
Raised by Session#assert_current_path (and the have_current_path matcher) when the browser's current path, or full URL, does not equal or match the expected String/Regexp within the wait time (default Capybara.default_max_wait_time). Capybara retries the comparison until the timer expires, then raises Capybara::ExpectationNotMet carrying the query's failure_message, which shows the expected and actual values.
Source
Thrown at lib/capybara/session/matchers.rb:24
# Asserts that the page has the given path.
# By default, if passed a full url this will compare against the full url,
# if passed a path only the path+query portion will be compared, if passed a regexp
# the comparison will depend on the :url option (path+query by default)
#
# @!macro current_path_query_params
# @overload $0(string, **options)
# @param string [String] The string that the current 'path' should equal
# @overload $0(regexp, **options)
# @param regexp [Regexp] The regexp that the current 'path' should match to
# @option options [Boolean] :url (true if `string` is a full url, otherwise false) Whether the comparison should be done against the full current url or just the path
# @option options [Boolean] :ignore_query (false) Whether the query portion of the current url/path should be ignored
# @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for the current url/path to eq/match given string/regexp argument
# @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
# @return [true]
#
def assert_current_path(path, **options, &optional_filter_block)
_verify_current_path(path, optional_filter_block, **options) do |query|
raise Capybara::ExpectationNotMet, query.failure_message unless query.resolves_for?(self)
end
end
##
# Asserts that the page doesn't have the given path.
# By default, if passed a full url this will compare against the full url,
# if passed a path only the path+query portion will be compared, if passed a regexp
# the comparison will depend on the :url option
#
# @macro current_path_query_params
# @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
# @return [true]
#
def assert_no_current_path(path, **options, &optional_filter_block)
_verify_current_path(path, optional_filter_block, **options) do |query|
raise Capybara::ExpectationNotMet, query.negative_failure_message if query.resolves_for?(self)
end
endView on GitHub (pinned to 15b5fdb76e)
Solutions
- Assert the destination right after the action that navigates and rely on the built-in retry; on slow CI extend it with expect(page).to have_current_path('/dashboard', wait: 5).
- If the app appends query params you do not care about, add ignore_query: true.
- Pass only the path when you want path-only comparison; pass a full URL only when you want full-URL comparison.
- For dynamic segments use a Regexp: have_current_path(%r{/items/\d+}).
Example fix
# before
expect(page).to have_current_path('/search') # fails: actual is '/search?q=capybara'
# after
expect(page).to have_current_path('/search', ignore_query: true) Defensive patterns
Strategy: retry
Validate before calling
# Wait for a landmark of the destination page first, then assert the path
expect(page).to have_selector('h1', text: 'Dashboard')
expect(page).to have_current_path('/dashboard') Type guard
def on_path?(expected, **opts)
expected = expected.to_s
current = opts[:ignore_query] ? page.current_path : page.current_url
expected.start_with?('http') ? current == expected : current.end_with?(expected)
end Try / catch
begin
expect(page).to have_current_path('/dashboard', wait: 2)
rescue Capybara::ExpectationNotMet
retry_count = (defined?(retry_count) ? retry_count : 0) + 1
retry if retry_count < 2 # bounded retry for redirect chains on slow CI
raise
end Prevention
- Assert current_path immediately after the navigation action so Capybara's built-in wait does the retrying.
- Use ignore_query: true whenever the app appends tracking/timestamp query params.
- Prefer Regexp with anchors (%r{/items/\d+}) over exact strings for dynamic routes.
- Set Capybara.default_max_wait_time high enough for the slowest CI environment.
When it happens
Trigger: expect(page).to have_current_path('/dashboard') while the page is still on '/login'; page.assert_current_path(%r{/items/\d+}) for a record page that has not finished rendering; passing a full URL string (comparison then runs against the full current URL) while the actual query string differs, e.g. '/search?q=1' vs '/search?q=2'.
Common situations: Not waiting for asynchronous navigation or a redirect chain after click_button/visit; trailing-slash or query-string mismatches; passing a path but expecting full-URL comparison (or the reverse); slow CI environments where default_max_wait_time is exceeded.
Related errors
- query.negative_failure_message
- result.failure_message
- query.failure_message
- res.join(' or ')
- result.negative_failure_message
AI-assisted analysis of teamcapybara/capybara@15b5fdb76e (2026-08-21).
Data as JSON: /api/errors/227459865c2baaa8.
Report an issue: GitHub.