javascript - CasperJS/PhantomJS ES6 Promise Polyfill

标签 javascript testing phantomjs ecmascript-6 casperjs

我目前正在尝试使用 PhantomJS 和 CasperJS 来制作端到端测试。我遇到的是 PhantomJS 缺乏 promise 的情况。目前我们的项目实现了它们。该应用程序仅在原生支持 promises 的 Google Chrome 中使用。

在运行我的测试时我收到错误: 错误:ReferenceError:找不到变量:Promise

这似乎是因为 PhantomJS 中当前版本的 Webkit 不支持 promise 。我意识到 SlimerJS 确实通过 Gecko 获得了这种支持,但是我们的应用程序在 Chrome 中运行,因此我希望在 Webkit 中进行测试。

我一直在努力解决的问题是将 ES6 promise polyfill 注入(inject) 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();
    });
});

最佳答案

我偶然发现了 PhantomJS 中没有适当的 ES6 支持的相同问题。我也曾尝试通过使用 es6-promise 来解决这个问题,并且像您一样,仍然有未定义的 Promise

将其替换为 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。

关于javascript - CasperJS/PhantomJS ES6 Promise Polyfill,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603470/

相关文章:

javascript - 多个浏览器和页面对象模式

php - CasperJS 将数据传回 PHP

python-3.x - python+selenium+phantomjs 无法处理 `adding certification is strongly advised` 的 https url

javascript - Json数据修改为新的键值对JavaScript

javascript - 如何保护react+redux+reactRouter应用程序的客户端?

javascript - 将 Base64 图像上传到 .NET Web 服务并在那里进行转换

javascript - Chrome 工作区无法使用 Source Maps 和 Javascript

javascript - Faker.js 困惑

android - 如何测试依赖于 CountDownTimer 的 Android 类

ffmpeg - 使用 ffmpeg image2pipe 和 phantomjs 从网页屏幕截图渲染视频时出错