dalekjs / dalek

[unmaintained] DalekJS Base framework
MIT License
695 stars 63 forks source link

Waiting for AJAX call to finish #131

Open burt202 opened 10 years ago

burt202 commented 10 years ago

Hi,

Hopefully this'll be the last issue raised by me for now (trying desperately not to overload you guys). But another thing I want to be able to do is to wait for an AJAX call to finish before continuing with the assertions etc. I have a mechanism in my app that adds a class to the <html> tag on ajaxStart and removes it on ajaxComplete so I thought I could hook into this using a combination of waitFor and an assert (within the function) but it doesnt seem to work. Any ideas?

Im using v0.0.8, let me know if you can think of any better way of achieving this

Cheers

mdix commented 10 years ago

Hi,

should be possible. From the docs:

  test.open('http://adomain.com')
     .waitFor(function () {
       return window.myCheck === true;
     })
     .done();

waitFor waits until the function inside returns true. So all you have to do is to add an interval inside this function and check if the class on the <html> tag still exists. If you have jQuery at hand, its pretty easy, else you'll have to find a JS solution:

if (!$('body').hasClass('ajaxRunning')) {
    return true;
}

If it's gone, clear the interval and return true.

Best Marc

burt202 commented 10 years ago

Hi, thanks for the response. I like your solution here but I havent been able to try it as even a simple waitFor hangs on my machine. Im on Ubuntu 14.04 and using phantomJS. The code that hangs is below:

 .waitFor(function () {
     return true;
 })

Im sure your solution will work but Im tempted not to close this issue until Ive heard back about waitFor functionality.

Cheers in the meantime

mdix commented 10 years ago

Hi,

ok, thats quite bad, but I have another solution in mind. Would you mind telling me wether you want to wait with the whole test (e. g. start the test once the class has been removed from the <body>) or if you need to, I'd call it 'break the test chain' and wait for the removal of that class? I think I can provide you with a solution for both requirements.

Best Marc

burt202 commented 10 years ago

My use case it to need this during the test or as you put it 'breaking the test chain'.

Cheers

rodneyrehm commented 10 years ago

Not that I have looked at the code behind waitFor, but is it possible some ES5 feature got in there somehow and your PhantomJS is failing because it can't deal with ES5 unless you load the es5-shim?

vectart commented 9 years ago

Hi there, It's possible to determine number or active AJAX requests performed via jQuery. Just use $.active in waitFor callback:

test.open('http://www.example.com')
     .waitFor(function () {
       return !$.active;
     })
     .done();