Capybara wait_for_ajax replacement for turbo-stream responses

I found a solution, which works for me at least.

In that article I mentioned a while back it talks about how wait_for_ajax really isn’t the right solution anyway and that the right approach was to use Capybara’s find which will wait for the elements to come into view. I didn’t really understand how this works at first, but I’ve been playing around with it, and it is starting to make sense to me.

So what I’ve been doing is this - when I want to wait for Turbo to load a new view, I identify an element that only appears on the to-be-loaded view, and I set up a find for that element - something like this

link_to_new_view.click

find('.element_that_only_exists_on_next_view')

expect(thing).to eq(value_that_we_needed_to_wait_for)

The find will wait until the element comes into view, which is pretty much what wait_for_ajax is trying to do anyway, but this is more specific and works really well once you start down this path.

I’m mostly using this with turbo-frames rather than turbo-streams but I think it will work the same way
with anything that changes the dom. You just need to know what you’re going to be looking for.

1 Like