我目前正在使用以下命令注入include.js
casper.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请求的同一页面上进行了测试。有没有人知道如何修复这种不一致?
发布于 2015-06-25 03:17:38
明白了!因此,在似乎只加载了部分脚本的地方,因为jQuery还没有加载到页面上,所以当它到达$(document).ajaxStop()时,实际上就出现了一个错误。在加载jQuery之后,CasperJS有时会注入包含文件,这就是它有时会正常工作的原因。
修复方法:下载jQuery,在包含文件前手动注入:
casper.options.clientScripts.push('./jquery-2.1.4.min.js')
casper.options.clientScripts.push('./include.js')https://stackoverflow.com/questions/31033375
复制相似问题