【发布时间】:2016-08-04 20:21:45
【问题描述】:
我目前正在尝试使用 PhantomJS 和 CasperJS 进行端到端测试。我遇到的是 PhantomJS 缺乏承诺的情况。目前我们的项目实现了它们。该应用程序仅在原生支持 Promise 的 Google Chrome 中使用。
在运行我的测试时收到错误: 错误:ReferenceError:找不到变量:Promise
这似乎是因为 PhantomJS 中当前版本的 Webkit 不支持 Promise。我意识到 SlimerJS 确实通过 Gecko 提供了这种支持,但是我们的应用程序在 Chrome 中运行,因此我希望在 Webkit 中进行测试。
我一直在努力将 ES6 承诺 polyfill 注入 Phantom,以便正确进行测试。我已经使用了 Casper JS 的 injectjs 和 casper.options.clientScripts.push - 两者似乎仍然会导致这种缺乏对 promises 的支持的问题。
我注意到其他人在 CasperJS 的 github 支持中表示他们已经通过 polyfill 使其工作,但我不确定他们是如何做到的,因为没有提供示例。
我已经包含了我当前脚本的示例。如果有人处理过这个问题并找到解决方法,我们将不胜感激。提前谢谢!
casper.test.begin('Example test loading', 3, function(test) {
casper.options.clientScripts.push("node_modules/es6-promise/es6-promise.js");
casper.start('http://localhost:8080/', function() {
this.captureSelector('stuff.png', 'html');
});
casper.on("remote.message", function(msg) {
this.echo("Console: " + msg);
});
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg);
});
casper.on("resource.error", function(resourceError) {
this.echo("ResourceError: " + JSON.stringify(resourceError, undefined, 4));
});
casper.on("page.initialized", function(page) {
page.onResourceTimeout = function(request) {
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
};
});
casper.then(function() {
test.assertTitle('Example Title', 'Example title is incorrect');
});
casper.run(function() {
test.done();
});
});
【问题讨论】:
-
clientScripts对于您的用例而言,可能为时已晚。 -
作为对面临此问题的其他任何人的说明 - 我为此项目切换到 slimerJS。然而,phantomJS 确实计划支持原生的 Promise,并且迟早会发布。您可以在此处查看该问题:github.com/ariya/phantomjs/issues/14166 - 开发人员 (Vitallium) 对此问题进行了大约一半。
标签: javascript testing phantomjs ecmascript-6 casperjs