Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: patch new tab creation for firefox 124 and up to fix issue where… #29179

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down
22 changes: 20 additions & 2 deletions packages/extension/app/v2/background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global window */
const get = require('lodash/get')
const map = require('lodash/map')
const pick = require('lodash/pick')
Expand Down Expand Up @@ -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)
},

Expand Down
27 changes: 27 additions & 0 deletions packages/extension/test/integration/v2/background_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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')
})
})
})
})
})
6 changes: 4 additions & 2 deletions packages/server/lib/browsers/firefox-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
Expand Down
Loading