Reputation: 481
I am writing an automation test program which should test any site.
I want to click some element of document, but before that I want to verify that no any AJAX call is running.
I'll only have document, not any AJAX call URLs or something like that.
Is there any ways so I can verify on document that No AJAX requests are being executed.
How can I do that?
Upvotes: 1
Views: 1060
Reputation: 3106
You can use technique below:
var loadingarray=[];
function GetCommand(id, sid) {
if (loadingarray[id]) {return false;}
loadingarray[id] = true;
getCommandResponse = $.ajax({
//send id to server
....
....
});
}
function AjaxFinishedGet(xml, status) {
//retrieve id from server in xml
loadingarray[id] = false;
...
...
}
and then you can simple check loadingarray has true value. If it has; some of ajax call running with id. If it hasn't you know that there isn't any ajax process on run.
Upvotes: 0
Reputation: 6769
jQuery has several AJAX event handlers. If you set ajaxIsExecuting
to true on ajaxStart
and set it to false on ajaxStop
, you should have some idea of whether AJAX is executing or not.
Upvotes: 1
Reputation: 1201
Maybe you don't need javascript either? So you can use this kind of code;
javascript:void(d=document);if(frames.length){alert('Script doesnt work in frame');}else
{while((el=d.getElementsByTagName('script')).length
{el[0].parentNode.removeChild(el[0]);};onerror=function(){};d.close();}
Upvotes: 0
Reputation: 39763
Simple answer: you can't. HTTP is stateless, the browser can't know if a request has been sent and if we are still waiting for a reply.
The only one who can know, is the application itself, using the application logic. Set a variable when you're waiting for an AJAX reply, add to a list, increase a counter... you can then 'watch' for variables.
I'm afraid I don't know of an elegant way to achieve this. Encapsulating all AJAX calls in a method and using the method instead might work - you can disable the testing code when you go live.
Upvotes: 1