Last active
April 5, 2022 22:10
-
-
Save artjomb/4cf43d16ce50d8674fdf 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
thx man