diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 10e38351f96e..d91ff59bf979 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -3,6 +3,10 @@ _Released 11/5/2024 (PENDING)_ +**Features:** + +- Added ability to specify the default running browser in cypress config. Addresses [#6646](https://github.com/cypress-io/cypress/issues/6646). + **Misc:** - Fixed a typo in CLI `global` option help text. Addresses [#30531](https://github.com/cypress-io/cypress/issues/30531). diff --git a/cli/types/cypress.d.ts b/cli/types/cypress.d.ts index f0f0c914b1c3..5a5be90b3868 100644 --- a/cli/types/cypress.d.ts +++ b/cli/types/cypress.d.ts @@ -3216,6 +3216,11 @@ declare namespace Cypress { setupNodeEvents: (on: PluginEvents, config: PluginConfigOptions) => Promise | PluginConfigOptions | void indexHtmlFile: string + + /** + * Set a default browser when user doesn't pass in "--browser". + */ + defaultBrowser: string } interface EndToEndConfigOptions extends Omit { diff --git a/packages/data-context/src/DataContext.ts b/packages/data-context/src/DataContext.ts index 895dc4d12e4e..9d1b9311d3ac 100644 --- a/packages/data-context/src/DataContext.ts +++ b/packages/data-context/src/DataContext.ts @@ -425,4 +425,8 @@ export class DataContext { this.#awaitingEmptyRequestCount.push(resolve) }) } + + setModeOptionsBrowser (browser: string) { + (this._modeOptions as Partial).browser = browser + } } diff --git a/packages/data-context/src/data/ProjectLifecycleManager.ts b/packages/data-context/src/data/ProjectLifecycleManager.ts index c46d10b5161d..489c3d92bf29 100644 --- a/packages/data-context/src/data/ProjectLifecycleManager.ts +++ b/packages/data-context/src/data/ProjectLifecycleManager.ts @@ -308,10 +308,21 @@ export class ProjectLifecycleManager { /** * Sets the initial `activeBrowser` depending on these criteria, in order of preference: * 1. The value of `--browser` passed via CLI. - * 2. The last browser selected in `open` mode (by name and channel) for this project. - * 3. The first browser found. + * 2. The value of `defaultBrowser` in `cypress.config`. + * 3. The last browser selected in `open` mode (by name and channel) for this project. + * 4. The first browser found. */ async setInitialActiveBrowser () { + const configBrowser = this.loadedFullConfig?.defaultBrowser + + if (configBrowser) { + if (this.ctx.isRunMode && !this.ctx.modeOptions.isBrowserGivenByCli) { + this.ctx.setModeOptionsBrowser(configBrowser) + } + + this.ctx.coreData.cliBrowser ??= configBrowser + } + if (this.ctx.coreData.cliBrowser) { await this.setActiveBrowserByNameOrPath(this.ctx.coreData.cliBrowser) diff --git a/packages/server/lib/modes/index.ts b/packages/server/lib/modes/index.ts index c542b7d807b9..19376aa29c3e 100644 --- a/packages/server/lib/modes/index.ts +++ b/packages/server/lib/modes/index.ts @@ -18,6 +18,7 @@ export = (mode, options) => { quiet: false, morgan: false, report: true, + isBrowserGivenByCli: options.browser !== undefined, }) } diff --git a/packages/types/src/modeOptions.ts b/packages/types/src/modeOptions.ts index f8ab17f81ec2..f12a99399f24 100644 --- a/packages/types/src/modeOptions.ts +++ b/packages/types/src/modeOptions.ts @@ -24,6 +24,7 @@ export interface RunModeOptions extends CommonModeOptions { parallel?: boolean | null ciBuildId?: string | null tag?: (string)[] | null + isBrowserGivenByCli: boolean } export type TestingType = 'e2e' | 'component' diff --git a/system-tests/projects/config-defaultBrowser/cypress.config.js b/system-tests/projects/config-defaultBrowser/cypress.config.js new file mode 100644 index 000000000000..20d060ec1140 --- /dev/null +++ b/system-tests/projects/config-defaultBrowser/cypress.config.js @@ -0,0 +1,8 @@ +const { defineConfig } = require('cypress') + +module.exports = defineConfig({ + e2e: { + supportFile: false, + }, + defaultBrowser: 'chrome', +}) diff --git a/system-tests/projects/config-defaultBrowser/cypress/e2e/spec.cy.js b/system-tests/projects/config-defaultBrowser/cypress/e2e/spec.cy.js new file mode 100644 index 000000000000..434d054dfaa1 --- /dev/null +++ b/system-tests/projects/config-defaultBrowser/cypress/e2e/spec.cy.js @@ -0,0 +1,3 @@ +it('works', () => { + expect(1).to.eq(1) +}) diff --git a/system-tests/test/config_spec.js b/system-tests/test/config_spec.js index b71c4a206a37..e139f9b382b3 100644 --- a/system-tests/test/config_spec.js +++ b/system-tests/test/config_spec.js @@ -243,4 +243,15 @@ describe('e2e config', () => { snapshot: true, }) }) + + it('launches browser using config.defaultBrowser', async function () { + await Fixtures.scaffoldProject('config-defaultBrowser') + + return systemTests.exec(this, { + project: 'config-defaultBrowser', + onStdout: (stdout) => { + expect(stdout).to.include('Browser: Chrome') + }, + }) + }) })