diff --git a/.changeset/pr-279.md b/.changeset/pr-279.md new file mode 100644 index 00000000..4e622b9c --- /dev/null +++ b/.changeset/pr-279.md @@ -0,0 +1,5 @@ +--- +"@wdio/browserstack-service": minor +--- + +- `browser.setCustomTags()` now works with WebdriverIO + CucumberJS: custom metadata set in steps or Cucumber `Before`/`After` hooks is reported on the scenario in Test Reporting & Analytics. diff --git a/packages/browserstack-service/src/cli/modules/customTagsModule.ts b/packages/browserstack-service/src/cli/modules/customTagsModule.ts index b70b679b..f3d1f532 100644 --- a/packages/browserstack-service/src/cli/modules/customTagsModule.ts +++ b/packages/browserstack-service/src/cli/modules/customTagsModule.ts @@ -11,6 +11,7 @@ import { TestFrameworkState } from '../states/testFrameworkState.js' import { TestFrameworkConstants } from '../frameworks/constants/testFrameworkConstants.js' import { CLIUtils } from '../cliUtils.js' import WdioMochaTestFramework from '../frameworks/wdioMochaTestFramework.js' +import WdioCucumberTestFramework from '../frameworks/wdioCucumberTestFramework.js' import { BrowserstackCLI } from '../index.js' import { mergeIntoTags, parseCommaSeparatedValues, getCurrentMochaHookWindow } from '../../customTags.js' import type { CustomMetadata } from '../../customTags.js' @@ -55,8 +56,9 @@ export default class CustomTagsModule extends BaseModule { return CustomTagsModule.MODULE_NAME } - private isMochaFramework(): boolean { - return BrowserstackCLI.getInstance().getTestFramework() instanceof WdioMochaTestFramework + private isSupportedFramework(): boolean { + const framework = BrowserstackCLI.getInstance().getTestFramework() + return framework instanceof WdioMochaTestFramework || framework instanceof WdioCucumberTestFramework } async onBeforeExecute() { @@ -75,14 +77,11 @@ export default class CustomTagsModule extends BaseModule { (browser as WebdriverIO.Browser).setCustomTags = async (key: string, value: string): Promise => { try { - // Mocha-only, matching the legacy handler (custom-tags-handler, which warns - // and no-ops for any framework other than mocha). The method is still - // registered so the call resolves and the warning reaches the user, exactly - // as on the legacy flow. Without this gate the CLI path silently STARTS - // supporting custom tags for other frameworks — a widening, but an - // unrequested behaviour change all the same. - if (!this.isMochaFramework()) { - this.logger.warn('setCustomTags is only supported for the mocha framework; ignoring call') + // Registered for every framework so the call always resolves; only the + // CLI frameworks that mint a per-test instance (a Mocha test, a Cucumber + // scenario) can carry the tags. + if (!this.isSupportedFramework()) { + this.logger.warn('setCustomTags is only supported for the mocha and cucumber frameworks; ignoring call') return } if (!key || !value) { diff --git a/packages/browserstack-service/tests/cli/modules/customTagsModule.test.ts b/packages/browserstack-service/tests/cli/modules/customTagsModule.test.ts index 32b98d3f..f8ddb624 100644 --- a/packages/browserstack-service/tests/cli/modules/customTagsModule.test.ts +++ b/packages/browserstack-service/tests/cli/modules/customTagsModule.test.ts @@ -33,9 +33,8 @@ import { BrowserstackCLI } from '../../../src/cli/index.js' import { BStackLogger } from '../../../src/cli/cliLogger.js' /** - * 8-D. `setCustomTags` warns and no-ops for every framework except mocha, which - * is what the legacy custom-tags-handler does. Discriminating: the SAME call merges tags under - * mocha and merges nothing under cucumber. + * `setCustomTags` merges for the CLI-supported frameworks (mocha, cucumber) and warns and + * no-ops when no CLI test framework is registered. */ describe('CustomTagsModule — framework gate', () => { let module: CustomTagsModule @@ -82,16 +81,16 @@ describe('CustomTagsModule — framework gate', () => { expect(instance.updateMultipleEntries).toHaveBeenCalled() }) - it('warns and no-ops for cucumber — the opposite answer on the same call', async () => { + it('merges tags into the current scenario for cucumber', async () => { useFramework(Object.create(WdioCucumberTestFramework.prototype)) const setCustomTags = await register() await setCustomTags('TC', 'TC-1,TC-2') - expect(instance.updateMultipleEntries).not.toHaveBeenCalled() - expect(BStackLogger.warn).toHaveBeenCalledWith( - 'setCustomTags is only supported for the mocha framework; ignoring call' - ) + expect(instance.updateMultipleEntries).toHaveBeenCalledWith({ + custom_metadata: { TC: { field_type: 'multi_dropdown', values: ['TC-1', 'TC-2'] } } + }) + expect(BStackLogger.warn).not.toHaveBeenCalled() }) it('warns and no-ops when no CLI test framework is registered', async () => { @@ -102,7 +101,7 @@ describe('CustomTagsModule — framework gate', () => { expect(instance.updateMultipleEntries).not.toHaveBeenCalled() expect(BStackLogger.warn).toHaveBeenCalledWith( - 'setCustomTags is only supported for the mocha framework; ignoring call' + 'setCustomTags is only supported for the mocha and cucumber frameworks; ignoring call' ) }) })