From e79089136d356242a68d46c02424039ff54e0d10 Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Tue, 4 Jun 2024 07:17:06 -0700 Subject: [PATCH] feat: add option to ignore chrome preferences (#29447) * feat: add option to ignore chrome preferences * build binary on this branch * adds explanation comment and test * update changelog from dev * Update packages/server/lib/browsers/chrome.ts Co-authored-by: Matt Schile * put disableRestorePagesPrompt back in promise list in browser open * Update cli/CHANGELOG.md * ensure we skip writing to chrome prefs when env is set * changelog update * Update packages/server/lib/browsers/chrome.ts Co-authored-by: Matt Schile * more fully ignore chrome preferences when env var is set --------- Co-authored-by: Cacie Prins Co-authored-by: Cacie Prins Co-authored-by: Matt Schile --- .circleci/workflows.yml | 8 ++--- cli/CHANGELOG.md | 1 + packages/server/lib/browsers/chrome.ts | 24 +++++++++++++- .../server/test/unit/browsers/chrome_spec.js | 32 +++++++++++++++++++ 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/.circleci/workflows.yml b/.circleci/workflows.yml index 9869aa284fa7..bfc741b0e2ee 100644 --- a/.circleci/workflows.yml +++ b/.circleci/workflows.yml @@ -30,8 +30,8 @@ mainBuildFilters: &mainBuildFilters - /^release\/\d+\.\d+\.\d+$/ # use the following branch as well to ensure that v8 snapshot cache updates are fully tested - 'update-v8-snapshot-cache-on-develop' + - 'ignore-chrom-prefs' - 'publish-binary' - - 'feat/support_next_14' # usually we don't build Mac app - it takes a long time # but sometimes we want to really confirm we are doing the right thing @@ -42,7 +42,7 @@ macWorkflowFilters: &darwin-workflow-filters - equal: [ develop, << pipeline.git.branch >> ] # use the following branch as well to ensure that v8 snapshot cache updates are fully tested - equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ] - - equal: [ 'feat/support_next_14', << pipeline.git.branch >> ] + - equal: [ 'ignore-chrom-prefs', << pipeline.git.branch >> ] - matches: pattern: /^release\/\d+\.\d+\.\d+$/ value: << pipeline.git.branch >> @@ -53,7 +53,7 @@ linuxArm64WorkflowFilters: &linux-arm64-workflow-filters - equal: [ develop, << pipeline.git.branch >> ] # use the following branch as well to ensure that v8 snapshot cache updates are fully tested - equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ] - - equal: [ 'feat/support_next_14', << pipeline.git.branch >> ] + - equal: [ 'ignore-chrom-prefs', << pipeline.git.branch >> ] - matches: pattern: /^release\/\d+\.\d+\.\d+$/ value: << pipeline.git.branch >> @@ -152,7 +152,7 @@ commands: name: Set environment variable to determine whether or not to persist artifacts command: | echo "Setting SHOULD_PERSIST_ARTIFACTS variable" - echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "feat/support_next_14" ]]; then + echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "ignore-chrom-prefs" ]]; then export SHOULD_PERSIST_ARTIFACTS=true fi' >> "$BASH_ENV" # You must run `setup_should_persist_artifacts` command and be using bash before running this command diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index b1050db6ef47..8a3299329702 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -6,6 +6,7 @@ _Released 6/4/2024 (PENDING)_ **Features:** - Added support for [Next.js 14](https://nextjs.org/blog/next-14) for component testing. Addresses [#28185](https://github.com/cypress-io/cypress/issues/28185). +- Added an `IGNORE_CHROME_PREFERENCES` environment variable to ignore Chrome preferences when launching Chrome. Addresses [#29330](https://github.com/cypress-io/cypress/issues/29330). **Performance:** diff --git a/packages/server/lib/browsers/chrome.ts b/packages/server/lib/browsers/chrome.ts index 1c4c034436b3..33ae2d6643f9 100644 --- a/packages/server/lib/browsers/chrome.ts +++ b/packages/server/lib/browsers/chrome.ts @@ -55,6 +55,15 @@ let browserCriClient: BrowserCriClient | undefined * @param userDir */ const _getChromePreferences = (userDir: string): Bluebird => { + // skip reading the preferences if requested by the user, + // typically used when the AUT encrypts the user data dir, causing relaunches of the browser not to work + // see https://github.com/cypress-io/cypress/issues/29330 + if (process.env.IGNORE_CHROME_PREFERENCES) { + debug('ignoring chrome preferences: not reading from chrome preference files') + + return Bluebird.resolve(_.mapValues(CHROME_PREFERENCE_PATHS, () => ({}))) + } + debug('reading chrome preferences... %o', { userDir, CHROME_PREFERENCE_PATHS }) return Bluebird.props(_.mapValues(CHROME_PREFERENCE_PATHS, (prefPath) => { @@ -95,7 +104,16 @@ const _mergeChromePreferences = (originalPrefs: ChromePreferences, newPrefs: Chr }) } -const _writeChromePreferences = (userDir: string, originalPrefs: ChromePreferences, newPrefs: ChromePreferences) => { +const _writeChromePreferences = (userDir: string, originalPrefs: ChromePreferences, newPrefs: ChromePreferences): Promise => { + // skip writing the preferences if requested by the user, + // typically used when the AUT encrypts the user data dir, causing relaunches of the browser not to work + // see https://github.com/cypress-io/cypress/issues/29330 + if (process.env.IGNORE_CHROME_PREFERENCES) { + debug('ignoring chrome preferences: not writing to preference files') + + return Promise.resolve() + } + return Bluebird.map(_.keys(originalPrefs), (key) => { const originalJson = originalPrefs[key] const newJson = newPrefs[key] @@ -154,6 +172,10 @@ const _removeRootExtension = () => { // https://github.com/cypress-io/cypress/issues/2048 const _disableRestorePagesPrompt = function (userDir) { + if (process.env.IGNORE_CHROME_PREFERENCES) { + return Promise.resolve() + } + const prefsPath = path.join(userDir, 'Default', 'Preferences') return fs.readJson(prefsPath) diff --git a/packages/server/test/unit/browsers/chrome_spec.js b/packages/server/test/unit/browsers/chrome_spec.js index 174455668bc5..b09f4de1b3bf 100644 --- a/packages/server/test/unit/browsers/chrome_spec.js +++ b/packages/server/test/unit/browsers/chrome_spec.js @@ -192,6 +192,38 @@ describe('lib/browsers/chrome', () => { }) }) + context('when IGNORE_CHROME_PREFERENCES env is set', () => { + let oldPref + let writeJson + + beforeEach(function () { + oldPref = process.env.IGNORE_CHROME_PREFERENCES + process.env.IGNORE_CHROME_PREFERENCES = true + this.readJson.rejects({ code: 'ENOENT' }) + writeJson = sinon.stub(fs, 'outputJson').resolves() + }) + + afterEach(() => { + process.env.IGNORE_CHROME_PREFERENCES = oldPref + writeJson.restore() + }) + + it('does not read or write preferences', async function () { + chrome._writeExtension.restore() + utils.getProfileDir.restore() + + await chrome.open({ + isHeadless: true, + isHeaded: false, + name: 'chromium', + channel: 'stable', + }, 'http://', openOpts, this.automation) + + expect(writeJson).not.to.be.called + expect(this.readJson).not.to.be.called + }) + }) + it('DEPRECATED: normalizes --load-extension if provided in plugin', function () { plugins.registerEvent('before:browser:launch', (browser, config) => { return Promise.resolve(['--foo=bar', '--load-extension=/foo/bar/baz.js'])