From 8707d520ebb6a8883f3880a2d1971feeb2dcd5a4 Mon Sep 17 00:00:00 2001 From: Prudhvi Date: Fri, 12 May 2023 00:18:18 +0530 Subject: [PATCH] Added describe name, hook timestamps to reporter (#3680) --- lib/reporter/index.js | 8 +++++++ lib/reporter/results.js | 37 ++++++++++++++++++++++++++++++++- lib/testsuite/index.js | 22 +++++++++++++++----- lib/utils/logger/index.js | 15 +++++++++++++ test/src/runner/testReporter.js | 20 ++++++++++++------ 5 files changed, 90 insertions(+), 12 deletions(-) diff --git a/lib/reporter/index.js b/lib/reporter/index.js index de41920d2a..e2085f3bda 100644 --- a/lib/reporter/index.js +++ b/lib/reporter/index.js @@ -127,10 +127,18 @@ class Reporter extends SimplifiedReporter { this.testResults.setElapsedTime(); } + setTestSectionElapsedTime() { + this.testResults.setTestSectionElapsedTime(); + } + setTestStatus() { this.testResults.setTestStatus(); } + collectTestSectionOutput() { + this.testResults.collectTestSectionOutput(); + } + testSuiteFinished() { this.testResults.setTotalElapsedTime(); } diff --git a/lib/reporter/results.js b/lib/reporter/results.js index 3776b9855d..055a3a28c1 100644 --- a/lib/reporter/results.js +++ b/lib/reporter/results.js @@ -49,6 +49,8 @@ module.exports = class Results { this.buildName = capabilities.buildName || desiredCapabilities.buildName || ''; const {webdriver = {}} = settings; this.host = webdriver.host || ''; + this.name = opts.suiteName || ''; + this.tags = opts.tags || []; this.__retryTest = false; this.initCount(tests); @@ -237,6 +239,8 @@ module.exports = class Results { suiteResults.results.status = this.getTestStatus(); suiteResults.results.seleniumLog = this.seleniumLog; suiteResults.results.host = this.host; + suiteResults.results.name = this.name; + suiteResults.results.tags = this.tags; // Backwards compat suiteResults.results.tests = this.testsCount; @@ -295,7 +299,9 @@ module.exports = class Results { retries: testcase.retriesCount, skipped: 0, tests: 0, - status: Results.TEST_PASS + status: Results.TEST_PASS, + startTimestamp: new Date().getTime(), + httpOutput: [] }; if (this.retryTest && this.testSections[testcase.testName]) { @@ -433,6 +439,26 @@ module.exports = class Results { return this; } + setTestSectionElapsedTime() { + const currentSection = this.getTestSection(this.currentSectionName); + const startTime = currentSection ? currentSection.startTimestamp : this.globalStartTime; + const endTime = new Date().getTime(); + const elapsedTime = endTime - startTime; + this.endTimestamp = endTime; + + this.time += elapsedTime; + + if (currentSection) { + currentSection.time = (elapsedTime/1000).toPrecision(4); + currentSection.timeMs = elapsedTime; + currentSection.startTimestamp = startTime; + currentSection.endTimestamp = endTime; + + } + + return this; + } + setTestStatus() { const currentTest = this.getCurrentTest(); const currentSection = this.getTestSection(this.currentSectionName); @@ -449,6 +475,15 @@ module.exports = class Results { } } + collectTestSectionOutput() { + const currentSection = this.getTestSection(this.currentSectionName); + if (!currentSection) { + return; + } + + currentSection.httpOutput = Logger.collectTestSectionOutput(); + } + setTotalElapsedTime() { this.timeMs = this.time; this.time = (this.time/1000).toPrecision(4); diff --git a/lib/testsuite/index.js b/lib/testsuite/index.js index 09460e44fc..d254546fa5 100644 --- a/lib/testsuite/index.js +++ b/lib/testsuite/index.js @@ -216,7 +216,8 @@ class TestSuite { reportPrefix: '', reportFileName: this.argv['report-filename'], groupName, - isMobile: this.client.api.isMobile() + isMobile: this.client.api.isMobile(), + tags: this.context.getTags() } }); } @@ -460,7 +461,11 @@ class TestSuite { await this.globalsInstance.runPluginHook(hookName, [this.settings]); } - return this.globalHooks[hookName].run(this.client); + const result = await this.globalHooks[hookName].run(this.client); + + this.onTestSectionFinished(); + + return result; }); } @@ -525,8 +530,8 @@ class TestSuite { return this.startTestSuite() .then(() => this.runHook('before')) .then(result => { - this.reporter.setTestStatus(); - + this.onTestSectionFinished(); + if (result instanceof Error) { Logger.error(result); } @@ -564,6 +569,7 @@ class TestSuite { if ((possibleError instanceof Error) && this.failFastMode) { throw possibleError; } + this.onTestSectionFinished(); return result; }); @@ -575,6 +581,12 @@ class TestSuite { .then((errorOrFailures) => this.onTestSuiteFinished(errorOrFailures)); } + onTestSectionFinished() { + this.reporter.setTestStatus(); + this.reporter.setTestSectionElapsedTime(); + this.reporter.collectTestSectionOutput(); + } + onTestSuiteFinished(errorOrFailures = false) { this.__snapShot = undefined; @@ -786,7 +798,7 @@ class TestSuite { testCaseFinished() { this.reporter.setElapsedTime(); - this.reporter.setTestStatus(); + this.onTestSectionFinished(); if (!this.testcase) { return Promise.resolve(); diff --git a/lib/utils/logger/index.js b/lib/utils/logger/index.js index 91d8546f36..4ac84db2c7 100644 --- a/lib/utils/logger/index.js +++ b/lib/utils/logger/index.js @@ -152,6 +152,7 @@ function logRequest(message, params) { const instance = Logger.getInstance(); instance.output.push([timeIso, lodashEscape(message), lodashEscape(inspectObject(params))]); + instance.testSectionOutput.push([timeIso, lodashEscape(message), lodashEscape(inspectObject(params))]); } function logError(severity, errOrMessage, args) { @@ -167,6 +168,7 @@ class Logger { constructor() { this.colors = colors; this.output = []; + this.testSectionOutput = []; } logMessage(...args) { @@ -447,3 +449,16 @@ module.exports.collectOutput = function() { return output; }; + +module.exports.collectTestSectionOutput = function() { + const instance = Logger.getInstance(); + + if (!instance) { + return []; + } + + const {testSectionOutput} = instance; + instance.testSectionOutput = []; + + return testSectionOutput; +}; diff --git a/test/src/runner/testReporter.js b/test/src/runner/testReporter.js index 1a69b23424..b75ba5d45f 100644 --- a/test/src/runner/testReporter.js +++ b/test/src/runner/testReporter.js @@ -252,6 +252,9 @@ describe('testReporter', function() { assert.ok(Object.keys(module).includes('startTimestamp')); assert.ok(Object.keys(module).includes('endTimestamp')); assert.ok(Object.keys(module).includes('host')); + assert.ok(Object.keys(module).includes('name')); + assert.ok(Object.keys(module).includes('tags')); + // check for individual test properties const test = module.completed['demoTest']; assert.ok(Object.keys(test).includes('status')); @@ -292,12 +295,17 @@ describe('testReporter', function() { const completedSections = module['completedSections']; - // check for module properties - assert.ok(Object.keys(completedSections).includes('__after_hook')); - assert.ok(Object.keys(completedSections).includes('__before_hook')); - assert.ok(Object.keys(completedSections).includes('__global_afterEach_hook')); - assert.ok(Object.keys(completedSections).includes('__global_beforeEach_hook')); - assert.ok(Object.keys(completedSections).includes('demoTest')); + // check module properties all for hooks + const hooks = ['__after_hook', '__before_hook', '__global_afterEach_hook', '__global_beforeEach_hook', 'demoTest']; + + hooks.forEach(hook => { + assert.ok(Object.keys(completedSections).includes(hook)); + + const sectionData = completedSections[hook]; + assert.ok(Object.keys(sectionData).includes('startTimestamp')); + assert.ok(Object.keys(sectionData).includes('endTimestamp')); + assert.ok(Object.keys(sectionData).includes('httpOutput')); + }); assert.strictEqual(completedSections['__after_hook']['commands'].length, 1); assert.strictEqual(completedSections['__after_hook']['commands'][0].name, 'end');