I needed a quick way to wait for ajax to finish in my capybara-based tests, so I made a quick google search. I discovered that pivotallabs had the same issue, and used the following code:
That’s all well and good, but I didn’t really want to type that out every single time, so I made a helper!
I just make sure to include it in whatever *_spec.rb file I need to use it in. If you’re using cucumber, there are other ways to include it in World, I believe.
Here’s an example:
Awesome! My spec now waits patiently for all ajax on the faux-page to complete.
Backbone.js back helper
Upon further integration testing, I quickly discovered yet another need: a way to press “back” like a user would do quite naturally, even habitually… When I’m actually using my app, I sometimes pretend to be a click-happy dufus user and click all kinds of things in as many wrong ways as possible, and the back button is a great one. Pressing backspace when you’re not in a text field triggers it, as does one of those extra buttons on my mouse, and of course there’s the regular left-clicking it on the browser action bar. Integration testing a backbone.js app, which this one is, makes it almost a requirement to test. As it turns out, it’s really simple with browsers that support window.history: