【问题标题】:why isn't my casperjs displaying any errors?为什么我的 casperjs 没有显示任何错误?
【发布时间】:2016-06-29 22:01:52
【问题描述】:
var casper = require('casper').create({
logLevel:'deubg',
verbose:true,
});
casper.start(someurl,function(){
not_existing_function();
})
执行上述代码时,我在屏幕上看到的只是调试信息,对我来说意义不大。我希望看到一些错误,说被调用的函数不存在,但不存在。
我认为这只是行为,直到我看到this。
这个问题清楚地表明他收到了一些错误信息:
ReferenceError:找不到变量:$
为什么我的屏幕上看不到类似的东西?
【问题讨论】:
标签:
javascript
debugging
error-handling
phantomjs
casperjs
【解决方案1】:
您可能正在使用 PhantomJS 2.x。它有一个known bug,其中没有报告一些错误。这包括您所描述的错误类别。
此外,在这种情况下,注册到 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 中运行你的脚本来捕捉这些错误。