fix: move window.open postMessage test to main runner to fix flake (#18735)
This commit is contained in:
parent
81ad355355
commit
e5d1e7b4da
5 changed files with 35 additions and 22 deletions
|
@ -1,7 +1,8 @@
|
||||||
import * as chai from 'chai'
|
import * as chai from 'chai'
|
||||||
import * as chaiAsPromised from 'chai-as-promised'
|
import * as chaiAsPromised from 'chai-as-promised'
|
||||||
import { BrowserWindow, session } from 'electron'
|
import { BrowserWindow, session, ipcMain } from 'electron'
|
||||||
import { emittedOnce } from './events-helpers';
|
import { emittedOnce } from './events-helpers';
|
||||||
|
import { closeAllWindows } from './window-helpers';
|
||||||
import * as https from 'https';
|
import * as https from 'https';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
|
@ -70,3 +71,19 @@ describe('reporting api', () => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('window.postMessage', () => {
|
||||||
|
afterEach(async () => {
|
||||||
|
await closeAllWindows()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('sets the source and origin correctly', async () => {
|
||||||
|
const w = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true}})
|
||||||
|
w.loadURL(`file://${fixturesPath}/pages/window-open-postMessage-driver.html`)
|
||||||
|
const [, message] = await emittedOnce(ipcMain, 'complete')
|
||||||
|
expect(message.data).to.equal('testing')
|
||||||
|
expect(message.origin).to.equal('file://')
|
||||||
|
expect(message.sourceEqualsOpener).to.equal(true)
|
||||||
|
expect(message.eventOrigin).to.equal('file://')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
|
@ -39,3 +39,9 @@ export const closeWindow = async (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function closeAllWindows() {
|
||||||
|
for (const w of BrowserWindow.getAllWindows()) {
|
||||||
|
await closeWindow(w, {assertNotWindows: false})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -721,27 +721,6 @@ describe('chromium feature', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('window.postMessage', () => {
|
describe('window.postMessage', () => {
|
||||||
it('sets the source and origin correctly', (done) => {
|
|
||||||
let b = null
|
|
||||||
listener = (event) => {
|
|
||||||
window.removeEventListener('message', listener)
|
|
||||||
b.close()
|
|
||||||
const message = JSON.parse(event.data)
|
|
||||||
expect(message.data).to.equal('testing')
|
|
||||||
expect(message.origin).to.equal('file://')
|
|
||||||
expect(message.sourceEqualsOpener).to.be.true()
|
|
||||||
expect(event.origin).to.equal('file://')
|
|
||||||
done()
|
|
||||||
}
|
|
||||||
window.addEventListener('message', listener)
|
|
||||||
app.once('browser-window-created', (event, { webContents }) => {
|
|
||||||
webContents.once('did-finish-load', () => {
|
|
||||||
b.postMessage('testing', '*')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
b = window.open(`file://${fixtures}/pages/window-open-postMessage.html`, '', 'show=no')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws an exception when the targetOrigin cannot be converted to a string', () => {
|
it('throws an exception when the targetOrigin cannot be converted to a string', () => {
|
||||||
const b = window.open('')
|
const b = window.open('')
|
||||||
expect(() => {
|
expect(() => {
|
||||||
|
|
10
spec/fixtures/pages/window-open-postMessage-driver.html
vendored
Normal file
10
spec/fixtures/pages/window-open-postMessage-driver.html
vendored
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<script>
|
||||||
|
const child = window.open(`./window-open-postMessage.html`, '', 'show=no')
|
||||||
|
window.onmessage = (e) => {
|
||||||
|
if (e.data === 'ready') {
|
||||||
|
child.postMessage('testing', '*')
|
||||||
|
} else {
|
||||||
|
require('electron').ipcRenderer.send('complete', {eventOrigin: e.origin, ...JSON.parse(e.data)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
|
@ -8,6 +8,7 @@
|
||||||
sourceEqualsOpener: e.source === window.opener
|
sourceEqualsOpener: e.source === window.opener
|
||||||
}), '*');
|
}), '*');
|
||||||
});
|
});
|
||||||
|
window.opener.postMessage("ready", "*")
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Add table
Reference in a new issue