代码之家  ›  专栏  ›  技术社区  ›  mrks

返回数组中的特定对象值

  •  0
  • mrks  · 技术社区  · 9 年前

    我想在测试期间检查特定的控制台日志

    it('should return logs', function(done) {
      browser
          .log('browser').then(function(logs){
            console.log(logs);
          })
      ...
    });
    

    我得到的是:

    [ { level: 'INFO',
        message: 'https://localhost:3000/ 42:14 "foo"',
        timestamp: 1485785320222 },
      { level: 'INFO',
        message: 'https://localhost:3000/ 43:14 "bar"',
        timestamp: 1485785320222 },
      { level: 'INFO',
        message: 'https://localhost:3000/ 46:14 Array[6]',
        timestamp: 1485785320225 },
      { level: 'SEVERE',
        message: 'https://localhost:3000/favicon.ico - Failed to load resource: the server responded with a status of 404 (Not Found)',
        timestamp: 1485785320298 } ]
    

    所以它应该只返回一个日志条目列表,比如“foo”和“bar”,这样我就可以检查它是否等于我的假设值。

    我现在不确定是否需要一个“for..in”来构建我可以重用的方法,或者只是循环多个对象并查找logs.value。消息并用“”修剪零件。

    1 回复  |  直到 9 年前
        1
  •  1
  •   Matt Mokary    9 年前

    可能最简单/最容易理解的方法是只过滤与您要查找的日志相匹配的日志:

    it('should return logs', function(done) {
      browser
          .log('browser').then(function(logs){
            console.log(logs.filter(function (logItem) {
              return logItem.message.match(/"foo"|"bar"$/);
            });
          })
      ...
    });