Debugging PhantomJS webpage.open errors

In PhantomJS, webpage.open makes a callback with a status parameter that is set to success or failure. According to the documents, it will be “success” if there are no network errors, otherwise “failure”. Is there a way to see the network error that caused the failure?

The URL I'm trying to download works fine when I put it in my browser, and when I take a screenshot after receiving the error message, I see the page I was on before I called webpage.open (so I can't just ignore failure). I use Phantom for testing, so ideally I would like to get a reliable way to get a useful error when webpage.open fails (or better still it never fails!)

+44
phantomjs
May 11 '13 at 14:03
source share
1 answer

Found this post explaining how to set up callbacks to determine the cause of the failure: http://newspaint.wordpress.com/2013/04/25/getting-to-the-bottom-of-why-a-phantomjs- page-load-fails /

Based on this page, you can print errors as follows:

page.onResourceError = function(resourceError) { console.error(resourceError.url + ': ' + resourceError.errorString); }; 

The page will show an example of detailed logging for phantoms.

 var system = require('system'); page.onResourceRequested = function (request) { system.stderr.writeLine('= onResourceRequested()'); system.stderr.writeLine(' request: ' + JSON.stringify(request, undefined, 4)); }; page.onResourceReceived = function(response) { system.stderr.writeLine('= onResourceReceived()' ); system.stderr.writeLine(' id: ' + response.id + ', stage: "' + response.stage + '", response: ' + JSON.stringify(response)); }; page.onLoadStarted = function() { system.stderr.writeLine('= onLoadStarted()'); var currentUrl = page.evaluate(function() { return window.location.href; }); system.stderr.writeLine(' leaving url: ' + currentUrl); }; page.onLoadFinished = function(status) { system.stderr.writeLine('= onLoadFinished()'); system.stderr.writeLine(' status: ' + status); }; page.onNavigationRequested = function(url, type, willNavigate, main) { system.stderr.writeLine('= onNavigationRequested'); system.stderr.writeLine(' destination_url: ' + url); system.stderr.writeLine(' type (cause): ' + type); system.stderr.writeLine(' will navigate: ' + willNavigate); system.stderr.writeLine(' from page\ main frame: ' + main); }; page.onResourceError = function(resourceError) { system.stderr.writeLine('= onResourceError()'); system.stderr.writeLine(' - unable to load url: "' + resourceError.url + '"'); system.stderr.writeLine(' - error code: ' + resourceError.errorCode + ', description: ' + resourceError.errorString ); }; page.onError = function(msg, trace) { system.stderr.writeLine('= onError()'); var msgStack = [' ERROR: ' + msg]; if (trace) { msgStack.push(' TRACE:'); trace.forEach(function(t) { msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : '')); }); } system.stderr.writeLine(msgStack.join('\n')); }; 
+68
May 23 '13 at 23:55
source share



All Articles