Skip to content

Commit

Permalink
feat: add option to ignore chrome preferences (#29447)
Browse files Browse the repository at this point in the history
* 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 <mschile@cypress.io>

* 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 <mschile@cypress.io>

* more fully ignore chrome preferences when env var is set

---------

Co-authored-by: Cacie Prins <cacie@cypress.io>
Co-authored-by: Cacie Prins <cacieprins@users.noreply.github.com>
Co-authored-by: Matt Schile <mschile@cypress.io>
  • Loading branch information
4 people committed Jun 4, 2024
1 parent 4ccdd86 commit e790891
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
8 changes: 4 additions & 4 deletions .circleci/workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 >>
Expand All @@ -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 >>
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

Expand Down
24 changes: 23 additions & 1 deletion packages/server/lib/browsers/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ let browserCriClient: BrowserCriClient | undefined
* @param userDir
*/
const _getChromePreferences = (userDir: string): Bluebird<ChromePreferences> => {
// 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) => {
Expand Down Expand Up @@ -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<void> => {
// 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]
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions packages/server/test/unit/browsers/chrome_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down

4 comments on commit e790891

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on e790891 Jun 4, 2024

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.11.0/linux-x64/develop-e79089136d356242a68d46c02424039ff54e0d10/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on e790891 Jun 4, 2024

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.11.0/darwin-arm64/develop-e79089136d356242a68d46c02424039ff54e0d10/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on e790891 Jun 4, 2024

Choose a reason for hiding this comment

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

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

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.11.0/win32-x64/develop-e79089136d356242a68d46c02424039ff54e0d10/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on e790891 Jun 4, 2024

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.11.0/darwin-x64/develop-e79089136d356242a68d46c02424039ff54e0d10/cypress.tgz

Please sign in to comment.