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

chore: use setWindowOpenHandler over 'new-window' event as it has bee… #27256

Merged
merged 1 commit into from
Jul 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
1 change: 1 addition & 0 deletions packages/server/test/unit/gui/windows_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('lib/gui/windows', () => {
this.win.getPosition = sinon.stub().returns([3, 4])
this.win.webContents = new EventEmitter()
this.win.webContents.openDevTools = sinon.stub()
this.win.webContents.setWindowOpenHandler = sinon.stub()
this.win.webContents.userAgent = DEFAULT_USER_AGENT
this.win.isDestroyed = sinon.stub().returns(false)
})
Expand Down