-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Console errors and warning plugin #1916
Changes from 4 commits
b65159f
0fd503d
3e9d669
1fb5b46
200eae8
8436f83
f953412
41590bb
2ff7192
e72bd90
65ba682
af97467
1bf7ab9
a4d4d10
8ea8466
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
var q = require('q'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should use protractor.promise instead of q. The webdriver code has its own promise implementation and you probably want to use the same infrastructure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's super confusing, but Protractor actually uses both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will use q for now as per Julies comment |
||
|
||
var testOut = {failedCount: 0, specResults: []}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You haven't quite conformed to the spec here. I think what you want is: var testOut = {failedCount: 0, specResults = [{
description: "Console output",
assertions: [],
duration: 0
}]}; And then, in later lines, when you write |
||
|
||
/** | ||
* This plugin scans the log after each test and can fail on error and warning messages | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clarify that it scans the 'browser log' |
||
* | ||
* exports.config = { | ||
* plugins: [{ | ||
* path: 'node_modules/protractor/plugins/console', | ||
* failOnWarning: {Boolean} (Default - false), | ||
* failOnError: {Boolean} (Default - true) | ||
* }] | ||
* }; | ||
*/ | ||
var ConsolePlugin = function () { | ||
this.failOnWarning = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style nits: We use 2 spaces for indent inside function blocks. |
||
this.failOnError = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style nits: no space between |
||
}; | ||
|
||
/** | ||
* Gets the browser log | ||
* | ||
* @returns {!webdriver.promise.Promise.<!Array.<!webdriver.logging.Entry>>} | ||
*/ | ||
ConsolePlugin.getBrowserLog = function () { | ||
return browser.manage().logs().get('browser'); | ||
}; | ||
|
||
/** | ||
* Logs messages to the test output | ||
* | ||
* @param warnings | ||
* The list of warnings detected by the browser log | ||
* @param errors | ||
* The list of errors detected by the browser log | ||
*/ | ||
ConsolePlugin.logMessages = function (warnings, errors) { | ||
warnings.map(function (warning) { | ||
console.error(warning.level.name + ': ' + warning.message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use "console.warn" here? |
||
}); | ||
|
||
errors.map(function (error) { | ||
console.error(error.level.name + ': ' + error.message); | ||
}); | ||
}; | ||
|
||
/** | ||
* Parses the log and decides whether to throw an error or not | ||
* | ||
* @param config | ||
* The config from the protractor config file | ||
* @returns {Deferred.promise} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: it's |
||
*/ | ||
ConsolePlugin.parseLog = function (config) { | ||
var self = this; | ||
var deferred = q.defer(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should use |
||
var failOnWarning = config.failOnWarning || this.failOnWarning; | ||
var failOnError = config.failOnError || this.failOnError; | ||
this.getBrowserLog().then(function (log) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if you |
||
|
||
var warnings = log.filter(function (node) { | ||
return (node.level || {}).name === 'WARNING'; | ||
}); | ||
|
||
var errors = log.filter(function (node) { | ||
return (node.level || {}).name === 'SEVERE'; | ||
}); | ||
|
||
if (warnings.length > 0 || errors.length > 0) { | ||
self.logMessages(warnings, errors); | ||
} | ||
|
||
testOut.failedCount += (warnings.length > 0 && failOnWarning) ? 1 : 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to add a message to |
||
testOut.failedCount += (errors.length > 0 && failOnError) ? 1 : 0; | ||
|
||
deferred.resolve(); | ||
}); | ||
|
||
return deferred.promise; | ||
}; | ||
|
||
/** | ||
* Tear-down function used by protractor | ||
* | ||
* @param config | ||
*/ | ||
ConsolePlugin.prototype.teardown = function (config) { | ||
var audits = []; | ||
|
||
audits.push(ConsolePlugin.parseLog(config)); | ||
|
||
return q.all(audits).then(function (result) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return testOut; | ||
}); | ||
}; | ||
|
||
var consolePlugin = new ConsolePlugin(); | ||
|
||
exports.teardown = consolePlugin.teardown.bind(consolePlugin); | ||
|
||
exports.ConsolePlugin = ConsolePlugin; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this export used anywhere? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var env = require('../../../spec/environment.js'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to get this test to run on travis, you'll need to make an addition to passingTests.push(
'node lib/cli.js plugins/timeline/spec/conf.js',
'node lib/cli.js plugins/ngHint/spec/successConfig.js',
'node lib/cli.js plugins/accessibility/spec/successConfig.js'); to passingTests.push(
'node lib/cli.js plugins/timeline/spec/conf.js',
'node lib/cli.js plugins/ngHint/spec/successConfig.js',
'node lib/cli.js plugins/accessibility/spec/successConfig.js',
'node lib/cli.js plugins/console/spec/consoleConfig.js'); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks I was looking for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is possible! If you look at the tests for the // Check ngHint plugin
executor.addCommandlineTest(
'node lib/cli.js plugins/ngHint/spec/failureConfig.js')
.expectExitCode(1)
.expectErrors([{
message: 'warning -- ngHint plugin cannot be run as ngHint code was ' +
'never included into the page'
}, {
message: 'warning -- ngHint is included on the page, but is not active ' +
'because there is no `ng-hint` attribute present'
}, {
message: 'warning -- Module "xApp" was created but never loaded.'
}]); This basically means "run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thats awesome, thanks for the help. Will try getting it to work this evening |
||
|
||
exports.config = { | ||
seleniumAddress: env.seleniumAddress, | ||
framework: 'jasmine2', | ||
specs: ['spec.js'], | ||
baseUrl: env.baseUrl, | ||
plugins: [{ | ||
path: '../index.js', | ||
failOnWarning: true, | ||
failOnError: true | ||
}] | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
describe('console plugin', function () { | ||
|
||
var logMessageButton = element(by.id('log-message')); | ||
var warningMessageButton = element(by.id('simulate-warning')); | ||
var deleteMessageButton = element(by.id('simulate-error')); | ||
|
||
it('not fail on log and debug messages', function () { | ||
browser.get('console/index.html'); | ||
logMessageButton.click(); | ||
}); | ||
|
||
it('should fail on warning message', function() { | ||
browser.get('console/index.html'); | ||
warningMessageButton.click(); | ||
}); | ||
|
||
it('should fail on error message', function() { | ||
browser.get('console/index.html'); | ||
deleteMessageButton.click(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!DOCTYPE html> | ||
|
||
<html ng-app="consolePlugin" lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Angular.js Example</title> | ||
<script src="../../lib/angular/angular.min.js"></script> | ||
<script src="../../lib/angular/angular-aria.min.js"></script> | ||
<script type="text/javascript"> | ||
angular.module('consolePlugin', []) | ||
.controller('TestCtrl', ['$scope', function ($scope) { | ||
|
||
$scope.logMessage = function () { | ||
console.log('This is a test log'); | ||
console.debug('This is a test debug'); | ||
}; | ||
|
||
$scope.simulateWarning = function () { | ||
console.warn('This is a test warning'); | ||
}; | ||
|
||
$scope.simulateError = function () { | ||
console.error('This is a test error'); | ||
} | ||
}]); | ||
</script> | ||
</head> | ||
<body ng-controller="TestCtrl"> | ||
<button id="log-message" ng-click="logMessage()">Log Message</button> | ||
<button id="simulate-warning" ng-click="simulateWarning()">Simulate Warning</button> | ||
<button id="simulate-error" ng-click="simulateError()">Simulate Error</button> | ||
</body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be in your personal git ignore file not the project one.