view console.log output in angular protractor jasmine test
Use browser.manage().logs().get('browser')
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);
}
});
});
A general misconception is console.log
will log the things in your browser. That is incorrect. When you run your tests, along with the results of tests you should see the console.log()
values also in the terminal. Browser console is completely different from this.
A general example:
it('get name as John', function(){
element(by.id('name')).getText().then(function(value){
console.log(value);
})
});
Results in Terminal:
John
get name as John - pass
Hope it helps.
This can now be achieved without writing any special code and via plugins:
- first party
protractor-console-plugin
(Chrome only) - third party
protractor-console