【问题标题】:CasperJS doesn't evaluate jQuery methodCasperJS 不评估 jQuery 方法
【发布时间】:2015-07-22 14:43:45
【问题描述】:

我将 jQuery 注入 CasperJS:

phantom.injectJs('./utils/jquery/jquery-2.1.4.js');

但是当我尝试评估一些 jQuery 代码时,它被忽略了: 示例:

function dragNdropAlertToActivity() {
    var tt = casper.evaluate(function() {
        $('div[id^="scheduler-alert-grid"] table:contains(BLUE ALERT)')[0].simulate("drag-n-drop", {
            dragTarget: {
                dx: 71,
                dy: 71,
                interpolation: {
                    stepCount: 2
                }
            }
        });
        return "done";
    });
    casper.echo(tt);
};

调用方法如:casper.test.begin(function(){...})。 测试使用:casperjs test tests

输出显示找不到$

当我写一个简单的选择器时,为什么它会忽略 jQuery?

【问题讨论】:

  • 看起来不错。你的脚本的输出是什么?请注册resource.errorpage.errorremote.messagecasper.page.onResourceTimeout 活动 (Example)。可能有错误。
  • 它找不到变量$,我应该以某种方式额外传递它吗?
  • 为什么不使用casper.options.clientScripts
  • 它说我尝试访问只读变量,如果我使用'casper.options.clientScript'

标签: javascript jquery testing casperjs


【解决方案1】:

phantom.injectJs() 文档清楚地表明(强调我的):

将指定文件中的外部脚本代码注入 Phantom 外部空间。

这意味着 jQuery 没有注入到您尝试使用它的页面上下文中。

您可以使用手动方法 (ref):

casper.then(function doSomething() {
    this.page.injectJs('relative/local/path/to/jquery.js');
    var tt = this.evaluate(function () {
        // ...
    });
});

或在您访问的每个页面上注入脚本的常规 CasperJS 方法:

var casper = require("casper").create({
    clientScripts: ["relative/local/path/to/jquery.js"]
});

casper.options.clientScripts.push("relative/local/path/to/jquery.js");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多