4

I have a search interface with instant search as you type. It is optimized to send AJAX requests only when they are needed, so I want to test if it does it correctly.

How do I check if Ajax request was sent? (any request would be good enough) .. also would be good to check if Ajax request was aborted (in other testcase)

Aurimas
  • 2,577
  • 5
  • 26
  • 37

2 Answers2

4

With Capybara, you don't. As Xavier points out, Capybara is focused on emulating a user. A user doesn't know about ajax requests, etc they just see the results. If you have a spinner (or some other visual item) you display on screen while a request is happening then you can check on the presence of that (since a user would see it) or you can check that the correct results get displayed.

If instead you are trying to test the actual optimization behavior of the AJAX requests, then you probably want to be looking at JS testing with something like jasmine/jasmine-ajax.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Thanks for the spinner idea, I actually dim the opacity of the results, so I think it will work for me, let me check! – Aurimas Sep 10 '16 at 18:42
2

If you're using jQuery, jQuery.active function answers your question.

I'm pretty sure there isn't a way to get this with raw XHR request without some kind of tracking in your library code.

As a meta-point, capybara works best when you stick to asserting on user-observable behaviours. Could you perhaps verify that particular search results/suggestions appear, rather than checking whether an ajax request happened?

Community
  • 1
  • 1
Xavier Shay
  • 4,067
  • 1
  • 30
  • 54
  • I knew about jQuery.active, but somehow it does not seem to be reliable. The result after local filtering and filtering after AJAX might be the same (AJAX would just verify in backend that the result is in fact correct, but sometimes it's unnecessary). Good points though! – Aurimas Sep 10 '16 at 18:41