Skip to content

Commit

Permalink
Handle file:// in specs of junit reporter - fixes #9352 (#10035)
Browse files Browse the repository at this point in the history
* Handle file:// in specs of junit reporter - fixes #9352

* Update packages/wdio-junit-reporter/tests/reporter.test.ts

---------

Co-authored-by: Christian Bromann <git@bromann.dev>
  • Loading branch information
nextlevelbeard and christian-bromann committed Mar 23, 2023
1 parent dcf7e89 commit 6b3bb1e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/wdio-junit-reporter/src/index.ts
Expand Up @@ -3,6 +3,8 @@ import type { SuiteStats, RunnerStats, TestStats } from '@wdio/reporter'
import WDIOReporter from '@wdio/reporter'
import type { Capabilities } from '@wdio/types'

const FILE_PROTOCOL_REGEX = /^file:\/\//

import { limit } from './utils.js'
import type { JUnitReporterOptions } from './types.js'

Expand Down Expand Up @@ -268,7 +270,10 @@ class JunitReporter extends WDIOReporter {
continue
}
const suite = this.suites[suiteKey]
if (isCucumberFrameworkRunner && suite.type === type && specFileName === suite.file) {

const sameFeature = isCucumberFrameworkRunner && specFileName.replace(FILE_PROTOCOL_REGEX, '') === suite.file.replace(FILE_PROTOCOL_REGEX, '')

if (isCucumberFrameworkRunner && suite.type === type && sameFeature) {
builder = this._addCucumberFeatureToBuilder(builder, runner, specFileName, suite)
} else if (!isCucumberFrameworkRunner) {
builder = this._addSuiteToBuilder(builder, runner, specFileName, suite)
Expand Down
Expand Up @@ -16,6 +16,10 @@ exports[`wdio-junit-reporter > generates xml output (Cucumber-style) (with packa

exports[`wdio-junit-reporter > generates xml output (Cucumber-style) 1`] = `"<?xmlversion=\\"1.0\\"encoding=\\"UTF-8\\"?><testsuites><testsuitename=\\"Samplefeature\\"timestamp=\\"2019-12-13T19:10:04.659Z\\"time=\\"15.907\\"tests=\\"1\\"failures=\\"1\\"errors=\\"1\\"skipped=\\"0\\"><properties><propertyname=\\"specId\\"value=\\"0\\"/><propertyname=\\"featureName\\"value=\\"Samplefeature\\"/><propertyname=\\"capabilities\\"value=\\"iphone.ios.12_1\\"/><propertyname=\\"featureFile\\"value=\\"./features/sample_feature.feature\\"/></properties><testcaseclassname=\\"CucumberJUnitReport-iphone.ios.12_1.Samplefeature\\"name=\\"Samplescenario\\"time=\\"1.755\\"><failure/><errormessage=\\"expectedtruetoequalfalse\\"/><system-out><![CDATA[✅Givenstephaspassed✅Whenstephaspassed❗Thenstephasfailed]]></system-out><system-err><![CDATA[AssertionError:expectedtruetoequalfalseatWorld.<anonymous>(./step_definitions/steps.js:642:21)]]></system-err></testcase></testsuite></testsuites>"`;

exports[`wdio-junit-reporter > generates xml output (Cucumber-style) when feature has file:// protocol 1`] = `"<?xmlversion=\\"1.0\\"encoding=\\"UTF-8\\"?><testsuites><testsuitename=\\"Samplefeature\\"timestamp=\\"2019-12-13T19:10:04.659Z\\"time=\\"15.907\\"tests=\\"1\\"failures=\\"1\\"errors=\\"1\\"skipped=\\"0\\"><properties><propertyname=\\"specId\\"value=\\"0\\"/><propertyname=\\"featureName\\"value=\\"Samplefeature\\"/><propertyname=\\"capabilities\\"value=\\"iphone.ios.12_1\\"/><propertyname=\\"featureFile\\"value=\\"file://./features/sample_feature.feature\\"/></properties><testcaseclassname=\\"CucumberJUnitReport-iphone.ios.12_1.Samplefeature\\"name=\\"Samplescenario\\"time=\\"1.755\\"><failure/><errormessage=\\"expectedtruetoequalfalse\\"/><system-out><![CDATA[✅Givenstephaspassed✅Whenstephaspassed❗Thenstephasfailed]]></system-out><system-err><![CDATA[AssertionError:expectedtruetoequalfalseatWorld.<anonymous>(./step_definitions/steps.js:642:21)]]></system-err></testcase></testsuite></testsuites>"`;

exports[`wdio-junit-reporter > generates xml output (Cucumber-style) when feature has file:// protocol and the addFileAttribute option is set 1`] = `"<?xmlversion=\\"1.0\\"encoding=\\"UTF-8\\"?><testsuites><testsuitename=\\"Samplefeature\\"timestamp=\\"2019-12-13T19:10:04.659Z\\"time=\\"15.907\\"tests=\\"1\\"failures=\\"1\\"errors=\\"1\\"skipped=\\"0\\"><properties><propertyname=\\"specId\\"value=\\"0\\"/><propertyname=\\"featureName\\"value=\\"Samplefeature\\"/><propertyname=\\"capabilities\\"value=\\"iphone.ios.12_1\\"/><propertyname=\\"featureFile\\"value=\\"file://./features/sample_feature.feature\\"/></properties><testcaseclassname=\\"CucumberJUnitReport-iphone.ios.12_1.Samplefeature\\"name=\\"Samplescenario\\"time=\\"1.755\\"file=\\"file://./features/sample_feature.feature\\"><failure/><errormessage=\\"expectedtruetoequalfalse\\"/><system-out><![CDATA[✅Givenstephaspassed✅Whenstephaspassed❗Thenstephasfailed]]></system-out><system-err><![CDATA[AssertionError:expectedtruetoequalfalseatWorld.<anonymous>(./step_definitions/steps.js:642:21)]]></system-err></testcase></testsuite></testsuites>"`;

exports[`wdio-junit-reporter > generates xml output 1`] = `"<?xmlversion=\\"1.0\\"encoding=\\"UTF-8\\"?><testsuites><testsuitename=\\"Myawesomefeature\\"timestamp=\\"2018-04-17T09:10:10.255Z\\"time=\\"0.745\\"tests=\\"3\\"failures=\\"1\\"errors=\\"1\\"skipped=\\"1\\"><properties><propertyname=\\"specId\\"value=\\"0\\"/><propertyname=\\"suiteName\\"value=\\"Myawesomefeature\\"/><propertyname=\\"capabilities\\"value=\\"chrome.65_0_3325_181.macosx\\"/><propertyname=\\"file\\"value=\\"/path/to/project/test/specs/sync.spec.js\\"/></properties><testcaseclassname=\\"chrome.65_0_3325_181.macosx.My_awesome_feature\\"name=\\"shouldcandosomething\\"time=\\"0.727\\"><system-out><![CDATA[COMMAND:POST/session/4875944f97c944d80e944a399b682656/url-{\\"url\\":\\"http://google.com/\\"}RESULT:{\\"url\\":\\"http://google.com/\\"}COMMAND:GET/session/4875944f97c944d80e944a399b682656/title-{}RESULT:{}]]></system-out></testcase><testcaseclassname=\\"chrome.65_0_3325_181.macosx.My_awesome_feature\\"name=\\"shouldskipatest\\"time=\\"0\\"><skipped/></testcase><testcaseclassname=\\"chrome.65_0_3325_181.macosx.My_awesome_feature\\"name=\\"shouldfailatest\\"time=\\"0.014\\"><failure/><errormessage=\\"uupsi\\"/><system-out><![CDATA[COMMAND:GET/session/4875944f97c944d80e944a399b682656/log/types-{}RESULT:{}COMMAND:POST/session/4875944f97c944d80e944a399b682656/log-{\\"type\\":\\"browser\\"}COMMAND:POST/session/4875944f97c944d80e944a399b682656/log-{\\"type\\":\\"driver\\"}RESULT:{\\"type\\":\\"driver\\"}RESULT:{\\"type\\":\\"browser\\"}COMMAND:DELETE/session/4875944f97c944d80e944a399b682656-{}RESULT:{}]]></system-out><system-err><![CDATA[Error:uupsiatContext.it(/path/to/project/test/specs/sync.spec.js:18:15)at/path/to/project/packages/wdio-sync/src/index.js:28:28atPromise(<anonymous>)atF(/path/to/project/node_modules/core-js/library/modules/_export.js:35:28)atContext.executeSync(/path/to/project/packages/wdio-sync/src/index.js:26:12)at/path/to/project/packages/wdio-sync/src/index.js:161:33]]></system-err></testcase></testsuite></testsuites>"`;

exports[`wdio-junit-reporter > generates xml output correctly when error options are set (Cucumber-style) 1`] = `"<?xmlversion=\\"1.0\\"encoding=\\"UTF-8\\"?><testsuites><testsuitename=\\"Samplefeature\\"timestamp=\\"2019-12-13T19:10:04.659Z\\"time=\\"15.907\\"tests=\\"1\\"failures=\\"1\\"errors=\\"1\\"skipped=\\"0\\"><properties><propertyname=\\"specId\\"value=\\"0\\"/><propertyname=\\"featureName\\"value=\\"Samplefeature\\"/><propertyname=\\"capabilities\\"value=\\"iphone.ios.12_1\\"/><propertyname=\\"featureFile\\"value=\\"./features/sample_feature.feature\\"/></properties><testcaseclassname=\\"CucumberJUnitReport-iphone.ios.12_1.Samplefeature\\"name=\\"Samplescenario\\"time=\\"1.755\\"><failuremessage=\\"expectedtruetoequalfalse\\"><![CDATA[AssertionError:expectedtruetoequalfalseatWorld.<anonymous>(./step_definitions/steps.js:642:21)]]></failure><errormessage=\\"expectedtruetoequalfalse\\"/><system-out><![CDATA[✅Givenstephaspassed✅Whenstephaspassed❗Thenstephasfailed]]></system-out><system-err><![CDATA[AssertionError:expectedtruetoequalfalseatWorld.<anonymous>(./step_definitions/steps.js:642:21)]]></system-err></testcase></testsuite></testsuites>"`;
Expand Down
20 changes: 20 additions & 0 deletions packages/wdio-junit-reporter/tests/reporter.test.ts
Expand Up @@ -115,6 +115,26 @@ describe('wdio-junit-reporter', () => {
expect(reporter['_buildJunitXml'](mochaRunnerLog as any).replace(/\s/g, '')).toMatchSnapshot()
})

it('generates xml output (Cucumber-style) when feature has file:// protocol', () => {
reporter = new WDIOJunitReporter({ stdout: true })
reporter.suites = { ...featuresLog } as any
const runner = { ...cucumberRunnerLog }
runner.specs = runner.specs.map(s => `file://${s}`)

// verifies the content of the report but omits format by stripping all whitespace and new lines
expect(reporter['_buildJunitXml'](runner as any).replace(/\s/g, '')).toMatchSnapshot()
})

it('generates xml output (Cucumber-style) when feature has file:// protocol and the addFileAttribute option is set', () => {
reporter = new WDIOJunitReporter({ stdout: true, addFileAttribute: true })
reporter.suites = { ...featuresLog } as any
const runner = { ...cucumberRunnerLog }
runner.specs = runner.specs.map(s => `file://${s}`)

// verifies the content of the report but omits format by stripping all whitespace and new lines
expect(reporter['_buildJunitXml'](runner as any).replace(/\s/g, '')).toMatchSnapshot()
})

it('generates xml output (Cucumber-style)', () => {
reporter.suites = featuresLog as any

Expand Down

0 comments on commit 6b3bb1e

Please sign in to comment.