I have a test that checks if, after the user chooses an option from the autocomplete suggestions, the path has changed. I'm testing with Capybara on Poltergeist. However, presumably because I'm triggering the page change trough JS, I have to use a sleep statement now in order to wait for the page to finish loading, which of course isn't ideal.
This is the js:
window.location.href = suggestion.url;
And this is the test:
sleep 1# Allow the time to navigate
expect(current_path).to eq(arg1)
where arg1 is the expected page url.
It works fine with sleep, but the reasons why this isn't optimal are obvious. How to make the test smarter and have it wait for the navigation to take place?
Don't use the eq
matcher with current_path (with most Capybara things actually), use the have_current_path matcher
expect(page).to have_current_path(arg1)
which will utilize the automatic wait/retry behavior built into Capybara
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments