为什么我的 casperjs 没有显示任何错误?

why isn't my casperjs displaying any errors?

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

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

当执行上面的代码时,我在屏幕上看到的只是调试信息,对我来说意义不大。我期待看到一些错误,指出被调用的函数不存在,但实际上不存在。

我以为这只是行为,直到我看到 this

这个问题清楚地表明他收到了一些错误信息:

ReferenceError: Can't find variable: $

为什么我的屏幕上看不到这样的东西?

您可能正在使用 PhantomJS 2.x。它有一个 known bug 没有报告一些错误。这包括您描述的 class 个错误。

此外,注册到 CasperJS/PhantomJS 的各种错误事件在这种情况下没有帮助,但这里它们以防万一:

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

您可以 运行 在脚本上使用类似 eslint 或 jshint 的东西来捕获语法错误,并且您可以 运行 PhantomJS 1.9.8/1.9.7 中的脚本以捕获这些类型的错误错误。