首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CasperJS注入客户端javascript没有完全加载?

问CasperJS注入客户端javascript没有完全加载?
EN

Stack Overflow用户
提问于 2015-06-25 01:40:16
回答 1查看 875关注 0票数 1

我目前正在使用以下命令注入include.js

代码语言:javascript
复制
casper.options.clientScripts.push('./include.js')

下面是include.js (用来确保完成所有ajax请求:Using $.ajaxStop() to determine when page is finished loading in CasperJS)

代码语言:javascript
复制
(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脚本

代码语言:javascript
复制
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请求的同一页面上进行了测试。有没有人知道如何修复这种不一致?

EN

回答 1

Stack Overflow用户

发布于 2015-06-25 03:17:38

明白了!因此,在似乎只加载了部分脚本的地方,因为jQuery还没有加载到页面上,所以当它到达$(document).ajaxStop()时,实际上就出现了一个错误。在加载jQuery之后,CasperJS有时会注入包含文件,这就是它有时会正常工作的原因。

修复方法:下载jQuery,在包含文件前手动注入:

代码语言:javascript
复制
casper.options.clientScripts.push('./jquery-2.1.4.min.js')
casper.options.clientScripts.push('./include.js')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31033375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档