This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Console errors and warning plugin #1916
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b65159f
Added new console plugin
el-davo 0fd503d
Added specs
el-davo 3e9d669
Added docs
el-davo 1fb5b46
Reverted port change
el-davo 200eae8
Removed .idea from git ignore file
el-davo 8436f83
Updated formatting as suggested in pull request
el-davo f953412
Updated to log to the specResults instead of console.log
el-davo 41590bb
Reverted change to the port
el-davo 2ff7192
Fixed logic to make the tests pass and fail
el-davo e72bd90
Updates from pull request
el-davo 65ba682
Updates from pull request
el-davo af97467
Added exact failed count number
el-davo 1bf7ab9
Added specs
el-davo a4d4d10
Revert to environment.js formatting
el-davo 8ea8466
Update to conform to spec results
el-davo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ changes.sh | |
xmloutput* | ||
npm-debug.log | ||
|
||
*.swp | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
var q = require('q'); | ||
|
||
var testOut = { | ||
failedCount: 0, specResults: [{ | ||
description: "Console output", | ||
assertions: [], | ||
duration: 0 | ||
}] | ||
}; | ||
|
||
/** | ||
* This plugin checks the browser log after each test for warnings and errors. It can be configured to fail a test if either is detected. | ||
* There is also an optional exclude parameter which accepts both regex and strings, | ||
* Any log matching the exclude parameter will not fail the test or be logged to the console | ||
* | ||
* exports.config = { | ||
* plugins: [{ | ||
* path: 'node_modules/protractor/plugins/console', | ||
* failOnWarning: {Boolean} (Default - false), | ||
* failOnError: {Boolean} (Default - true) | ||
* exclude: {Array of strings and regex} (Default - []) | ||
* }] | ||
* }; | ||
*/ | ||
var ConsolePlugin = function() { | ||
}; | ||
|
||
/** | ||
* 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) { | ||
var passed = (testOut.failedCount === 0); | ||
warnings.map(function(warning) { | ||
testOut.specResults[0].assertions.push( | ||
{passed: passed, errorMsg: warning.level.name + ': ' + warning.message} | ||
); | ||
}); | ||
|
||
errors.map(function(error) { | ||
testOut.specResults[0].assertions.push( | ||
{passed: passed, errorMsg: error.level.name + ': ' + error.message} | ||
); | ||
}); | ||
}; | ||
|
||
/** | ||
* Determines if a log message is filtered out or not. This can be set at the config stage using the exclude parameter. | ||
* The parameter accepts both strings and regex | ||
* | ||
* @param logMessage | ||
* Current log message | ||
* @returns {boolean} | ||
*/ | ||
ConsolePlugin.includeLog = function(logMessage) { | ||
return this.exclude.filter(function(e) { | ||
return (e instanceof RegExp) ? logMessage.match(e) : logMessage.indexOf(e) > -1; | ||
}).length === 0; | ||
}; | ||
|
||
/** | ||
* Parses the log and decides whether to throw an error or not | ||
* | ||
* @param config | ||
* The config of the plugin defined in the protractor config file | ||
* @returns {!webdriver.promise.Promise.<R>} | ||
*/ | ||
ConsolePlugin.parseLog = function(config) { | ||
var self = this; | ||
var failOnWarning = (config.failOnWarning === undefined) ? false : config.failOnWarning; | ||
var failOnError = (config.failOnError == undefined) ? true : config.failOnError; | ||
this.exclude = config.exclude || []; | ||
|
||
return this.getBrowserLog().then(function(log) { | ||
|
||
var warnings = log.filter(function(node) { | ||
return (node.level || {}).name === 'WARNING' && self.includeLog(node.message); | ||
}); | ||
|
||
var errors = log.filter(function(node) { | ||
return (node.level || {}).name === 'SEVERE' && self.includeLog(node.message); | ||
}); | ||
|
||
testOut.failedCount += (warnings.length > 0 && failOnWarning) ? warnings.length : 0; | ||
testOut.failedCount += (errors.length > 0 && failOnError) ? errors.length : 0; | ||
|
||
self.logMessages(warnings, errors); | ||
}); | ||
|
||
}; | ||
|
||
/** | ||
* Tear-down function used by protractor | ||
* | ||
* @param config | ||
*/ | ||
ConsolePlugin.prototype.teardown = function(config) { | ||
return ConsolePlugin.parseLog(config).then(function() { | ||
return testOut; | ||
}); | ||
}; | ||
|
||
var consolePlugin = new ConsolePlugin(); | ||
|
||
exports.teardown = consolePlugin.teardown.bind(consolePlugin); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var env = require('../../../spec/environment.js'); | ||
|
||
exports.config = { | ||
seleniumAddress: env.seleniumAddress, | ||
framework: 'jasmine2', | ||
specs: ['fail_spec.js'], | ||
baseUrl: env.baseUrl, | ||
plugins: [{ | ||
path: '../index.js', | ||
failOnWarning: true, | ||
failOnError: true | ||
}] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var env = require('../../../spec/environment.js'); | ||
|
||
exports.config = { | ||
seleniumAddress: env.seleniumAddress, | ||
framework: 'jasmine2', | ||
specs: ['fail_error_spec.js'], | ||
baseUrl: env.baseUrl, | ||
plugins: [{ | ||
path: '../index.js', | ||
failOnWarning: false, | ||
failOnError: true | ||
}] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var env = require('../../../spec/environment.js'); | ||
|
||
exports.config = { | ||
seleniumAddress: env.seleniumAddress, | ||
framework: 'jasmine2', | ||
specs: ['fail_error_spec.js'], | ||
baseUrl: env.baseUrl, | ||
plugins: [{ | ||
path: '../index.js', | ||
failOnWarning: true, | ||
failOnError: true, | ||
exclude: [ | ||
'string', | ||
/regex/ | ||
] | ||
}] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var env = require('../../../spec/environment.js'); | ||
|
||
exports.config = { | ||
seleniumAddress: env.seleniumAddress, | ||
framework: 'jasmine2', | ||
specs: ['fail_warning_spec.js'], | ||
baseUrl: env.baseUrl, | ||
plugins: [{ | ||
path: '../index.js', | ||
failOnWarning: true, | ||
failOnError: false | ||
}] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var env = require('../../../spec/environment.js'); | ||
|
||
exports.config = { | ||
seleniumAddress: env.seleniumAddress, | ||
framework: 'jasmine2', | ||
specs: ['pass_spec.js'], | ||
baseUrl: env.baseUrl, | ||
plugins: [{ | ||
path: '../index.js', | ||
failOnWarning: false, | ||
failOnError: false | ||
}] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
describe('console plugin', function() { | ||
|
||
var logMessageButton = element(by.id('log-message')); | ||
var deleteMessageButton = element(by.id('simulate-error')); | ||
|
||
it('should not fail on log and debug messages', function() { | ||
browser.get('console/index.html'); | ||
logMessageButton.click(); | ||
}); | ||
|
||
it('should fail on error message', function() { | ||
browser.get('console/index.html'); | ||
deleteMessageButton.click(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('should 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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
describe('console plugin', function() { | ||
|
||
var logMessageButton = element(by.id('log-message')); | ||
var warningMessageButton = element(by.id('simulate-warning')); | ||
|
||
it('should 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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('should not fail on log and debug messages', function() { | ||
browser.get('console/index.html'); | ||
logMessageButton.click(); | ||
}); | ||
|
||
it('should not fail on warning message', function() { | ||
browser.get('console/index.html'); | ||
warningMessageButton.click(); | ||
}); | ||
|
||
it('should not fail on error message', function() { | ||
browser.get('console/index.html'); | ||
deleteMessageButton.click(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!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'); | ||
console.error('This should be filtered out by string'); | ||
console.error('This should be filtered out by regex'); | ||
} | ||
}]); | ||
</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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
It's super confusing, but Protractor actually uses both
q
and the webdriver promises. I think it's OK to useq
in this situation, because it has no need to sync up with the webdriver promises - and it's actually probably safer, because it can't accidentally interfere with the webdriver control flow.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.
Will use q for now as per Julies comment