Helping CasperJS 1.1.0-beta3 play nice with PhantomJS 2.0.0

As of today, 30-SEP-2015, the latest build for CasperJS 1.1.0-beta3. Not exactly comforting for production use, but it is the most recent and decently capable version available outside of a pull request. So.. let’s get started:

First, I checked the current version of

[root@ip-10-153-205-78 ~]# casperjs –version
1.1.0-beta3
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/local/lib/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.

NOTE:You may notice that simply running casper causes PhantomJS to hurl out worthless warning messages. This is the very reason I’m undergoing this exercise.

Next, I dropped the new binary on target server and verified that I am dealing with pJS 2.0, and it’s functioning:

[root@ip-10-153-205-78 ~]# phantomjs -v
2.0.0

Next step is to get to the meat of the errors.

The first I encountered was this:

[root@ip-10-153-205-78 ~]# casperjs –version
CasperJS needs PhantomJS v1.x

/usr/local/lib/node_modules/casperjs/bin/bootstrap.js:91 in __die

Opening this file, around line 91 the following is found:

function __die(message) {
if (message) {
console.error(message);
}
phantom.exit(1);

Tracing back to the caller, the test is performed here:

(function(version) {
// required version check
if (version.major !== 1) {
return __die(‘CasperJS needs PhantomJS v1.x’);
} if (version.minor < 8) { return __die('CasperJS needs at least PhantomJS v1.8 or later.'); } if (version.minor === 8 && version.patch < 1) { return __die('CasperJS needs at least PhantomJS v1.8.1 or later.'); } })(phantom.version);

Next I tried to make it accept version 2, by changing that block to this:

(function(version) {
if (version.major == 1) {
if (version.minor < 8) { return __die('CasperJS needs at least PhantomJS v1.8 or later.'); } if (version.minor === 8 && version.patch < 1) { return __die('CasperJS needs at least PhantomJS v1.8.1 or later.'); } } if (version.major < 2) { return __die('CasperJS needs PhantomJS v1.x or v2.x'); } })(phantom.version);

Next error message was this:

[root@ip-10-153-205-78 ~]# casperjs –version
Couldn’t find nor compute phantom.casperPath, exiting.

/usr/local/lib/node_modules/casperjs/bin/bootstrap.js:91 in __die

It’s origin was in this block:

// CasperJS root path
if (!phantom.casperPath) {
try {
phantom.casperPath = phantom.args.map(function _map(arg) {
var match = arg.match(/^–casper-path=(.*)/);
if (match) {
return fs.absolute(match[1]);
}
}).filter(function _filter(path) {
return fs.isDirectory(path);
}).pop();
} catch (e) {
return __die(“Couldn’t find nor compute phantom.casperPath, exiting.”);
}
}

Based upon information found in this post.. Latest pull of casperjs not working with latest pull of phantomjs2 I made the following modifications:

Paste this section of code in above the first non-comment section in bootstrap file:

// Mods to get Casper and PhantomJS playing nice
var system = require(‘system’);
var argsdeprecated = system.args;
argsdeprecated.shift();
phantom.args = argsdeprecated;

Once you have done that, you should be able to use Casper 1.1.0-beta3 with PhantomJS 2.0 (and look.. NO MORE LAME WARNINGS!!!)

[root@ip-10-153-205-78 ~]# casperjs –version
1.1.0-beta3

One thought on “Helping CasperJS 1.1.0-beta3 play nice with PhantomJS 2.0.0”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.