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

时间:2016-06-29 22:01:53

标签: javascript debugging error-handling phantomjs casperjs

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

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

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

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

问题清楚地表明他有一些错误信息:

  

ReferenceError:找不到变量:$

为什么我在屏幕上看不到这样的内容?

1 个答案:

答案 0 :(得分:3)

您可能正在使用PhantomJS 2.x.它有一个here,其中没有报告某些错误。这包括您正在描述的错误类别。

此外,注册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中运行脚本以捕获这些错误。

相关问题