From e1af0b97dfd252d0cbed131e28a0cbaebf5444aa Mon Sep 17 00:00:00 2001 From: Anudeep Date: Sun, 25 Aug 2024 19:26:36 +0530 Subject: [PATCH] chore: capture metadata from cucumber --- package-lock.json | 4 +- package.json | 2 +- src/parsers/cucumber.js | 13 +- tests/data/cucumber/suites-with-metadata.json | 186 +++++++++++++++++ tests/parser.cucumber.spec.js | 196 ++++++++++++++++++ 5 files changed, 394 insertions(+), 7 deletions(-) create mode 100644 tests/data/cucumber/suites-with-metadata.json diff --git a/package-lock.json b/package-lock.json index 6973c96..d5aa061 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "test-results-parser", - "version": "0.2.3", + "version": "0.2.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "test-results-parser", - "version": "0.2.3", + "version": "0.2.4", "license": "MIT", "dependencies": { "fast-xml-parser": "^4.4.1", diff --git a/package.json b/package.json index f8781d6..9f90c66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "test-results-parser", - "version": "0.2.3", + "version": "0.2.4", "description": "Parse test results from JUnit, TestNG, xUnit, cucumber and many more", "main": "src/index.js", "types": "./src/index.d.ts", diff --git a/src/parsers/cucumber.js b/src/parsers/cucumber.js index da5ca74..e9bdc36 100644 --- a/src/parsers/cucumber.js +++ b/src/parsers/cucumber.js @@ -51,7 +51,7 @@ class CucumberParser extends BaseParser { test_suite.duration = test_suite.cases.reduce((total, _) => total + _.duration, 0); test_suite.duration = parseFloat(test_suite.duration.toFixed(2)); test_suite.status = test_suite.total === test_suite.passed ? 'PASS' : 'FAIL'; - const { tags, metadata } = this.#getTagsAndMetadata(feature.tags); + const { tags, metadata } = this.#getTagsAndMetadata(feature); test_suite.tags = tags; test_suite.metadata = metadata; for (const test_case of test_suite.cases) { @@ -88,7 +88,7 @@ class CucumberParser extends BaseParser { test_case.failure = failed_step.failure; test_case.stack_trace = failed_step.stack_trace } - const { tags, metadata } = this.#getTagsAndMetadata(scenario.tags); + const { tags, metadata } = this.#getTagsAndMetadata(scenario); test_case.tags = tags; test_case.metadata = metadata; return test_case; @@ -134,9 +134,10 @@ class CucumberParser extends BaseParser { /** * - * @param {import('./cucumber.result').CucumberTag[]} cucumber_tags + * @param {import('./cucumber.result').CucumberFeature | import('./cucumber.result').CucumberElement} feature */ - #getTagsAndMetadata(cucumber_tags) { + #getTagsAndMetadata(feature) { + const cucumber_tags = feature.tags || []; const metadata = {}; const tags = []; if (cucumber_tags) { @@ -149,6 +150,10 @@ class CucumberParser extends BaseParser { } } } + if (feature.metadata) { + Object.assign(metadata, feature.metadata); + } + return { tags, metadata }; } diff --git a/tests/data/cucumber/suites-with-metadata.json b/tests/data/cucumber/suites-with-metadata.json new file mode 100644 index 0000000..e8598b4 --- /dev/null +++ b/tests/data/cucumber/suites-with-metadata.json @@ -0,0 +1,186 @@ +[ + { + "description": "Verify calculator functionalities", + "elements": [ + { + "description": "", + "id": "addition;addition-of-two-numbers", + "keyword": "Scenario", + "line": 5, + "name": "Addition of two numbers", + "steps": [ + { + "arguments": [], + "keyword": "Given ", + "line": 6, + "name": "I have number 6 in calculator", + "match": { + "location": "features\\support\\steps.js:5" + }, + "result": { + "status": "passed", + "duration": 1211400 + } + }, + { + "arguments": [], + "keyword": "When ", + "line": 7, + "name": "I entered number 7", + "match": { + "location": "features\\support\\steps.js:9" + }, + "result": { + "status": "passed", + "duration": 136500 + } + }, + { + "arguments": [], + "keyword": "Then ", + "line": 8, + "name": "I should see result 13", + "match": { + "location": "features\\support\\steps.js:13" + }, + "result": { + "status": "passed", + "duration": 244700 + } + } + ], + "tags": [ + { + "name": "@green", + "line": 4 + }, + { + "name": "@fast", + "line": 4 + }, + { + "name": "@testCase=1234", + "line": 4 + } + ], + "type": "scenario" + } + ], + "id": "addition", + "line": 1, + "keyword": "Feature", + "name": "Addition", + "tags": [ + { + "name": "@blue", + "line": 4 + }, + { + "name": "@slow", + "line": 4 + }, + { + "name": "@suite=1234", + "line": 4 + } + ], + "uri": "features\\sample.feature", + "metadata": { + "browser": { "name": "firefox", "version": "129.0" }, + "device": "Desktop", + "platform": { "name": "Windows", "version": "11" } + } + }, + { + "description": "Verify calculator functionalities", + "elements": [ + { + "description": "", + "id": "addition;addition-of-two-numbers", + "keyword": "Scenario", + "line": 5, + "name": "Addition of two numbers", + "steps": [ + { + "arguments": [], + "keyword": "Given ", + "line": 6, + "name": "I have number 6 in calculator", + "match": { + "location": "features\\support\\steps.js:5" + }, + "result": { + "status": "passed", + "duration": 1211400 + } + }, + { + "arguments": [], + "keyword": "When ", + "line": 7, + "name": "I entered number 7", + "match": { + "location": "features\\support\\steps.js:9" + }, + "result": { + "status": "passed", + "duration": 136500 + } + }, + { + "arguments": [], + "keyword": "Then ", + "line": 8, + "name": "I should see result 13", + "match": { + "location": "features\\support\\steps.js:13" + }, + "result": { + "status": "passed", + "duration": 244700 + } + } + ], + "tags": [ + { + "name": "@green", + "line": 4 + }, + { + "name": "@fast", + "line": 4 + }, + { + "name": "@testCase=1234", + "line": 4 + } + ], + "type": "scenario" + } + ], + "id": "addition", + "line": 1, + "keyword": "Feature", + "name": "Addition", + "tags": [ + { + "name": "@blue", + "line": 4 + }, + { + "name": "@slow", + "line": 4 + }, + { + "name": "@suite=1234", + "line": 4 + } + ], + "uri": "features\\sample.feature", + "metadata": { + "browser": { "name": "chrome", "version": "129.0" }, + "device": "Desktop", + "platform": { "name": "Windows", "version": "11" } + } + } +] \ No newline at end of file diff --git a/tests/parser.cucumber.spec.js b/tests/parser.cucumber.spec.js index 3ddca81..1c2d010 100644 --- a/tests/parser.cucumber.spec.js +++ b/tests/parser.cucumber.spec.js @@ -465,4 +465,200 @@ describe('Parser - Cucumber Json', () => { }); }); + it('test with metadata', () => { + const result = parse({ type: 'cucumber', files: [`${testDataPath}/suites-with-metadata.json`] }); + assert.deepEqual(result, { + "id": "", + "name": "", + "total": 2, + "passed": 2, + "failed": 0, + "errors": 0, + "skipped": 0, + "retried": 0, + "duration": 3.18, + "status": "PASS", + "tags": [], + "metadata": {}, + "suites": [ + { + "id": "", + "name": "Addition", + "total": 1, + "passed": 1, + "failed": 0, + "errors": 0, + "skipped": 0, + "duration": 1.59, + "status": "PASS", + "tags": [ + "@blue", + "@slow" + ], + "metadata": { + "suite": "1234", + "browser": { + "name": "firefox", + "version": "129.0" + }, + "device": "Desktop", + "platform": { + "name": "Windows", + "version": "11" + } + }, + "cases": [ + { + "id": "", + "name": "Addition of two numbers", + "total": 3, + "passed": 3, + "failed": 0, + "errors": 0, + "skipped": 0, + "duration": 1.59, + "status": "PASS", + "failure": "", + "stack_trace": "", + "tags": [ + "@green", + "@fast", + "@blue", + "@slow" + ], + "metadata": { + "testCase": "1234", + "suite": "1234", + "browser": { + "name": "firefox", + "version": "129.0" + }, + "device": "Desktop", + "platform": { + "name": "Windows", + "version": "11" + } + }, + "steps": [ + { + "id": "", + "name": "Given I have number 6 in calculator", + "duration": 1.21, + "status": "PASS", + "failure": "", + "stack_trace": "" + }, + { + "id": "", + "name": "When I entered number 7", + "duration": 0.14, + "status": "PASS", + "failure": "", + "stack_trace": "" + }, + { + "id": "", + "name": "Then I should see result 13", + "duration": 0.24, + "status": "PASS", + "failure": "", + "stack_trace": "" + } + ], + "attachments": [] + } + ] + }, + { + "id": "", + "name": "Addition", + "total": 1, + "passed": 1, + "failed": 0, + "errors": 0, + "skipped": 0, + "duration": 1.59, + "status": "PASS", + "tags": [ + "@blue", + "@slow" + ], + "metadata": { + "suite": "1234", + "browser": { + "name": "chrome", + "version": "129.0" + }, + "device": "Desktop", + "platform": { + "name": "Windows", + "version": "11" + } + }, + "cases": [ + { + "id": "", + "name": "Addition of two numbers", + "total": 3, + "passed": 3, + "failed": 0, + "errors": 0, + "skipped": 0, + "duration": 1.59, + "status": "PASS", + "failure": "", + "stack_trace": "", + "tags": [ + "@green", + "@fast", + "@blue", + "@slow" + ], + "metadata": { + "testCase": "1234", + "suite": "1234", + "browser": { + "name": "chrome", + "version": "129.0" + }, + "device": "Desktop", + "platform": { + "name": "Windows", + "version": "11" + } + }, + "steps": [ + { + "id": "", + "name": "Given I have number 6 in calculator", + "duration": 1.21, + "status": "PASS", + "failure": "", + "stack_trace": "" + }, + { + "id": "", + "name": "When I entered number 7", + "duration": 0.14, + "status": "PASS", + "failure": "", + "stack_trace": "" + }, + { + "id": "", + "name": "Then I should see result 13", + "duration": 0.24, + "status": "PASS", + "failure": "", + "stack_trace": "" + } + ], + "attachments": [] + } + ] + } + ] + }); + }); + });