CasperJS不评估jQuery方法 [英] CasperJS doesn't evaluate jQuery method
问题描述
我将jQuery注入了CasperJS:
I injected jQuery to CasperJS:
phantom.injectJs('./utils/jquery/jquery-2.1.4.js');
但是当我尝试评估一些jQuery代码时,它将被忽略: 例如:
but when I try to evaluate some jQuery code, it is ignored: example:
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
calling method like: casper.test.begin(function(){...})
.
test are executed using: casperjs test tests
输出显示找不到$
.
当我编写一个简单的选择器时,为什么它会忽略jQuery?
Why does it ignore jQuery, when I write even a simple selector?
推荐答案
phantom.injectJs()
文档明确指出(强调我的意思):
The phantom.injectJs()
documentation clearly says (emphasis mine):
将来自指定文件的外部脚本代码注入到幻影 外部 空间.
Injects external script code from the specified file into the Phantom outer space.
这意味着jQuery不会注入您尝试使用它的页面上下文中.
Which means that jQuery is not injected into the page context where you try to use it.
您可以使用手动方法(参考):
You can either use the manual approach (ref):
casper.then(function doSomething() {
this.page.injectJs('relative/local/path/to/jquery.js');
var tt = this.evaluate(function () {
// ...
});
});
或普通的CasperJS方法将脚本插入到您访问的每个页面上:
or the normal CasperJS approach which injects the script on every page that you visit:
var casper = require("casper").create({
clientScripts: ["relative/local/path/to/jquery.js"]
});
或
casper.options.clientScripts.push("relative/local/path/to/jquery.js");
这篇关于CasperJS不评估jQuery方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!