簡體   English   中英

返回數組中的特定對象值

[英]Return specific object value in array

我想在測試期間檢查特定的控制台日志

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.message並用“”修飾部分。

僅過濾與您要查找的內容匹配的日志可能是最簡單/最容易理解的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM