Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a remote form, which disables the submit button while the AJAX request is in progress. I'd like to check that if I stress click the button, no other AJAX requests will be done. How could I check this in an integration test?

like image 225
Geo Avatar asked Dec 20 '12 15:12

Geo


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

might be a solution.

As you probably don't know when calls occur a helper function might 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

wich can be called like this

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

Thus you test for 5 seconds if there are ever more than one active ajax call

like image 79
rapimo Avatar answered Sep 29 '22 15:09

rapimo


Keep a global or a static variable. Increment the variable inside the function called by Ajax and check what will be the value of your variable. It should never exceed one, guaranteeing that Ajax call is made only once.

If you are onto automated testing, this provides you more info on checking number of ajax calls active and other stuffs.

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

Hope this helps. Thank you.

like image 36
LPD Avatar answered Sep 29 '22 14:09

LPD