Skip to content

Instantly share code, notes, and snippets.

@xxxxlr
Forked from artjomb/1_phantomErrors.js
Created July 10, 2016 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xxxxlr/facf3426530251c024b41ae00765fd9f to your computer and use it in GitHub Desktop.
Save xxxxlr/facf3426530251c024b41ae00765fd9f to your computer and use it in GitHub Desktop.
Error event handlers for PhantomJS and CasperJS: PhantomJS and CasperJS don't show errors on the page by default. This can give clues as to what did go wrong.
var page = require('webpage').create(),
url = 'http://example.com/';
// Put the event handlers somewhere in the code before the action of
// interest (opening the page in question or clicking something)
// http://phantomjs.org/api/webpage/handler/on-console-message.html
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
// http://phantomjs.org/api/webpage/handler/on-error.html
page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
console.error(msgStack.join('\n'));
};
// http://phantomjs.org/api/webpage/handler/on-resource-error.html
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + ' URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
// http://phantomjs.org/api/webpage/handler/on-resource-timeout.html
page.onResourceTimeout = function(request) {
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
};
// use other helpful event handlers to debug the page behavior are:
// onNavigationRequested
// onResourceReceived
// onResourceRequested
// onUrlChanged
page.open(url, function(status) {
console.log('Status: ' + status);
phantom.exit();
});
var casper = require('casper').create({
// these options only show some behavior of the page, but rarely any helpful errors
verbose: true,
logLevel: "debug"
});
var url = 'http://example.com/';
// http://docs.casperjs.org/en/latest/events-filters.html#remote-message
casper.on("remote.message", function(msg) {
this.echo("Console: " + msg);
});
// http://docs.casperjs.org/en/latest/events-filters.html#page-error
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg);
// maybe make it a little fancier with the code from the PhantomJS equivalent
});
// http://docs.casperjs.org/en/latest/events-filters.html#resource-error
casper.on("resource.error", function(resourceError) {
this.echo("ResourceError: " + JSON.stringify(resourceError, undefined, 4));
});
// http://docs.casperjs.org/en/latest/events-filters.html#page-initialized
casper.on("page.initialized", function(page) {
// CasperJS doesn't provide `onResourceTimeout`, so it must be set through
// the PhantomJS means. This is only possible when the page is initialized
page.onResourceTimeout = function(request) {
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
};
});
casper.start(url).run();
var phantom = require('phantom');
phantom.create(function (ph) {
ph.createPage(function (page) {
// Put the event handlers somewhere in the code before the action of
// interest (opening the page in question or clicking something)
// See https://github.com/sgentle/phantomjs-node#functionality-details for proper use of callbacks
// http://phantomjs.org/api/webpage/handler/on-console-message.html
page.set('onConsoleMessage', function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
});
// http://phantomjs.org/api/webpage/handler/on-error.html
page.set('onError', function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
console.error(msgStack.join('\n'));
});
// http://phantomjs.org/api/webpage/handler/on-resource-error.html
page.set('onResourceError', function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + ' URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
});
// http://phantomjs.org/api/webpage/handler/on-resource-timeout.html
page.set('onResourceTimeout', function(request) {
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
});
page.open("https://www.google.com", function (status) {
console.log("opened google? ", status);
page.evaluate(function () { return document.title; }, function (result) {
console.log('Page title is ' + result);
ph.exit();
});
});
});
}, /* on Windows only: */ {
dnodeOpts: {
weak: false
}
});
var Horseman = require('node-horseman');
var horseman = new Horseman();
horseman
.on("consoleMessage", function(msg){
console.log('CONSOLE: ' + msg);
})
.on("error", function(msg, trace){
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
console.error(msgStack.join('\n'));
})
.on("resourceError", function(resourceError){
console.log('Unable to load resource (#' + resourceError.id + ' URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
})
.on("resourceTimeout", function(request){
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
})
.open('http://www.google.com')
.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment