Skip to content

Commit

Permalink
chore: use setWindowOpenHandler over 'new-window' event as it has bee…
Browse files Browse the repository at this point in the history
…n deprecated in Electron v13 and removed in Electron v22
  • Loading branch information
AtofStryker committed Jul 10, 2023
1 parent 17ed1c9 commit e620557
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
12 changes: 3 additions & 9 deletions packages/server/lib/browsers/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export = {
return menu.set({ withInternalDevTools: true })
}
},
async onNewWindow (this: BrowserWindow, e, url) {
async onNewWindow (this: BrowserWindow, { url }) {
let _win: BrowserWindow | null = this

_win.on('closed', () => {
Expand All @@ -168,7 +168,7 @@ export = {
})

try {
const child = await _this._launchChild(e, url, _win, projectRoot, state, options, automation)
const child = await _this._launchChild(url, _win, projectRoot, state, options, automation)

// close child on parent close
_win.on('close', () => {
Expand Down Expand Up @@ -218,9 +218,7 @@ export = {
return await this._launch(win, url, automation, preferences, options.videoApi)
},

_launchChild (e, url, parent, projectRoot, state, options, automation) {
e.preventDefault()

_launchChild (url, parent, projectRoot, state, options, automation) {
const [parentX, parentY] = parent.getPosition()

const electronOptions = this._defaultOptions(projectRoot, state, options, automation)
Expand All @@ -237,10 +235,6 @@ export = {

const win = Windows.create(projectRoot, electronOptions)

// needed by electron since we prevented default and are creating
// our own BrowserWindow (https://electron.atom.io/docs/api/web-contents/#event-new-window)
e.newGuest = win

return this._launch(win, url, automation, electronOptions)
},

Expand Down
14 changes: 11 additions & 3 deletions packages/server/lib/gui/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type WindowOptions = Electron.BrowserWindowConstructorOptions & {
*/
trackState?: TrackStateMap
onFocus?: () => void
onNewWindow?: (e, url, frameName, disposition, options) => Promise<void>
onNewWindow?: ({ disposition, features, frameName, postBody, referrer, url }) => Promise<void>
onCrashed?: () => void
}

Expand Down Expand Up @@ -189,8 +189,16 @@ export function create (projectRoot, _options: WindowOptions, newBrowserWindow =
return options.onCrashed.apply(win, args)
})

win.webContents.on('new-window', function (...args) {
return options.onNewWindow.apply(win, args)
// As of Electron v22, the 'new-window' event has been removed for 'setWindowOpenHandler'.
// @see https://github.com/electron/electron/blob/v21.0.0/docs/api/web-contents.md#event-new-window-deprecated
// @see https://github.com/electron/electron/pull/34526
win.webContents.setWindowOpenHandler(function (...args) {
// opens the child window from the root window so Cypress can decorate it with needed events and configuration
options.onNewWindow.apply(win, args)

// Because the opening of the window is handled by the root window above, deny opening the window.
// @see https://github.com/electron/electron/blob/v21.0.0/docs/api/web-contents.md#contentssetwindowopenhandlerhandler
return { action: 'deny' }
})

if (options.trackState) {
Expand Down
9 changes: 3 additions & 6 deletions packages/server/test/unit/browsers/electron_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,19 +797,16 @@ describe('lib/browsers/electron', () => {
return sinon.stub(electron, '_launchChild').resolves(this.win)
})

it('passes along event, url, parent window and options', function () {
it('passes along url, parent window and options', function () {
const opts = electron._defaultOptions(this.options.projectRoot, this.state, this.options, this.automation)

const event = {}
const parentWindow = {
on: sinon.stub(),
}

opts.onNewWindow.call(parentWindow, event, this.url)
opts.onNewWindow.call(parentWindow, { url: this.url })

expect(electron._launchChild).to.be.calledWith(
event, this.url, parentWindow, this.options.projectRoot, this.state, this.options, this.automation,
)
expect(electron._launchChild).to.be.calledWith(this.url, parentWindow, this.options.projectRoot, this.state, this.options, this.automation)
})

it('adds pid of new BrowserWindow to allPids list', function () {
Expand Down

0 comments on commit e620557

Please sign in to comment.