【问题标题】:CasperJS / PhantomJS ES6 Promise PolyfillCasperJS / PhantomJS ES6 Promise Polyfill
【发布时间】: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


【解决方案1】:

我偶然发现了同样的问题,即在 PhantomJS 中没有适当的 ES6 支持。我也尝试过使用es6-promise 来解决这个问题,并且和你一样,仍然有未定义的Promises。

将其替换为 babel-polyfill 解决了问题。

一旦你这样做了

npm install --save-dev babel-polyfill

您可以将clientScripts 替换为

casper.options.clientScripts.push("node_modules/babel-polyfill/dist/polyfill.js")

注意:我没有花时间去理解为什么 es6-promise 会出现问题,而我在这个代码库中唯一的 ES6 功能是 Promises,所以是 IMMV。

【讨论】:

  • 不为我工作仍然得到错误:ReferenceError:找不到变量:设置
  • @MuneebZulfiqar 这似乎是一个不同的问题。我建议提出一个新问题。
猜你喜欢
  • 2016-03-22
  • 2015-06-06
  • 1970-01-01
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多