使用量角器记录控制台错误 [英] Log console errors using protractor
问题描述
我正在尝试像这样在量角器中记录控制台错误
I am trying to log console errors in protractor like this
日志输出采用格式
{
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.
当我输入错误的websocket路径时,如果出现错误,则将其记录下来.但是,当我给出错误的 ng-include
路径,并且如果控制台中出现 404错误
时,它就不会记录日志.
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.
我正在使用firefox进行测试.这个控制台插件浏览器是依赖的还是什么?为什么控制台错误显示不同的行为?
I am using firefox for testing. Is this console plugin browser dependent or what? Why it is showing different behaviour for console errors?
推荐答案
有类似的问题,所以我写了包装功能
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([]);
});
};
这篇关于使用量角器记录控制台错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!