Tuesday, December 12Be an Automation Engineer

Identifying Javascript errors in a webpage using Protractor

Javascript errors are very common in any web application. It is possible that the Javascript errors may skip through testers eye. So, it would be better if an automation tool identify them well before they cause any problems in production.

As Protractor was implemented using JavaScript, we can easily capture the Javascript errors from the browser’s console.

Use the below code snippet for capturing Javascript errors using Protractor.

 
describe('Capture Javascript errors', function() {

beforeEach(function() {
browser.get('http://angularjs.org/')
});

afterEach(function() {
browser.manage().logs().get('browser').then(function(browserLog) {
// Uncomment to fail the test if Javascript errors are observed.
//expect(browserLog.length).toEqual(0);
if(browserLog.length > 0)
console.log('log: ' + JSON.parse(JSON.stringify(browserLog))[0].message);
});
});

it('This test should log an error into the console', function() {
browser.executeScript(function() {console.error('error from test')});
});

});

There are three different loggers in protractor. Please check out the loggers and setting their levels here.

The default logging level is ‘WARNING’ for all the loggers in protractor. Which means the messages of type warning and above(‘SEVERE ‘ and ‘WARNING’) will be captured. We can change the logging level using loggindPrefs in config file.

%d bloggers like this: