Skip to content

Commit

Permalink
fix: patch new tab creation for firefox 124 and up to fix issue where… (
Browse files Browse the repository at this point in the history
#29179)

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

* opt for global window instead of no undef for window reference

---------

Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
  • Loading branch information
AtofStryker and jennifer-shehane committed Mar 21, 2024
1 parent ef66293 commit 9c27c37
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
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

5 comments on commit 9c27c37

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9c27c37 Mar 21, 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.7.1/linux-x64/develop-9c27c37b06d5f344f578e639ac0a9c9154c55535/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9c27c37 Mar 21, 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 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.7.1/linux-arm64/develop-9c27c37b06d5f344f578e639ac0a9c9154c55535/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9c27c37 Mar 21, 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.7.1/darwin-x64/develop-9c27c37b06d5f344f578e639ac0a9c9154c55535/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9c27c37 Mar 21, 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.7.1/darwin-arm64/develop-9c27c37b06d5f344f578e639ac0a9c9154c55535/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9c27c37 Mar 21, 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.7.1/win32-x64/develop-9c27c37b06d5f344f578e639ac0a9c9154c55535/cypress.tgz

Please sign in to comment.