test: move the last of the browser window specs (#19295)
* move devtools extensions tests * move executeJavaScript tests * move previewFile test * move contextIsolation tests * move OSR tests * RIP api-browser-window-spec.js .--. .-, .-..-.__ .'(`.-` \_.-'-./` |\_( "\__ __.>\ '; _;---,._| / __/`'--) /.--. : |/' _.--.<| / | | _..-' `\ /' /` /_/ _/_/ >_.-``-. `Y /' _;---.`|/)))) '` .-''. \|: .' __, .-'"` .'--._ `-: \/: /' '.\ _|_ /.'`\ :; /' `- `-|-` -` | | | :.; : | .-'~^~`-. |: | .' _ _ `. |:. | | |_) | |_) | :. : | | | \ | | | : ; | | | : ; | | Here lies | : ; | | 1000 | : ; | | flaky | : ; | | tests | .jgs. : ; | | -."-/\\\/:::. `\."-._'."-"_\\-| |///."- " -."-.\\"-."//.-".`-."_\\-.".-\\`=.........=`//-". * remove unused ipcMain listeners * remove debugging logs * close windows in offscreen test * more closeAllWindows * remove extra logs * refactor webContents main spec using closeAllWindows
This commit is contained in:
parent
16011cacef
commit
2a5d40617a
4 changed files with 600 additions and 755 deletions
|
@ -1,9 +1,11 @@
|
|||
import * as chai from 'chai'
|
||||
import { AddressInfo } from 'net'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
import * as path from 'path'
|
||||
import * as http from 'http'
|
||||
import { BrowserWindow, webContents } from 'electron'
|
||||
import { emittedOnce } from './events-helpers';
|
||||
import { closeWindow } from './window-helpers';
|
||||
import { closeAllWindows } from './window-helpers';
|
||||
|
||||
const { expect } = chai
|
||||
|
||||
|
@ -12,28 +14,13 @@ chai.use(chaiAsPromised)
|
|||
const fixturesPath = path.resolve(__dirname, '..', 'spec', 'fixtures')
|
||||
|
||||
describe('webContents module', () => {
|
||||
let w: BrowserWindow = (null as unknown as BrowserWindow)
|
||||
|
||||
beforeEach(() => {
|
||||
w = new BrowserWindow({
|
||||
show: false,
|
||||
width: 400,
|
||||
height: 400,
|
||||
webPreferences: {
|
||||
backgroundThrottling: false,
|
||||
nodeIntegration: true,
|
||||
webviewTag: true
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await closeWindow(w)
|
||||
w = (null as unknown as BrowserWindow)
|
||||
})
|
||||
|
||||
describe('getAllWebContents() API', () => {
|
||||
afterEach(closeAllWindows)
|
||||
it('returns an array of web contents', async () => {
|
||||
const w = new BrowserWindow({
|
||||
show: false,
|
||||
webPreferences: { webviewTag: true }
|
||||
})
|
||||
w.loadFile(path.join(fixturesPath, 'pages', 'webview-zoom-factor.html'))
|
||||
|
||||
await emittedOnce(w.webContents, 'did-attach-webview')
|
||||
|
@ -54,7 +41,9 @@ describe('webContents module', () => {
|
|||
})
|
||||
|
||||
describe('will-prevent-unload event', () => {
|
||||
afterEach(closeAllWindows)
|
||||
it('does not emit if beforeunload returns undefined', (done) => {
|
||||
const w = new BrowserWindow({show: false})
|
||||
w.once('closed', () => done())
|
||||
w.webContents.once('will-prevent-unload', (e) => {
|
||||
expect.fail('should not have fired')
|
||||
|
@ -63,14 +52,131 @@ describe('webContents module', () => {
|
|||
})
|
||||
|
||||
it('emits if beforeunload returns false', (done) => {
|
||||
const w = new BrowserWindow({show: false})
|
||||
w.webContents.once('will-prevent-unload', () => done())
|
||||
w.loadFile(path.join(fixturesPath, 'api', 'close-beforeunload-false.html'))
|
||||
})
|
||||
|
||||
it('supports calling preventDefault on will-prevent-unload events', (done) => {
|
||||
const w = new BrowserWindow({show: false})
|
||||
w.webContents.once('will-prevent-unload', event => event.preventDefault())
|
||||
w.once('closed', () => done())
|
||||
w.loadFile(path.join(fixturesPath, 'api', 'close-beforeunload-false.html'))
|
||||
})
|
||||
})
|
||||
|
||||
describe('webContents.send(channel, args...)', () => {
|
||||
afterEach(closeAllWindows)
|
||||
it('throws an error when the channel is missing', () => {
|
||||
const w = new BrowserWindow({show: false})
|
||||
expect(() => {
|
||||
(w.webContents.send as any)()
|
||||
}).to.throw('Missing required channel argument')
|
||||
|
||||
expect(() => {
|
||||
w.webContents.send(null as any)
|
||||
}).to.throw('Missing required channel argument')
|
||||
})
|
||||
})
|
||||
|
||||
describe('webContents.executeJavaScript', () => {
|
||||
describe('in about:blank', () => {
|
||||
const expected = 'hello, world!'
|
||||
const expectedErrorMsg = 'woops!'
|
||||
const code = `(() => "${expected}")()`
|
||||
const asyncCode = `(() => new Promise(r => setTimeout(() => r("${expected}"), 500)))()`
|
||||
const badAsyncCode = `(() => new Promise((r, e) => setTimeout(() => e("${expectedErrorMsg}"), 500)))()`
|
||||
const errorTypes = new Set([
|
||||
Error,
|
||||
ReferenceError,
|
||||
EvalError,
|
||||
RangeError,
|
||||
SyntaxError,
|
||||
TypeError,
|
||||
URIError
|
||||
])
|
||||
let w: BrowserWindow
|
||||
|
||||
before(async () => {
|
||||
w = new BrowserWindow({show: false})
|
||||
await w.loadURL('about:blank')
|
||||
})
|
||||
after(closeAllWindows)
|
||||
|
||||
it('resolves the returned promise with the result', async () => {
|
||||
const result = await w.webContents.executeJavaScript(code)
|
||||
expect(result).to.equal(expected)
|
||||
})
|
||||
it('resolves the returned promise with the result if the code returns an asyncronous promise', async () => {
|
||||
const result = await w.webContents.executeJavaScript(asyncCode)
|
||||
expect(result).to.equal(expected)
|
||||
})
|
||||
it('rejects the returned promise if an async error is thrown', async () => {
|
||||
await expect(w.webContents.executeJavaScript(badAsyncCode)).to.eventually.be.rejectedWith(expectedErrorMsg)
|
||||
})
|
||||
it('rejects the returned promise with an error if an Error.prototype is thrown', async () => {
|
||||
for (const error of errorTypes) {
|
||||
await expect(w.webContents.executeJavaScript(`Promise.reject(new ${error.name}("Wamp-wamp"))`))
|
||||
.to.eventually.be.rejectedWith(error)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe("on a real page", () => {
|
||||
let w: BrowserWindow
|
||||
beforeEach(() => {
|
||||
w = new BrowserWindow({show: false})
|
||||
})
|
||||
afterEach(closeAllWindows)
|
||||
|
||||
let server: http.Server = null as unknown as http.Server
|
||||
let serverUrl: string = null as unknown as string
|
||||
|
||||
before((done) => {
|
||||
server = http.createServer((request, response) => {
|
||||
response.end()
|
||||
}).listen(0, '127.0.0.1', () => {
|
||||
serverUrl = 'http://127.0.0.1:' + (server.address() as AddressInfo).port
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
after(() => {
|
||||
server.close()
|
||||
})
|
||||
|
||||
it('works after page load and during subframe load', (done) => {
|
||||
w.webContents.once('did-finish-load', () => {
|
||||
// initiate a sub-frame load, then try and execute script during it
|
||||
w.webContents.executeJavaScript(`
|
||||
var iframe = document.createElement('iframe')
|
||||
iframe.src = '${serverUrl}/slow'
|
||||
document.body.appendChild(iframe)
|
||||
`).then(() => {
|
||||
w.webContents.executeJavaScript('console.log(\'hello\')').then(() => {
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
w.loadURL(serverUrl)
|
||||
})
|
||||
|
||||
it('executes after page load', (done) => {
|
||||
w.webContents.executeJavaScript(`(() => "test")()`).then(result => {
|
||||
expect(result).to.equal("test")
|
||||
done()
|
||||
})
|
||||
w.loadURL(serverUrl)
|
||||
})
|
||||
|
||||
it('works with result objects that have DOM class prototypes', (done) => {
|
||||
w.webContents.executeJavaScript('document.location').then(result => {
|
||||
expect(result.origin).to.equal(serverUrl)
|
||||
expect(result.protocol).to.equal('http:')
|
||||
done()
|
||||
})
|
||||
w.loadURL(serverUrl)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue