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

Jasper Kennis

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?

Thomas Walpole

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to make JS wait for previous lines of code to finish first?

How to make Terraform wait for cloudinit to finish?

How to make Angular animations wait for rendering to finish

GNU make: How to wait "cp" command to finish

How to make forEach wait for subscribe to finish?

How to make parallel Tasks and wait for a Task to finish

How to make a method call wait for an animation to finish

How to wait function to finish in for loop js?

JS Wait for .CSS to finish

How to make "onAuthStateChanged" wait to "currentUser.updateProfile" to finish?

How do I make Vue wait for Firestore to finish downloading collection?

CMake: how to make execute_process wait for subdirectory to finish?

How to make several async (RX) calls, and wait for them to finish in Kotlin?

How do I make jQuery wait for a recursive function to finish?

How can I make "ng serve" wait for "ng build" to finish?

In c, how to make a thread wait for other threads to finish

How do I make applescript not wait for sound effect to finish?

How do I make code wait for previous lines to finish

How to make a batch file command wait for powershell script to finish

Make celery wait for task to finish

Make Jquery wait for function to finish

How to wait for subscribe to finish?

How to wait for a ThreadPoolExecutor to finish

How to wait for constructor to finish?

How to wait for a task to finish

How to wait for all async tasks to finish in Node.js?

How to wait for for loop with async requests to finish in node.js?

How to wait for function to finish before continuning in Node.js

Node.JS: how to wait for a process to finish before continuing?