javascript - CasperJS 中的静默错误

标签 javascript phantomjs casperjs

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

例如,如果我执行代码:

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"

最佳答案

有一个开放的PhantomJS issue对此。

你可以通过包装每个 then 和类似的 reportErrors 函数来绕过它,例如:

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

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

关于javascript - CasperJS 中的静默错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35634208/

相关文章:

javascript - 如何验证 Phantom 钱包的签名?

php - 如何将数据从 HTML 表单保存到 WordPress 中的数据库表?

javascript - 如何将 PhantomJS 作为服务器运行并远程调用它?

javascript - CasperJS 像 for 循环一样多次提交和评估

javascript - 优惠券验证

javascript - 使用 PhantomJS 预渲染 AngualrJS 站点时出现 "Failed to instantiate module ngSanitize"

javascript - 为什么我需要一个 Selenium Server 而不是直接调用 WebDriver 实现

javascript - 如何使用 CasperJS/PhantomJS 生成类似移动设备的高 DPI 屏幕截图?

javascript - Casperjs: "TypeError: '未定义'不是一个函数“如果evaluate()在另一个文件中使用

javascript - 如何比较基本 lodash 循环内数组中的特定项目