How to wait for jQuery Ajax requests to complete from WatiN?

后端 未结 3 440
臣服心动
臣服心动 2021-02-02 03:27

I\'m writing WatiN tests to test an Ajax web application and have come across a timing issue with Ajax requests.

After an Ajax request is triggered by an action on the

3条回答
  •  孤街浪徒
    2021-02-02 03:47

    This solution doesn't work very well because .ajaxStart is called only for the first Ajax request, while .ajaxComplete is called each time an ajax request is finished. if you run a this simple code in your console :

    $.ajax({url:"/"}); $.ajax({url:"/"})
    

    and add some logging in the .ajaxStart and .ajaxComplete handler methods, you can see that .ajaxStart handler will be called only once and .ajaxComplete handler twice. So ajaxRequestCount will become negative and all your design is screwed.

    I suggest that you use .ajaxSend instead of .ajaxStart if you want to keep your design.

    Another solution would be to use .ajaxStop instead of .ajaxComplete, but by doing so, you don't need the ajaxRequestCount, you only need a boolean that say if there are ajax requests running behind the scene.

    Very useful information can be found : http://api.jquery.com/category/ajax/global-ajax-event-handlers/

    Hope this helps.

提交回复
热议问题