How to get current path with query string using Capybara
I've updated this answer to reflect modern conventions in capybara. I think this is ideal since this is the accepted answer, and what many people are being referred to when looking for a solution. With that said, the correct way to check the current path is to use the has_current_path?
matcher provided by Capybara, as documented here: Click Here
Example usage:
expect(page).to have_current_path(people_path(search: 'name'))
As you can see in the documentation, other options are available. If the current page is /people?search=name
but you only care that it's on the /people
page regardless of the param, you can send the only_path
option:
expect(page).to have_current_path(people_path, only_path: true)
Additionally, if you want to compare the entire URL:
expect(page).to have_current_path(people_url, url: true)
Credit to Tom Walpole for pointing out this method.
I replaced the _path method with _url to achieve comparing the full urls with parameters.
current_url.should == people_url(:search => 'name')
Just updating this question for modern times. The current best practice for checking current_paths when using Capybara 2.5+ is to use the current_path matcher, which will use Capybaras waiting behavior to check the path. If wanting to check against the request_uri (path and query string)
expect(page).to have_current_path(people_path(:search => 'name'))
If only wanting the path part (ignoring the query string)
expect(page).to have_current_path(people_path, only_path: true) # Capybara < 2.16expect(page).to have_current_path(people_path, ignore_query: true) # Capybara >= 2.16
If wanting to match the full url
expect(page).to have_current_path(people_url, url: true) # Capybara < 2.16expect(page).to have_current_path(people_url) # Capybara >= 2.16
the matcher will take a string which is compared with == or a regex to match against
expect(page).to have_current_path(/search=name/)