From 9c27c37b06d5f344f578e639ac0a9c9154c55535 Mon Sep 17 00:00:00 2001 From: Bill Glesias Date: Thu, 21 Mar 2024 13:37:23 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20patch=20new=20tab=20creation=20for=20fir?= =?UTF-8?q?efox=20124=20and=20up=20to=20fix=20issue=20where=E2=80=A6=20(#2?= =?UTF-8?q?9179)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: patch new tab creation for firefox 124 and up to fix issue where cypress was not executing beyond the first spec in cypress run (specific to firefox). * Update cli/CHANGELOG.md Co-authored-by: Jennifer Shehane * opt for global window instead of no undef for window reference --------- Co-authored-by: Jennifer Shehane --- cli/CHANGELOG.md | 1 + packages/extension/app/v2/background.js | 22 +++++++++++++-- .../test/integration/v2/background_spec.js | 27 +++++++++++++++++++ packages/server/lib/browsers/firefox-util.ts | 6 +++-- 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 34cfe1c4db83..0aff63b0dde7 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -5,6 +5,7 @@ _Released 3/26/2024 (PENDING)_ **Bugfixes:** +- Fixed an issue where Cypress was not executing beyond the first spec in `cypress run` for versions of Firefox 124 and up. Fixes [#29172](https://github.com/cypress-io/cypress/issues/29172). - Fixed an issue blurring shadow dom elements. Fixed in [#29125](https://github.com/cypress-io/cypress/pull/29125). **Dependency Updates:** diff --git a/packages/extension/app/v2/background.js b/packages/extension/app/v2/background.js index 97ae70e24b42..6be1573b4343 100644 --- a/packages/extension/app/v2/background.js +++ b/packages/extension/app/v2/background.js @@ -1,3 +1,4 @@ +/* global window */ const get = require('lodash/get') const map = require('lodash/map') const pick = require('lodash/pick') @@ -287,8 +288,25 @@ const automation = { resetBrowserTabsForNextTest (fn) { return Promise.try(() => { return browser.windows.getCurrent({ populate: true }) - }).then((windowInfo) => { - return browser.tabs.remove(windowInfo.tabs.map((tab) => tab.id)) + }).then(async (windowInfo) => { + let newTabId = null + + try { + // credit to https://stackoverflow.com/questions/7000190/detect-all-firefox-versions-in-js + const match = window.navigator.userAgent.match(/Firefox\/([0-9]+)\./) + const version = match ? parseInt(match[1]) : 0 + + // in versions of Firefox 124 and up, firefox no longer creates a new tab for us when we close all tabs in the browser. + // to keep change minimal and backwards compatible, we are creating an 'about:blank' tab here to keep the behavior consistent. + if (version >= 124) { + const newAboutBlankTab = await browser.tabs.create({ url: 'about:blank', active: false }) + + newTabId = newAboutBlankTab.id + } + // eslint-disable-next-line no-empty + } catch (e) {} + + return browser.tabs.remove(windowInfo.tabs.map((tab) => tab.id).filter((tab) => tab.id !== newTabId)) }).then(fn) }, diff --git a/packages/extension/test/integration/v2/background_spec.js b/packages/extension/test/integration/v2/background_spec.js index 9a54082ff9c7..d46353054e34 100644 --- a/packages/extension/test/integration/v2/background_spec.js +++ b/packages/extension/test/integration/v2/background_spec.js @@ -848,6 +848,9 @@ describe('app/background', () => { beforeEach(() => { sinon.stub(browser.windows, 'getCurrent').withArgs({ populate: true }).resolves({ id: '10', tabs: [{ id: '1' }, { id: '2' }, { id: '3' }] }) sinon.stub(browser.tabs, 'remove').withArgs(['1', '2', '3']).resolves() + sinon.stub(browser.tabs, 'create').withArgs({ url: 'about:blank', active: false }).resolves({ + id: 'new-tab', + }) }) it('closes the tabs in the current browser window', function (done) { @@ -857,12 +860,36 @@ describe('app/background', () => { expect(browser.windows.getCurrent).to.be.called expect(browser.tabs.remove).to.be.called + expect(browser.tabs.create).not.to.be.called done() }) this.server.emit('automation:request', 123, 'reset:browser:tabs:for:next:test') }) + + // @see https://github.com/cypress-io/cypress/issues/29172 + describe('firefox 124 and up', () => { + beforeEach(() => { + global.window.navigator = { + userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0', + } + }) + + it('creates a new "about:blank" tab and closes the other tabs in the current browser window', function (done) { + this.socket.on('automation:response', (id, obj) => { + expect(id).to.eq(123) + expect(obj.response).to.be.undefined + + expect(browser.windows.getCurrent).to.be.called + expect(browser.tabs.remove).to.be.calledWith(['1', '2', '3']) + expect(browser.tabs.create).to.be.calledWith({ url: 'about:blank', active: false }) + done() + }) + + this.server.emit('automation:request', 123, 'reset:browser:tabs:for:next:test') + }) + }) }) }) }) diff --git a/packages/server/lib/browsers/firefox-util.ts b/packages/server/lib/browsers/firefox-util.ts index 6661e81b8936..38fff4cf01fa 100644 --- a/packages/server/lib/browsers/firefox-util.ts +++ b/packages/server/lib/browsers/firefox-util.ts @@ -105,8 +105,10 @@ const attachToTabMemory = Bluebird.method((tab) => { }) async function connectMarionetteToNewTab () { - // When firefox closes its last tab, it keeps a blank tab open. This will be the only handle - // So we will connect to it and navigate it to about:blank to set it up for CDP connection + // Firefox keeps a blank tab open in versions of Firefox 123 and lower when the last tab is closed. + // For versions 124 and above, a new tab is not created, so @packages/extension creates one for us. + // Since the tab is always available on our behalf, + // we can connect to it here and navigate it to about:blank to set it up for CDP connection const handles = await sendMarionette({ name: 'WebDriver:GetWindowHandles', })