Casperjs:不能在自己的函数中使用casper.evaluate()

时间:2016-07-07 06:26:33

标签: casperjs evaluate

与casperjs我填写textarea并触发一个事件:

casper.thenClick( "#project-bar-yes-no-modal.in button.modal-no", function() { 
    casper.evaluate(function(){
        $("#project-button-textarea-for-testing").val( "// This is a test program\nellipse(100, 100, 100, 102);\n\nrect(50, 50, 50, 50);" ).trigger("set-live-editor");
    });
    this.wait(200);
} );

工作正常。当我尝试使用函数执行相同操作时,不会调用casper.evaluate()。

casper.setLiveEditorCode = function( code ) {
    utils.dump("1");
    casper.evaluate(function(){
        utils.dump("2");
        $("#project-button-textarea-for-testing").val( code ).trigger("set-live-editor");
    });
};

casper.thenClick( "#project-bar-yes-no-modal.in button.modal-no", function() { 
    casper.setLiveEditorCode( "ellipse(100, 100, 100, 101);\n\n" );
    this.wait(200);
} );

" 1"在输出中显示," 2"不是。为什么呢?

1 个答案:

答案 0 :(得分:0)

简而言之,评估中的代码由浏览器执行,并且utils对象未在浏览器的js环境中定义...

casper.setLiveEditorCode = function( code ) {
    utils.dump("1");//here is casperjs js environment, and utils is imported by casperjs bootstrap.js
    casper.evaluate(function(){
        utils.dump("2"); //here browser js environment, and utils is not defined in that scope...
    });
};

请阅读official docs关于evaluate的详细信息。

相关问题