0
var casper = require('casper').create({
  logLevel:'deubg',
  verbose:true,
});

casper.start(someurl,function(){
  not_existing_function();
})

When executing above code, all I see on the screen is debug informations that means very little to me. I am expecting to see some error saying the function being called doesn't exist, but there is not.

I thought it was just the behavior, until I see this.

The question clearly indicates he has got some error message:

ReferenceError: Can't find variable: $

Why can't I see something like this on my screen?

Community
  • 1
  • 1
shenkwen
  • 3,536
  • 5
  • 45
  • 85

1 Answers1

3

You're likely using PhantomJS 2.x. It has a known bug where some errors are not reported. That includes the class of errors that you're describing.

Also, registering to the various error events of CasperJS/PhantomJS doesn't help in this case, but here they are just in case:

// http://phantomjs.org/api/phantom/handler/on-error.html
phantom.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);
};

// 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));
    };
});

You can run something like eslint or jshint over the script to catch syntax errors and you can run your script in PhantomJS 1.9.8/1.9.7 in order to catch these sort of errors.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222