Javascript CasperJS中的无声错误

Javascript CasperJS中的无声错误,javascript,phantomjs,casperjs,Javascript,Phantomjs,Casperjs,我刚刚开始使用CasperJs,调试它时遇到了困难,因为太多的编码错误似乎会导致脚本在没有提供错误消息的情况下退出。当您使用verbose模式时,您会得到您应该得到的消息,直到出现问题的代码行,这时它就退出了 例如,如果我执行代码: var casper = require('casper').create({ verbose: true, logLevel: "debug" }); casper.start('https://www.google.com/#q=stackov

我刚刚开始使用CasperJs,调试它时遇到了困难,因为太多的编码错误似乎会导致脚本在没有提供错误消息的情况下退出。当您使用verbose模式时,您会得到您应该得到的消息,直到出现问题的代码行,这时它就退出了

例如,如果我执行代码:

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

casper.start('https://www.google.com/#q=stackoverflow', function(){

});
casper.wait(2000, function(){
});


casper.then(function() {
    hrefAr = this.evaluate(getLinks);
    this.log(hrefAr.length + ' links found', 'info');
    this.exit();
});

casper.run(function() {
    this.exit();
});

function getLinks() {
    var links = document.querySelectorAll('a');
    return Array.prototype.map.call(links, function(e) {
        return e.getAttribute('href');
    });
}
我得到以下结果:

...a bunch of info & debug messages, followed by...
[info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&cad=h (HTTP 200)
[info] [phantom] 89 links found
[debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"
如果我向函数getLinks添加日志语句

...code as shown above...
function getLinks() {
    this.log('getLinks ran', 'info');
    var links = document.querySelectorAll('a');
...code as shown above...
…这会导致脚本失败,如下所示:

...the same info & debug messages...
[info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&cad=h (HTTP 200)
...NO LOGS, ECHOS, OR RESULTS PAST THIS POINT, JUST THESE TWO CLOSING STATEMENTS...
[debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"
它并没有告诉您出了什么问题,它只是将您送回空白状态并结束执行

有没有更好的错误报告方法?或任何错误报告

当我尝试使用以下代码实现以下解决方案时:

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

casper.start('https://www.google.com/#q=stackoverflow', function(){

});
casper.wait(2000, function(){
});


casper.then(function() {
    reportErrors(function() {
        hrefAr = this.evaluate(getLinks);
        this.log(hrefAr.length + ' links found', 'info');
        this.exit();
    });
});

casper.run(function() {
    this.exit();
});

function getLinks() {

        //this.log('getLinks ran', 'info');
        var links = document.querySelectorAll('a');
        return Array.prototype.map.call(links, function(e) {
            return e.getAttribute('href');
        });

}

function reportErrors(f) {
  var ret = null;
  try {
    ret = f();
  } catch (e) {
    casper.echo("ERROR: " + e);
    casper.exit();
  }
  return ret;
}
我得到

...info & debug messages shown above...
[info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&cad=h (HTTP 200)
ERROR: TypeError: undefined is not a constructor (evaluating 'this.evaluate(getLinks)') 
--THIS IS WHERE MY LINK COUNT WOULD BE REPORTED
[debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"
这是一个公开的问题

您可以通过使用reportErrors函数包装每个then&来解决此问题,例如:

function reportErrors(f) {
  var ret = null;
  try {
    ret = f();
  } catch (e) {
    casper.echo("ERROR: " + e);
    casper.exit();
  }
  return ret;
}

casper.then(function() {
  reportErrors(function() {
    ...
  });
});
这是一个公开的问题

您可以通过使用reportErrors函数包装每个then&来解决此问题,例如:

function reportErrors(f) {
  var ret = null;
  try {
    ret = f();
  } catch (e) {
    casper.echo("ERROR: " + e);
    casper.exit();
  }
  return ret;
}

casper.then(function() {
  reportErrors(function() {
    ...
  });
});

在修复PhantomJS 2.x中的吞咽错误之前,您可以尝试以下几种方法:

使用PhantomJS1.9.8。到目前为止,底层的QtWebKit引擎已经使用了5年多,但在大多数情况下仍然运行良好。如果存在ssl/TLS问题,可以添加命令行选项,例如-ignore ssl errors=true。CasperJS支持通过将PhantomJS_EXECUTABLE环境变量设置为要使用的可执行文件或可执行文件路径,根据需要更改当前终端会话的PhantomJS版本。例如,在Windows上:set PHANTOMJS_EXECUTABLE=phantomjs198我将它们编号并放在路径中

如果您担心的是语法错误,那么请首先对代码进行筛选。我可以推荐eslint和jshint

使用其他事件检测PhantomJS 1.9.8中的错误:resource.error、page.error、remote.message和casper.page.onResourceTimeout

关于你的特殊功能。这在reportErrors的回调中没有意义。您需要绑定另一个对象:

casper.reportErrors = function (f) {
  var ret = null;
  try {
    ret = f.call(this);
  } catch (e) {
    this.echo("ERROR: " + e);
    this.exit();
  }
  return ret;
}
然后您可以像以前一样使用它:

casper.then(function() {
    this.reportErrors(function() {
        hrefAr = this.evaluate(getLinks);
        this.log(hrefAr.length + ' links found', 'info');
        this.exit();
    });
});

在修复PhantomJS 2.x中的吞咽错误之前,您可以尝试以下几种方法:

使用PhantomJS1.9.8。到目前为止,底层的QtWebKit引擎已经使用了5年多,但在大多数情况下仍然运行良好。如果存在ssl/TLS问题,可以添加命令行选项,例如-ignore ssl errors=true。CasperJS支持通过将PhantomJS_EXECUTABLE环境变量设置为要使用的可执行文件或可执行文件路径,根据需要更改当前终端会话的PhantomJS版本。例如,在Windows上:set PHANTOMJS_EXECUTABLE=phantomjs198我将它们编号并放在路径中

如果您担心的是语法错误,那么请首先对代码进行筛选。我可以推荐eslint和jshint

使用其他事件检测PhantomJS 1.9.8中的错误:resource.error、page.error、remote.message和casper.page.onResourceTimeout

关于你的特殊功能。这在reportErrors的回调中没有意义。您需要绑定另一个对象:

casper.reportErrors = function (f) {
  var ret = null;
  try {
    ret = f.call(this);
  } catch (e) {
    this.echo("ERROR: " + e);
    this.exit();
  }
  return ret;
}
然后您可以像以前一样使用它:

casper.then(function() {
    this.reportErrors(function() {
        hrefAr = this.evaluate(getLinks);
        this.log(hrefAr.length + ' links found', 'info');
        this.exit();
    });
});

您可以使用casper.evaluate等代替this.evaluate、this.log、this.exit。我还发现@NiKo将console.log事件打印到屏幕上的方法非常有用。对于像我这样的noobs,只需将casper.on语句粘贴到页面底部,它就可以从那里开始工作。如果您在电脑上,因此没有casper.log事件的彩色输出,我觉得添加casper.on语句,然后对代码中的所有日志消息使用console.log会更容易,完全绕过casper.log。您可以使用casper.evaluate代替this.evaluate、this.log、this.exit,我还发现@NiKo将console.log事件打印到屏幕上的方法非常有用。对于像我这样的noobs,只需将casper.on语句粘贴到页面底部,它就可以从那里开始工作。如果您在PC上,因此没有casper.log事件的彩色输出,我觉得添加casper.on语句,然后对代码中的所有日志消息使用console.log更容易,完全绕过casper.log。当您引入错误时,此版本会返回错误,当其他所有操作都正确时,此版本会无错误运行。但是,错误始终为error:TypeError:null不是计算“hrefAr.length”的对象。当我使用var test=notdefined启动getLinks时就是这样;当我开始使用这个.log'getLinks run','info'获取链接时。但至少它告诉我有一个错误!好的,现在我看到了错误:ReferenceError:在使用var test=notdefined语句运行脚本时,在hrefAr错误之前找不到变量:notdefined。当引入错误时,此版本会返回一个错误,当所有其他内容都被删除时,此版本会无误运行
对的但是,错误始终为error:TypeError:null不是计算“hrefAr.length”的对象。当我使用var test=notdefined启动getLinks时就是这样;当我开始使用这个.log'getLinks run','info'获取链接时。但至少它告诉我有一个错误!好的,现在我看到了错误:ReferenceError:在使用var test=notdefined语句运行脚本时,在hrefAr错误之前找不到变量:notdefined。谢谢。这对我很有帮助,发现一行对我来说甚至都不可疑的代码导致了疯狂的问题。CasperError:casper.test属性只能使用'casperjs test'命令使用。实际上,我必须使用在此处编辑的函数版本:谢谢。这对我很有帮助,发现一行对我来说甚至都不可疑的代码导致了疯狂的问题。CasperError:casper.test属性只能使用“casperjs test”命令使用。实际上,我必须使用在此处编辑的函数版本: