How to debug PhantomJS?

1,059 views
Skip to first unread message

b1th0ven

unread,
Aug 20, 2016, 9:37:44 AM8/20/16
to phantomjs
I have script test.js:
var page = require('webpage').create();
var fs = require('fs');
var url = 'http:://google.com';
page.open(url, function(status) {
        console.log('1');
phantom.exit();
});};
When I try to run this script in my console it gets hung and nothing happens: 
b1th0ven@compic:/var/www/test/js$ phantom print.js
How and where I can read about errors in script?

It's an example of my typically problem with PhantomJS. When script have an error I can't read about it anything... anywhere. How to debug PhantomJS?  

Sean McNamara

unread,
Aug 20, 2016, 11:12:12 AM8/20/16
to phan...@googlegroups.com
You have two colons in "http:://google.com" but you need only one.
> --
> You received this message because you are subscribed to the Google Groups
> "phantomjs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to phantomjs+...@googlegroups.com.
> Visit this group at https://groups.google.com/group/phantomjs.
> For more options, visit https://groups.google.com/d/optout.

Dawid Bartosz Janiga

unread,
Aug 20, 2016, 12:07:52 PM8/20/16
to phan...@googlegroups.com
Phantomjs (^2.1.1) has insufficient error handling IMHO, but there are ways to deal with it.

Errors in webpage
You can intercept any JavaScript errors thrown by WebPage instance (errors which normally you can see in Console tab in DevTools).
Example snippet with dealing it:
page.onError = function (msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function (t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : ''));
});
}
console.error(msgStack.join('\n'));
phantom.exit(1);
};
In Terminal
➜  phantomjs test.js
PHANTOM ERROR: Error: ni chuja
TRACE:
 -> http://localhost:8000/error.html: 2 (in function global code)

  phantomjs://code/test.js:13 in onError

Errors in phantomJS code

Unfortunately, phantomjs doesn't throw any syntax error, it just hung up. But there is smart way to check syntax. 
➜  node test.js

And results run on your code:
/Users/djaniga/Documents/dawidjaniga/atelier/test.js:7
});};
    ^
SyntaxError: Unexpected token ;
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:511:25)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:445:3

Hope that will be an answer for your question,
cheers.

b1th0ven

unread,
Aug 20, 2016, 7:19:42 PM8/20/16
to phantomjs
Thanks, David. But this event don't help me. Let's take a look on this code.

var page = require('webpage').create();


page
.open('http://google.com', function(status) {
    console
.log("Status: " + status);
   
if(status === "success") {
       
var a.c = 1;
        console
.log(a);
   
}
    phantom
.exit(0);
});

page
.onError = function(msg, trace) {
  console
.log('page onError...');
  phantom
.exit(1);
};

phantom
.onError = function(msg, trace) {
  console
.log('phantom onError...');
  phantom
.exit(2);
};
Specially for Sean McNamara - I know that code have an error in this line var a.c = 1;

So, when I run script it gets hung and nothing happens... again... 


суббота, 20 августа 2016 г., 19:07:52 UTC+3 пользователь Dawid Bartosz Janiga написал:
Screencast 2016-08-21 02:11:15.mp4

Dawid Bartosz Janiga

unread,
Aug 20, 2016, 9:45:52 PM8/20/16
to phantomjs
Ok, first of all, describe your goal. If you would like to know about a syntax error, the best way for now is call the script in Node environment - as I mentioned in the example.
Reply all
Reply to author
Forward
0 new messages