【问题标题】:CasperJS injected client javascript not being loaded fully?CasperJS 注入的客户端 javascript 没有被完全加载?
【发布时间】:2015-06-24 17:40:16
【问题描述】:

我目前正在使用

注入一个 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 请求的同一页面上进行测试。有谁知道如何解决这种不一致?

【问题讨论】:

    标签: javascript jquery ajax phantomjs casperjs


    【解决方案1】:

    知道了!因此,在似乎只有部分脚本被加载的地方,当它到达 $(document).ajaxStop() 时确实存在错误,因为页面上尚未加载 jQuery。 CasperJS 有时会在加载 jQuery 后注入包含文件,这就是它有时工作的原因。

    通过下载 jQuery 并在包含文件之前手动注入来修复它:

    casper.options.clientScripts.push('./jquery-2.1.4.min.js')
    casper.options.clientScripts.push('./include.js')
    

    【讨论】:

      猜你喜欢
      • 2014-12-08
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 2012-05-24
      • 2011-11-10
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多