I have a step_definition
Then(/^I should be redirected to the (.+?) page/) do |target|
expect(current_url).to eq(Urls[target])
end
and generally it works quite well. Sometimes when I'm using the poltergeist driver it goes faster than normal and the current_url is still the old page. That's when I get errors like this:
Then I should be redirected to the login page # features/step_definitions/navigate-steps.rb:64
expected: "http://example.com/"
got: "http://example.com/reset-password"
(compared using ==)
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/navigation.rb:50:in `/^I should be redirected to the (.+?) page$/'
features/password.feature:100:in `Then I should be redirected to the login page'
Is there a way to make the matcher wait a little for the url to update?
Don't use the eq
matcher with current_path
or current_url
. Instead, use the have_current_path
matcher provided by Capybara 2.5+
expect(page).to have_current_path(Urls[target], url: true)
The have_current_path
matcher uses Capybara's waiting/retrying behavior, so it will wait for the page to change. I added the url: true
option to make it compare the full url. If Urls[target]
resolves to just a path, you can remove the option.