Skip to content

Commit

Permalink
feat(config): optional test description prefix based on capability's …
Browse files Browse the repository at this point in the history
…name

If this flag `addPrefixToTests` is set to true, a prefix will be added to every test description with the `name` value presented on the `capabilities` object defined on the protractor configs.

Resolve add addPrefixToTests config (#72)
  • Loading branch information
joaomqcunha authored and azachar committed Nov 20, 2018
1 parent 49bd38c commit 989f8e0
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 15 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,12 @@ If this flag set to true, screenshot and HTML report directories will be emptied

Default: `false`

## addPrefixToTests

If this flag is set to true, a prefix will be added to every test description with the `name` value presented on the `capabilities` object defined on the protractor configs.

Default: `false`

## failTestOnErrorLog (Chrome only)

Contains a set of configuration for console log. When browser console has errors of a certain log level (default:>900), the spec/test is marked failed along with log in the error report/stacktrace.
Expand Down
34 changes: 24 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ try { // optional dependency, ignore if not installed
* writeReportFreq: {String} (Default - 'end', 'spec', 'asap'),
* screenshotPath: {String} (Default - 'reports/screenshots')
* clearFoldersBeforeTest: {Boolean} (Default - false),
* addPrefixToTests: {Boolean} (Default - false),
* failTestOnErrorLog: {
* failTestOnErrorLogLevel: {Number}, (Default - 900)
* excludeKeywords: {A JSON Array}
Expand Down Expand Up @@ -436,19 +437,31 @@ protractorUtil.registerJasmineReporter = function(context) {
}
},
specStarted: function() {
protractorUtil.test = {
start: moment(),
specScreenshots: [],
specLogs: [],
specHtmls: [],
failedExpectations: [],
passedExpectations: []
};
protractorUtil.testResults.push(protractorUtil.test);
protractorUtil.test = {
start: moment(),
specScreenshots: [],
specLogs: [],
specHtmls: [],
failedExpectations: [],
passedExpectations: [],
prefix: ''
};
global.browser.getProcessedConfig().then(function(config) {
if(config.capabilities) {
protractorUtil.test.prefix = '[' + config.capabilities.name + '] ';
}
protractorUtil.testResults.push(protractorUtil.test);
});
},
specDone: function(result) {
protractorUtil.takeOnSpecDone(result, context, protractorUtil.test); //exec async operation

//Add defined name to the test.description as a prefix
if(context.config.addPrefixToTests) {
result.description = protractorUtil.test.prefix + result.description;
result.fullName = protractorUtil.test.prefix + result.fullName;
}

//calculate total fails, success and so on
if (!protractorUtil.stat[result.status]) {
protractorUtil.stat[result.status] = 0;
Expand Down Expand Up @@ -606,7 +619,8 @@ protractorUtil.prototype.setup = function() {
},
dump: null,
htmlReport: true,
writeReportFreq: 'end'
writeReportFreq: 'end',
addPrefixToTests: false
}

this.ci = this.obtainCIVariables(process.env);
Expand Down
35 changes: 35 additions & 0 deletions spec/integrational/protractor-config/addPrefixToTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var env = require('../environment');

exports.config = {
seleniumAddress: env.seleniumAddress,
framework: 'jasmine2',
plugins: [{
path: '../../../index.js',
screenshotPath: '.tmp/addPrefixToTests',
clearFoldersBeforeTest: false,
addPrefixToTests: true
}],
multiCapabilities: [
{
'browserName': 'chrome',
'name': 'L',
'chromeOptions': {
args: ['--window-size=1400,1200']
},
specs: ['../protractor/angularjs-homepage-test.js'],
}, {
'browserName': 'chrome',
'name': 'M',
'chromeOptions': {
args: ['--window-size=800,1200']
},
specs: ['../protractor/angularjs-homepage-test.js'],
}
],
onPrepare: function() {
// returning the promise makes protractor wait for the reporter config before executing tests
return global.browser.getProcessedConfig().then(function(config) {
//it is ok to be empty
});
}
};
33 changes: 31 additions & 2 deletions spec/integrational/screenshoter.int.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,6 @@ describe("Screenshoter running under protractor", function() {
return done.fail(err);
}
expect(data).toContain("angular.module('reporter').constant('data'");

var report = getReportAsJson(data);
expect(report.tests[0].failedExpectations.length).toBe(1); //Console-error
expect(report.tests[1].failedExpectations.length).toBe(1); //Console-error
Expand All @@ -1389,7 +1388,6 @@ describe("Screenshoter running under protractor", function() {
return done.fail(err);
}
expect(data).toContain("angular.module('reporter').constant('data'");

var report = getReportAsJson(data);
expect(report.tests[0].failedExpectations.length).toBe(0);
expect(report.tests[1].failedExpectations.length).toBe(0);
Expand Down Expand Up @@ -1525,4 +1523,35 @@ describe("Screenshoter running under protractor", function() {
});

});

describe("when the user set the addPrefixToTests parameter", function() {
it("should add the capabilities.name to the test description", function(done) {
runProtractorWithConfig('addPrefixToTests.js');

fs.readFile('.tmp/addPrefixToTests/report.js', 'utf8', function(err, data) {
if (err) {
return done.fail(err);
}

expect(data).toContain("angular.module('reporter').constant('data'");

var report = getReportAsJson(data);
//Since we can't guarantee which one of the test-specs will end up firts, I've to do a matcher validation between "M" and "L" letters.
expect(report.tests[0].description).toMatch('\[[M|L]\] should greet the named user');
expect(report.tests[0].fullName).toMatch('\[[M|L]\] angularjs homepage should greet the named user');
expect(report.tests[1].description).toMatch('\[[M|L]\] should list todos');
expect(report.tests[1].fullName).toMatch('\[[M|L]\] angularjs homepage todo list should list todos');
expect(report.tests[2].description).toMatch('\[[M|L]\] should add a todo');
expect(report.tests[2].fullName).toMatch('\[[M|L]\] angularjs homepage todo list should add a todo');
expect(report.tests[3].description).toMatch('\[[M|L]\] should greet the named user');
expect(report.tests[3].fullName).toMatch('\[[M|L]\] angularjs homepage should greet the named user');
expect(report.tests[4].description).toMatch('\[[M|L]\] should list todos');
expect(report.tests[4].fullName).toMatch('\[[M|L]\] angularjs homepage todo list should list todos');
expect(report.tests[5].description).toMatch('\[[M|L]\] should add a todo');
expect(report.tests[5].fullName).toMatch('\[[M|L]\] angularjs homepage todo list should add a todo');
done();
});

});
});
});
9 changes: 6 additions & 3 deletions spec/unit/screenshoter.unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ describe("Screenshoter unit", function() {
},
clearFoldersBeforeTest: true,
htmlReport: true,
writeReportFreq: 'end'
writeReportFreq: 'end',
addPrefixToTests: false
});
});

Expand Down Expand Up @@ -119,7 +120,8 @@ describe("Screenshoter unit", function() {
clearFoldersBeforeTest: true,
htmlReport: true,
writeReportFreq: 'end',
path: './bla/bla'
path: './bla/bla',
addPrefixToTests: false
});
});

Expand Down Expand Up @@ -150,7 +152,8 @@ describe("Screenshoter unit", function() {
},
clearFoldersBeforeTest: true,
writeReportFreq: 'end',
htmlReport: true
htmlReport: true,
addPrefixToTests: false
});
});

Expand Down

0 comments on commit 989f8e0

Please sign in to comment.