jeudi 14 avril 2016

How to make Capybara/Poltergeist wait for js initiated navigation to finish?

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?

Aucun commentaire:

Enregistrer un commentaire