Skip to content

Commit 989f8e0

Browse files
joaomqcunhaazachar
authored andcommitted
feat(config): optional test description prefix based on capability's 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)
1 parent 49bd38c commit 989f8e0

File tree

5 files changed

+102
-15
lines changed

5 files changed

+102
-15
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,12 @@ If this flag set to true, screenshot and HTML report directories will be emptied
446446

447447
Default: `false`
448448

449+
## addPrefixToTests
450+
451+
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.
452+
453+
Default: `false`
454+
449455
## failTestOnErrorLog (Chrome only)
450456

451457
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.

index.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ try { // optional dependency, ignore if not installed
3434
* writeReportFreq: {String} (Default - 'end', 'spec', 'asap'),
3535
* screenshotPath: {String} (Default - 'reports/screenshots')
3636
* clearFoldersBeforeTest: {Boolean} (Default - false),
37+
* addPrefixToTests: {Boolean} (Default - false),
3738
* failTestOnErrorLog: {
3839
* failTestOnErrorLogLevel: {Number}, (Default - 900)
3940
* excludeKeywords: {A JSON Array}
@@ -436,19 +437,31 @@ protractorUtil.registerJasmineReporter = function(context) {
436437
}
437438
},
438439
specStarted: function() {
439-
protractorUtil.test = {
440-
start: moment(),
441-
specScreenshots: [],
442-
specLogs: [],
443-
specHtmls: [],
444-
failedExpectations: [],
445-
passedExpectations: []
446-
};
447-
protractorUtil.testResults.push(protractorUtil.test);
440+
protractorUtil.test = {
441+
start: moment(),
442+
specScreenshots: [],
443+
specLogs: [],
444+
specHtmls: [],
445+
failedExpectations: [],
446+
passedExpectations: [],
447+
prefix: ''
448+
};
449+
global.browser.getProcessedConfig().then(function(config) {
450+
if(config.capabilities) {
451+
protractorUtil.test.prefix = '[' + config.capabilities.name + '] ';
452+
}
453+
protractorUtil.testResults.push(protractorUtil.test);
454+
});
448455
},
449456
specDone: function(result) {
450457
protractorUtil.takeOnSpecDone(result, context, protractorUtil.test); //exec async operation
451458

459+
//Add defined name to the test.description as a prefix
460+
if(context.config.addPrefixToTests) {
461+
result.description = protractorUtil.test.prefix + result.description;
462+
result.fullName = protractorUtil.test.prefix + result.fullName;
463+
}
464+
452465
//calculate total fails, success and so on
453466
if (!protractorUtil.stat[result.status]) {
454467
protractorUtil.stat[result.status] = 0;
@@ -606,7 +619,8 @@ protractorUtil.prototype.setup = function() {
606619
},
607620
dump: null,
608621
htmlReport: true,
609-
writeReportFreq: 'end'
622+
writeReportFreq: 'end',
623+
addPrefixToTests: false
610624
}
611625

612626
this.ci = this.obtainCIVariables(process.env);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var env = require('../environment');
2+
3+
exports.config = {
4+
seleniumAddress: env.seleniumAddress,
5+
framework: 'jasmine2',
6+
plugins: [{
7+
path: '../../../index.js',
8+
screenshotPath: '.tmp/addPrefixToTests',
9+
clearFoldersBeforeTest: false,
10+
addPrefixToTests: true
11+
}],
12+
multiCapabilities: [
13+
{
14+
'browserName': 'chrome',
15+
'name': 'L',
16+
'chromeOptions': {
17+
args: ['--window-size=1400,1200']
18+
},
19+
specs: ['../protractor/angularjs-homepage-test.js'],
20+
}, {
21+
'browserName': 'chrome',
22+
'name': 'M',
23+
'chromeOptions': {
24+
args: ['--window-size=800,1200']
25+
},
26+
specs: ['../protractor/angularjs-homepage-test.js'],
27+
}
28+
],
29+
onPrepare: function() {
30+
// returning the promise makes protractor wait for the reporter config before executing tests
31+
return global.browser.getProcessedConfig().then(function(config) {
32+
//it is ok to be empty
33+
});
34+
}
35+
};

spec/integrational/screenshoter.int.spec.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,6 @@ describe("Screenshoter running under protractor", function() {
13711371
return done.fail(err);
13721372
}
13731373
expect(data).toContain("angular.module('reporter').constant('data'");
1374-
13751374
var report = getReportAsJson(data);
13761375
expect(report.tests[0].failedExpectations.length).toBe(1); //Console-error
13771376
expect(report.tests[1].failedExpectations.length).toBe(1); //Console-error
@@ -1389,7 +1388,6 @@ describe("Screenshoter running under protractor", function() {
13891388
return done.fail(err);
13901389
}
13911390
expect(data).toContain("angular.module('reporter').constant('data'");
1392-
13931391
var report = getReportAsJson(data);
13941392
expect(report.tests[0].failedExpectations.length).toBe(0);
13951393
expect(report.tests[1].failedExpectations.length).toBe(0);
@@ -1525,4 +1523,35 @@ describe("Screenshoter running under protractor", function() {
15251523
});
15261524

15271525
});
1526+
1527+
describe("when the user set the addPrefixToTests parameter", function() {
1528+
it("should add the capabilities.name to the test description", function(done) {
1529+
runProtractorWithConfig('addPrefixToTests.js');
1530+
1531+
fs.readFile('.tmp/addPrefixToTests/report.js', 'utf8', function(err, data) {
1532+
if (err) {
1533+
return done.fail(err);
1534+
}
1535+
1536+
expect(data).toContain("angular.module('reporter').constant('data'");
1537+
1538+
var report = getReportAsJson(data);
1539+
//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.
1540+
expect(report.tests[0].description).toMatch('\[[M|L]\] should greet the named user');
1541+
expect(report.tests[0].fullName).toMatch('\[[M|L]\] angularjs homepage should greet the named user');
1542+
expect(report.tests[1].description).toMatch('\[[M|L]\] should list todos');
1543+
expect(report.tests[1].fullName).toMatch('\[[M|L]\] angularjs homepage todo list should list todos');
1544+
expect(report.tests[2].description).toMatch('\[[M|L]\] should add a todo');
1545+
expect(report.tests[2].fullName).toMatch('\[[M|L]\] angularjs homepage todo list should add a todo');
1546+
expect(report.tests[3].description).toMatch('\[[M|L]\] should greet the named user');
1547+
expect(report.tests[3].fullName).toMatch('\[[M|L]\] angularjs homepage should greet the named user');
1548+
expect(report.tests[4].description).toMatch('\[[M|L]\] should list todos');
1549+
expect(report.tests[4].fullName).toMatch('\[[M|L]\] angularjs homepage todo list should list todos');
1550+
expect(report.tests[5].description).toMatch('\[[M|L]\] should add a todo');
1551+
expect(report.tests[5].fullName).toMatch('\[[M|L]\] angularjs homepage todo list should add a todo');
1552+
done();
1553+
});
1554+
1555+
});
1556+
});
15281557
});

spec/unit/screenshoter.unit.spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ describe("Screenshoter unit", function() {
8989
},
9090
clearFoldersBeforeTest: true,
9191
htmlReport: true,
92-
writeReportFreq: 'end'
92+
writeReportFreq: 'end',
93+
addPrefixToTests: false
9394
});
9495
});
9596

@@ -119,7 +120,8 @@ describe("Screenshoter unit", function() {
119120
clearFoldersBeforeTest: true,
120121
htmlReport: true,
121122
writeReportFreq: 'end',
122-
path: './bla/bla'
123+
path: './bla/bla',
124+
addPrefixToTests: false
123125
});
124126
});
125127

@@ -150,7 +152,8 @@ describe("Screenshoter unit", function() {
150152
},
151153
clearFoldersBeforeTest: true,
152154
writeReportFreq: 'end',
153-
htmlReport: true
155+
htmlReport: true,
156+
addPrefixToTests: false
154157
});
155158
});
156159

0 commit comments

Comments
 (0)