diff --git a/README.md b/README.md index 6bdba43..d1932d3 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,11 @@ console. A false setting to logWarnings also overrides the failOnWarning setting exports.config = { plugins: [{ package: 'protractor-console-plugin', - failOnWarning: {Boolean} (Default - false), - failOnError: {Boolean} (Default - true), - logWarnings: {Boolean} (Default - true), - exclude: {Array of strings and regex} (Default - []) + failOnWarning: {Boolean} (Default - false), + failOnError: {Boolean} (Default - true), + logWarnings: {Boolean} (Default - true), + exclude: {Array of strings and regex} (Default - []) + excludeSpecs: {Array of strings and regex} (Default - []) }] }; ``` diff --git a/index.js b/index.js index 4e60032..3a6406f 100644 --- a/index.js +++ b/index.js @@ -52,17 +52,36 @@ ConsolePlugin.logMessages = function(warnings, errors, /** * 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. + * config stage using the exclude && excludeSpecs parameters. + * The parameters accept both strings and regex. * * @param {string} logMessage Current log message. * @return {boolean} true iff the log should be included in the output */ -ConsolePlugin.includeLog = function(logMessage) { - return ConsolePlugin.exclude.filter(function(e) { - return (e instanceof RegExp) ? logMessage.match(e) : - logMessage.indexOf(e) > -1; - }).length === 0; +ConsolePlugin.includeLog = function(logMessage, testInfo) { + var include = true; + for (var i = 0; i < ConsolePlugin.exclude.length; i++) { + if (ConsolePlugin.exclude[i] instanceof RegExp) { + if (logMessage.match(ConsolePlugin.exclude[i])) include = false; + } + else { + if (logMessage.indexOf(ConsolePlugin.exclude[i]) > -1 ) include = false; + } + } + + for (var i = 0; i < ConsolePlugin.excludeSpecs.length; i++) { + if (ConsolePlugin.excludeSpecs[i] instanceof RegExp) { + if ((testInfo.category + ' ' + testInfo.name).match(ConsolePlugin.excludeSpecs[i])) include = false; + } + else { + if ((testInfo.category + ' ' + testInfo.name).indexOf(ConsolePlugin.excludeSpecs[i]) > -1) include = false; + } + } + + if (include) + console.error("CONSOLE PLUGIN FAIL: \033[31m" + logMessage + "\033[0m") + + return include }; /** @@ -72,7 +91,7 @@ ConsolePlugin.includeLog = function(logMessage) { * @return {!webdriver.promise.Promise.} A promise which resolves when the * logs have been gathered */ -ConsolePlugin.parseLog = function(context) { +ConsolePlugin.parseLog = function(context, testInfo) { var failOnWarning = (context.config.failOnWarning === undefined) ? false : context.config.failOnWarning; var failOnError = (context.config.failOnError === undefined) ? true : @@ -80,19 +99,20 @@ ConsolePlugin.parseLog = function(context) { var logWarnings = (context.config.logWarnings === undefined) ? true : context.config.logWarnings; ConsolePlugin.exclude = context.config.exclude || []; + ConsolePlugin.excludeSpecs = context.config.excludeSpecs || []; return ConsolePlugin.getBrowserLog().then(function(log) { var warnings = []; if (logWarnings) { warnings = log.filter(function(node) { return (node.level || {}).name === 'WARNING' && - ConsolePlugin.includeLog(node.message); + ConsolePlugin.includeLog(node.message, testInfo); }); } var errors = log.filter(function(node) { return (node.level || {}).name === 'SEVERE' && - ConsolePlugin.includeLog(node.message); + ConsolePlugin.includeLog(node.message, testInfo); }); ConsolePlugin.logMessages(warnings, errors, failOnWarning, failOnError, @@ -103,13 +123,13 @@ ConsolePlugin.parseLog = function(context) { /** * Gather the console logs and output them as test results. See the - * documentation of the teardown function in the protractor plugin API. + * documentation of the postTest function in the protractor plugin API. * * @return {!webdriver.promise.Promise.} A promise which resolves to the * test results generated by the console logs */ -ConsolePlugin.prototype.teardown = function() { - return ConsolePlugin.parseLog(this); +ConsolePlugin.prototype.postTest = function(passed, testInfo) { + return ConsolePlugin.parseLog(this, testInfo); }; var consolePlugin = new ConsolePlugin(); diff --git a/spec/consoleFailFilterSpecConfig.js b/spec/consoleFailFilterSpecConfig.js new file mode 100644 index 0000000..7d4fd24 --- /dev/null +++ b/spec/consoleFailFilterSpecConfig.js @@ -0,0 +1,17 @@ +var env = require('./environment.js'); + +exports.config = { + seleniumAddress: env.seleniumAddress, + framework: 'jasmine', + specs: ['fail_error_spec.js'], + baseUrl: env.baseUrl, + plugins: [{ + path: '../index.js', + failOnWarning: true, + failOnError: true, + excludeSpec: [ + 'should fail on error', + /messages/ + ] + }] +}; diff --git a/spec/consolePassFilterSpecConfig.js b/spec/consolePassFilterSpecConfig.js new file mode 100644 index 0000000..4c1e5aa --- /dev/null +++ b/spec/consolePassFilterSpecConfig.js @@ -0,0 +1,17 @@ +var env = require('./environment.js'); + +exports.config = { + seleniumAddress: env.seleniumAddress, + framework: 'jasmine', + specs: ['fail_error_spec.js'], + baseUrl: env.baseUrl, + plugins: [{ + path: '../index.js', + failOnWarning: true, + failOnError: true, + excludeSpec: [ + 'console plugin should fail on error message', + /messages/ + ] + }] +}; diff --git a/test.js b/test.js index cb84624..6fd79d5 100755 --- a/test.js +++ b/test.js @@ -4,7 +4,8 @@ var Executor = require('./test_util').Executor; var ptor = 'node node_modules/protractor/lib/cli.js '; var passingTests = [ - ptor + 'spec/consolePassConfig.js' + ptor + 'spec/consolePassConfig.js', + ptor + 'spec/consolePassFilterSpecConfig.js', ]; var executor = new Executor(); @@ -51,4 +52,11 @@ executor.addCommandlineTest( {message: 'This is a test error'} ]); + executor.addCommandlineTest( + ptor + 'spec/consoleFailFilterSpecConfig.js') + .expectExitCode(1) + .expectErrors([ + {message: 'This is a test error'} + ]); + executor.execute();