It allows asserting the browser console logs in each test for warnings and errors.
You can set the ignored messages, specify the expectations using strings, regex or custom matchers.
You can check the expectations using verify method which returns a promise:
it('this is my test',function(){
logs.expect('one');
// ...
returnlogs.verify();
});
Using advanced features
var browserLogs =require('protractor-browser-logs');
describe('Home page:',function(){
var log =browserLogs(browser);
beforeEach(function(){
// Use only one instance, but need to reset before each test.
log.reset();
// Combine matcher functions
logs.ignore(logs.or(logs.DEBUG,logs.INFO));
// Specify custom matcher function
logs.ignore(function(message){
returnmessage.message.indexOf('Oops')!==-1;
});
});
it('should log an error after clicking a button',function(){
// The sequence of expectations does matter
logs.expect(/retrying/,logs.WARN);// Expect message having "retrying" text and WARNING level.
logs.expect(/server request failed/,logs.ERROR);
browser.get('...');
element(by.id('button')).click();
});
afterEach(function(){
returnlogs.verify();
});
});
Sharing the code inside a protractor configuration file
onPrepare=function(){
var browserLogs =require('protractor-browser-logs'),
logs =browserLogs(browser);
if(global.logs){
thrownewError('Oops, name is already reserved!');
}
global.logs= logs;
beforeEach(function(){
logs.reset();
// You can put here all expected generic expectations.
logs.ignore('cast_sender.js');
logs.ignore('favicon.ico');
logs.ignore(logs.or(logs.INFO,logs.DEBUG));
});
afterEach(function(){
returnlogs.verify();
});
};
Protractor capabilities configuration
By default browser allows recording only WARNING and SEVERE level messages.
In order to be able asserting any level, You need to change the loggingPrefs.browser capabilities.