Skip to content

Commit

Permalink
feat: Add support for optional env key to browser launch options (#23624
Browse files Browse the repository at this point in the history
)

Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
  • Loading branch information
ryanjduffy and lmiller1990 authored Sep 22, 2022
1 parent 90729e7 commit acc1050
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 72 deletions.
1 change: 1 addition & 0 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5495,6 +5495,7 @@ declare namespace Cypress {
extensions: string[]
preferences: { [key: string]: any }
args: string[]
env: { [key: string]: any }
}

interface Dimensions {
Expand Down
1 change: 1 addition & 0 deletions cli/types/tests/plugins-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const pluginConfig2: Cypress.PluginConfig = (on, config) => {
browser.displayName // $ExpectType string
options.extensions // $ExpectType string[]
options.args // $ExpectType string[]
options.env // $ExpectType { [key: string]: any; }

console.log('launching browser', browser.displayName)
return options
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/errors/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export const AllCypressErrors = {
This option will not have an effect in ${fmt.off(_.capitalize(browser))}. Tests that rely on web security being disabled will not run as expected.`
},
BROWSER_UNSUPPORTED_LAUNCH_OPTION: (browser: string, options: string[]) => {
return errTemplate`\
Warning: The following browser launch options were provided but are not supported by ${fmt.highlightSecondary(browser)}
${fmt.listItems(options)}`
},
BROWSER_NOT_FOUND_BY_NAME: (browser: string, foundBrowsersStr: string[]) => {
let canarySuffix: PartialErr | null = null

Expand Down
6 changes: 6 additions & 0 deletions packages/errors/test/unit/visualSnapshotErrors_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1206,5 +1206,11 @@ describe('visual error templates', () => {
default: [],
}
},

BROWSER_UNSUPPORTED_LAUNCH_OPTION: () => {
return {
default: ['electron', ['env']],
}
},
})
})
6 changes: 4 additions & 2 deletions packages/launcher/lib/browsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export function launch (
url: string,
debuggingPort: number,
args: string[] = [],
defaultBrowserEnv = {},
browserEnv = {},
) {
debug('launching browser %o', { browser, url })

Expand All @@ -181,7 +181,9 @@ export function launch (

// allow setting default env vars such as MOZ_HEADLESS_WIDTH
// but only if it's not already set by the environment
const env = Object.assign({}, defaultBrowserEnv, process.env)
const env = Object.assign({}, browserEnv, process.env)

debug('spawning browser with environment %o', { env })

const proc = cp.spawn(browser.path, args, { stdio: ['ignore', 'pipe', 'pipe'], env })

Expand Down
2 changes: 1 addition & 1 deletion packages/server/lib/browsers/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ export = {
// first allows us to connect the remote interface,
// start video recording and then
// we will load the actual page
const launchedBrowser = await launch(browser, 'about:blank', port, args) as unknown as BrowserInstance & { browserCriClient: BrowserCriClient}
const launchedBrowser = await launch(browser, 'about:blank', port, args, launchOptions.env) as unknown as BrowserInstance & { browserCriClient: BrowserCriClient}

la(launchedBrowser, 'did not get launched browser instance')

Expand Down
14 changes: 14 additions & 0 deletions packages/server/lib/browsers/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,18 @@ export = {
throw new Error('Attempting to connect to existing browser for Cypress in Cypress which is not yet implemented for electron')
},

validateLaunchOptions (launchOptions: typeof utils.defaultLaunchOptions) {
const options: string[] = []

if (Object.keys(launchOptions.env).length > 0) options.push('env')

if (launchOptions.args.length > 0) options.push('args')

if (options.length > 0) {
errors.warning('BROWSER_UNSUPPORTED_LAUNCH_OPTION', 'electron', options)
}
},

async open (browser: Browser, url: string, options: BrowserLaunchOpts, automation: Automation) {
debug('open %o', { browser, url })

Expand All @@ -486,6 +498,8 @@ export = {

const launchOptions = await utils.executeBeforeBrowserLaunch(browser, defaultLaunchOptions, electronOptions)

this.validateLaunchOptions(launchOptions)

const { preferences } = launchOptions

debug('launching browser window to url: %s', url)
Expand Down
1 change: 1 addition & 0 deletions packages/server/lib/browsers/firefox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ export async function open (browser: Browser, url: string, options: BrowserLaunc
// user can overwrite this default with these env vars or --height, --width arguments
MOZ_HEADLESS_WIDTH: '1280',
MOZ_HEADLESS_HEIGHT: '721',
...launchOptions.env,
})

try {
Expand Down
2 changes: 2 additions & 0 deletions packages/server/lib/browsers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ const defaultLaunchOptions: {
preferences: {[key: string]: any}
extensions: string[]
args: string[]
env: {[key: string]: any}
} = {
preferences: {},
extensions: [],
args: [],
env: {},
}

const KNOWN_LAUNCH_OPTION_PROPERTIES = _.keys(defaultLaunchOptions)
Expand Down
1 change: 1 addition & 0 deletions packages/server/lib/browsers/webkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export async function open (browser: Browser, url: string, options: BrowserLaunc
},
extensions: [],
args: [],
env: {},
}

const launchOptions = await utils.executeBeforeBrowserLaunch(browser, defaultLaunchOptions, options)
Expand Down
Loading

4 comments on commit acc1050

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on acc1050 Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.9.0/linux-x64/develop-acc1050dd2659182fa87729be5ce9c3ca99de94b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on acc1050 Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.9.0/linux-arm64/develop-acc1050dd2659182fa87729be5ce9c3ca99de94b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on acc1050 Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.9.0/darwin-arm64/develop-acc1050dd2659182fa87729be5ce9c3ca99de94b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on acc1050 Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.9.0/darwin-x64/develop-acc1050dd2659182fa87729be5ce9c3ca99de94b/cypress.tgz

Please sign in to comment.