I'm new to writing tests in capybara and I'm having trouble getting the current URL of a page. I wrote it like this:
url = page.current_url + page.current_path
Somehow its just returning the base URL. Help is much appreciated.
I'm new to writing tests in capybara and I'm having trouble getting the current URL of a page. I wrote it like this:
url = page.current_url + page.current_path
Somehow its just returning the base URL. Help is much appreciated.
Try this:
url = URI.parse(current_url)
current_url
gives the exact same response as the line above ^^ –
Lindbom from capybara session doc:
Fully qualified URL of the current page
def current_url
driver.current_url
end
Path of the current page, without any domain information
def current_path
URI.parse(current_url).path
end
I think that what you are doing is not right
you could use have_current_path
:
expect(page).to have_current_path(new_user_path)
before seeing that I was doing something like:
def current_path
current_uri = URI.parse(page.current_url)
current_path = current_uri.path
current_path += "?#{current_uri.query}" if current_uri.query.present?
current_path
end
You can use the has_current_path
matcher and match for full URLs.
expect(page).to have_current_path('https://example.com', url: true)
Have a look at the Capybara test suite for more examples:
has_current_path_spec.rb#L54
.
© 2022 - 2025 — McMap. All rights reserved.
url = URI.parse(current_url).to_s
– Kreindler