CasperJS / PhantomJS ES6 Promise Polyfill

I am currently working on creating end-to-end tests using PhantomJS and CasperJS. I came across a situation where PhantomJS lacks promises. Our project is currently implementing them. The application is used only in Google Chrome, which supports promises natively.

When I run my tests, I get the error message: Error: ReferenceError: cannot find the variable: Promise

It looks like the current version of Webkit in PhantomJS does not support promises. I understand that SlimerJS has this support through Gecko, however our application works in Chrome, and therefore I would like the tests to run in Webkit.

What I was struggling with was inserting the ES6 polyfill promise into Phantom so that the test passes correctly. I used both Casper JS injectjs and casper.options.clientScripts.push - both seem to return this lack of support for the promises problem.

I noticed that others have stated in support of github from CasperJS that they got it to work through polyfill, but I'm not sure how they did it, as examples are not provided.

I have included an example of my current script. If someone dealt with this problem and found a way to solve this problem, it would be very helpful. Thank you in advance!

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(); }); }); 
+7
javascript ecmascript-6 testing phantomjs casperjs
source share
1 answer

I came across the same problems as some ES6 support in PhantomJS. I also tried to overcome the problem using es6-promise , and like you, there was still undefined Promise s.

Replacing it with babel-polyfill fixes the problems.

As soon as you do

 npm install --save-dev babel-polyfill 

you can replace clientScripts with

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

Note. . I did not take the time to understand why there were problems with es6-promise , and the only ES6 function that I have in this code base is Promises, therefore IMMV.

+5
source share

All Articles