2019-04-02 01:28:11 +00:00
|
|
|
import * as chai from 'chai'
|
2019-07-26 14:09:33 +00:00
|
|
|
import { AddressInfo } from 'net'
|
2019-04-02 01:28:11 +00:00
|
|
|
import * as chaiAsPromised from 'chai-as-promised'
|
|
|
|
import * as path from 'path'
|
2019-07-26 14:09:33 +00:00
|
|
|
import * as http from 'http'
|
2019-08-12 17:38:41 +00:00
|
|
|
import { BrowserWindow, ipcMain, webContents } from 'electron'
|
2019-04-02 01:28:11 +00:00
|
|
|
import { emittedOnce } from './events-helpers';
|
2019-07-26 14:09:33 +00:00
|
|
|
import { closeAllWindows } from './window-helpers';
|
2019-04-02 01:28:11 +00:00
|
|
|
|
|
|
|
const { expect } = chai
|
|
|
|
|
|
|
|
chai.use(chaiAsPromised)
|
|
|
|
|
2019-05-29 20:38:14 +00:00
|
|
|
const fixturesPath = path.resolve(__dirname, '..', 'spec', 'fixtures')
|
2019-04-02 01:28:11 +00:00
|
|
|
|
|
|
|
describe('webContents module', () => {
|
|
|
|
describe('getAllWebContents() API', () => {
|
2019-07-26 14:09:33 +00:00
|
|
|
afterEach(closeAllWindows)
|
2019-04-02 01:28:11 +00:00
|
|
|
it('returns an array of web contents', async () => {
|
2019-07-26 14:09:33 +00:00
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: { webviewTag: true }
|
|
|
|
})
|
2019-04-02 01:28:11 +00:00
|
|
|
w.loadFile(path.join(fixturesPath, 'pages', 'webview-zoom-factor.html'))
|
|
|
|
|
|
|
|
await emittedOnce(w.webContents, 'did-attach-webview')
|
|
|
|
|
|
|
|
w.webContents.openDevTools()
|
|
|
|
|
|
|
|
await emittedOnce(w.webContents, 'devtools-opened')
|
|
|
|
|
|
|
|
const all = webContents.getAllWebContents().sort((a, b) => {
|
|
|
|
return a.id - b.id
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(all).to.have.length(3)
|
|
|
|
expect(all[0].getType()).to.equal('window')
|
|
|
|
expect(all[all.length - 2].getType()).to.equal('webview')
|
|
|
|
expect(all[all.length - 1].getType()).to.equal('remote')
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 20:38:14 +00:00
|
|
|
|
|
|
|
describe('will-prevent-unload event', () => {
|
2019-07-26 14:09:33 +00:00
|
|
|
afterEach(closeAllWindows)
|
2019-05-29 20:38:14 +00:00
|
|
|
it('does not emit if beforeunload returns undefined', (done) => {
|
2019-07-26 14:09:33 +00:00
|
|
|
const w = new BrowserWindow({show: false})
|
2019-05-29 20:38:14 +00:00
|
|
|
w.once('closed', () => done())
|
|
|
|
w.webContents.once('will-prevent-unload', (e) => {
|
|
|
|
expect.fail('should not have fired')
|
|
|
|
})
|
|
|
|
w.loadFile(path.join(fixturesPath, 'api', 'close-beforeunload-undefined.html'))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('emits if beforeunload returns false', (done) => {
|
2019-07-26 14:09:33 +00:00
|
|
|
const w = new BrowserWindow({show: false})
|
2019-05-29 20:38:14 +00:00
|
|
|
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) => {
|
2019-07-26 14:09:33 +00:00
|
|
|
const w = new BrowserWindow({show: false})
|
2019-05-29 20:38:14 +00:00
|
|
|
w.webContents.once('will-prevent-unload', event => event.preventDefault())
|
|
|
|
w.once('closed', () => done())
|
|
|
|
w.loadFile(path.join(fixturesPath, 'api', 'close-beforeunload-false.html'))
|
|
|
|
})
|
|
|
|
})
|
2019-07-26 14:09:33 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
})
|
2019-08-12 17:38:41 +00:00
|
|
|
|
|
|
|
it('does not block node async APIs when sent before document is ready', (done) => {
|
|
|
|
// Please reference https://github.com/electron/electron/issues/19368 if
|
|
|
|
// this test fails.
|
|
|
|
ipcMain.once('async-node-api-done', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
|
|
|
sandbox: false,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
w.loadFile(path.join(fixturesPath, 'pages', 'send-after-node.html'))
|
|
|
|
setTimeout(() => {
|
|
|
|
w.webContents.send("test")
|
|
|
|
}, 50)
|
|
|
|
})
|
2019-07-26 14:09:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-04-02 01:28:11 +00:00
|
|
|
})
|