【发布时间】:2015-06-24 17:40:16
【问题描述】:
我目前正在使用
注入一个 include.jscasper.options.clientScripts.push('./include.js')
这里是include.js(这是为了确保所有ajax请求都完成:Using $.ajaxStop() to determine when page is finished loading in CasperJS)
(function(){
window._allAjaxRequestsHaveStopped = false;
var interval;
console.log('injecting include js')
$.ajaxSetup({'global':true});
$(document).ajaxStop(function() {
console.log('NO MORE outstanding ajax calls');
if (interval) {
clearInterval(interval);
interval = null;
}
interval = setTimeout(function() {
window._allAjaxRequestsHaveStopped = true;
}, 1000);
});
$(document).ajaxStart(function() {
console.log('NEW ajax call');
if (interval) {
clearInterval(interval);
interval = null;
}
});
console.log('include js loaded');
})();
问题是有时 ajaxStop 和 ajaxStart 函数不会被加载,但它总是输出第一个 console.log 'injecting include js'。
CasperJS 脚本
casper.waitForAjax = (callback) ->
fn_wait = ->
casper.evaluate ->
return window._allAjaxRequestsHaveStopped
casper.waitFor fn_wait, callback
casper.options.clientScripts.push('./include.js')
casper.options.waitTimeout = 10000
describe 'Main Test', ->
before ->
casper.start 'url', ->
phantom.clearCookies()
it 'Test Case 1', ->
casper.then ->
@click 'button'
casper.waitForAjax ->
// Move on with test
所以有时我的代码会超时等待 window._allAjaxRequestsHaveStopped 变为真,而其他时候它会正常工作。我还在我知道有 ajax 请求的同一页面上进行测试。有谁知道如何解决这种不一致?
【问题讨论】:
标签: javascript jquery ajax phantomjs casperjs