How can I check the number of AJAX calls made in the integration test?

I have a remote form that disables the submit button while an AJAX request is being executed. I would like to verify that if I hit the button, there will be no other AJAX requests. How can I test this in an integration test?

+7
source share
2 answers

you can check active ajax calls with jquery

$.active 

If you use capybara for integration tests

 page.evaluate_script('$.active').should be <= 1 

may be a solution.

As you probably don’t know when calls arise, a helper function can do the trick

 def test_until(seconds=5) start_time = Time.now while (Time.now - start_time) <= seconds do yield sleep 0.05 end end 

which can be called so

 test_until do page.evaluate_script('$.active').should be <= 1 end 

So you test 5 seconds if there is another ajax call active

+4
source

Save a global or static variable. Increase the variable inside a function called Ajax and check what the value of your variable will be. It should never exceed one, ensuring that an Ajax call is made only once.

If you use automatic testing, this gives you more information about checking the number of active ajax calls and other products.

http://hedleyproctor.com/2012/07/effective-selenium-testing/

Hope this helps. Thanks.

+1
source

All Articles