Log console errors using protractor

*爱你&永不变心* 提交于 2021-02-18 13:57:37

问题


I am trying to log console errors in protractor like this

log output is in format

{
  level: {
    value: 900,
    name: 'WARNING'
  },
  message: 'message',
  timestamp: 1442090739962,
  type: ''
}, {
  level: {
    value: 800,
    name: 'INFO'
  },
  message: 'message',
  timestamp: 1442090740723,
  type: ''
}, {
  level: {
    value: 1000,
    name: 'ERROR'
  },
  message: 'error message',
  timestamp: 1442090740723,
  type: ''
},

I want to catch only errors so i have written test like this

it('it should be detect console errors', function() {
  browser.manage().logs().get('browser').then(function(browserLogs) {
    // browserLogs is an array of objects with level and message fields
    browserLogs.forEach(function(log) {
      if (log.level.value > 900) { // it's an error log
        console.log('Browser console error!');
        console.log(log.message);
      }
    });
  });
});

Problem is it's catching errors sometimes and sometimes not.

when i give wrong path to websocket and if there is an error then it is logging it. But When I give wrong path to ng-include and if there is a 404 error in console then it's not logging.

I am using firefox for testing. Is this console plugin browser dependent or what? Why it is showing different behaviour for console errors?


回答1:


Had similar problems, so I wrote wrapping functions

injectConsoleTracing = function () {
    browser.executeScript('window.errs=typeof(errs)=="undefined" ? [] : window.errs; window.console.error = function(msg){window.errs.push(msg); }; ');
    browser.executeScript('window.logs=typeof(logs)=="undefined" ? [] : window.logs; window.console.log = function(msg){window.logs.push(msg); }; ');
};

expectNoConsoleErrors = function () {
    browser.executeScript('return window.errs;').then(function (v) {
        expect(v).toEqual([]);
    });
    browser.executeScript('return window.logs;').then(function (v) {
        expect(v).toEqual([]);
    });
};



回答2:


800 is not greater than 900 then how do you expect to catch the error, it should be

if (log.level.value <= 900)



回答3:


In my Test Suite I use the following to check for Console Errors and display them if any are present:

browser.manage().logs().get('browser').then(function(browserLog) {
    if (browserLog.length) {
        console.log('Browser console error!');
        console.error('log: ' + JSON.stringify(browserLog));
    }
});


来源:https://stackoverflow.com/questions/32546905/log-console-errors-using-protractor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!