From 989f8e0b52b986f7ddb07831b5b92dca6dceeb07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Cunha?= Date: Tue, 20 Nov 2018 15:16:58 +0000 Subject: [PATCH] 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) --- README.md | 6 ++++ index.js | 34 ++++++++++++------ .../protractor-config/addPrefixToTests.js | 35 +++++++++++++++++++ spec/integrational/screenshoter.int.spec.js | 33 +++++++++++++++-- spec/unit/screenshoter.unit.spec.js | 9 +++-- 5 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 spec/integrational/protractor-config/addPrefixToTests.js diff --git a/README.md b/README.md index 0f6bb87..6182a26 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index 3fba422..5d67c82 100755 --- a/index.js +++ b/index.js @@ -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} @@ -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; @@ -606,7 +619,8 @@ protractorUtil.prototype.setup = function() { }, dump: null, htmlReport: true, - writeReportFreq: 'end' + writeReportFreq: 'end', + addPrefixToTests: false } this.ci = this.obtainCIVariables(process.env); diff --git a/spec/integrational/protractor-config/addPrefixToTests.js b/spec/integrational/protractor-config/addPrefixToTests.js new file mode 100644 index 0000000..14da6b2 --- /dev/null +++ b/spec/integrational/protractor-config/addPrefixToTests.js @@ -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 + }); + } +}; diff --git a/spec/integrational/screenshoter.int.spec.js b/spec/integrational/screenshoter.int.spec.js index 03f5c8c..6dad3d0 100644 --- a/spec/integrational/screenshoter.int.spec.js +++ b/spec/integrational/screenshoter.int.spec.js @@ -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 @@ -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); @@ -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(); + }); + + }); + }); }); diff --git a/spec/unit/screenshoter.unit.spec.js b/spec/unit/screenshoter.unit.spec.js index ddff29f..be3b8fe 100644 --- a/spec/unit/screenshoter.unit.spec.js +++ b/spec/unit/screenshoter.unit.spec.js @@ -89,7 +89,8 @@ describe("Screenshoter unit", function() { }, clearFoldersBeforeTest: true, htmlReport: true, - writeReportFreq: 'end' + writeReportFreq: 'end', + addPrefixToTests: false }); }); @@ -119,7 +120,8 @@ describe("Screenshoter unit", function() { clearFoldersBeforeTest: true, htmlReport: true, writeReportFreq: 'end', - path: './bla/bla' + path: './bla/bla', + addPrefixToTests: false }); }); @@ -150,7 +152,8 @@ describe("Screenshoter unit", function() { }, clearFoldersBeforeTest: true, writeReportFreq: 'end', - htmlReport: true + htmlReport: true, + addPrefixToTests: false }); });