Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Add option to exclude some specs from console log checking #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 - [])
}]
};
```
Expand Down
46 changes: 33 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

/**
Expand All @@ -72,27 +91,28 @@ ConsolePlugin.includeLog = function(logMessage) {
* @return {!webdriver.promise.Promise.<R>} 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 :
context.config.failOnError;
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,
Expand All @@ -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.<Object>} 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();
Expand Down
17 changes: 17 additions & 0 deletions spec/consoleFailFilterSpecConfig.js
Original file line number Diff line number Diff line change
@@ -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/
]
}]
};
17 changes: 17 additions & 0 deletions spec/consolePassFilterSpecConfig.js
Original file line number Diff line number Diff line change
@@ -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/
]
}]
};
10 changes: 9 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();