123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809 |
- const assert = require('assert')
- const {expect} = require('chai')
- const path = require('path')
- const http = require('http')
- const url = require('url')
- const {ipcRenderer, remote} = require('electron')
- const {app, session, getGuestWebContents, ipcMain, BrowserWindow, webContents} = remote
- const {closeWindow} = require('./window-helpers')
- const isCI = remote.getGlobal('isCi')
- const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled')
- /* Most of the APIs here don't use standard callbacks */
- /* eslint-disable standard/no-callback-literal */
- describe('<webview> tag', function () {
- this.timeout(3 * 60 * 1000)
- const fixtures = path.join(__dirname, 'fixtures')
- let webview = null
- let w = null
- const openTheWindow = async (...args) => {
- await closeTheWindow()
- w = new BrowserWindow(...args)
- return w
- }
- const closeTheWindow = async () => {
- await closeWindow(w)
- w = null
- }
- const waitForEvent = (eventTarget, eventName) => {
- return new Promise((resolve) => {
- eventTarget.addEventListener(eventName, resolve, {once: true})
- })
- }
- const waitForOnce = (eventTarget, eventName) => {
- return new Promise((resolve) => {
- eventTarget.once(eventName, (...args) => resolve(args))
- })
- }
- const loadWebView = (webview, attributes = {}) => {
- for (const [name, value] of Object.entries(attributes)) {
- webview.setAttribute(name, value)
- }
- document.body.appendChild(webview)
- return waitForEvent(webview, 'did-finish-load')
- }
- const startLoadingWebViewAndWaitForMessage = (webview, attributes = {}) => {
- loadWebView(webview, attributes) // Don't wait for load to be finished.
- return waitForEvent(webview, 'console-message')
- }
- beforeEach(() => {
- webview = new WebView()
- })
- afterEach(() => {
- if (!document.body.contains(webview)) {
- document.body.appendChild(webview)
- }
- webview.remove()
- return closeTheWindow()
- })
- it('works without script tag in page', async () => {
- const w = await openTheWindow({show: false})
- w.loadURL('file://' + fixtures + '/pages/webview-no-script.html')
- await waitForOnce(ipcMain, 'pong')
- })
- it('is disabled when nodeIntegration is disabled', async () => {
- const w = await openTheWindow({
- show: false,
- webPreferences: {
- nodeIntegration: false,
- preload: path.join(fixtures, 'module', 'preload-webview.js')
- }
- })
- w.loadURL(`file://${fixtures}/pages/webview-no-script.html`)
- const [, type] = await waitForOnce(ipcMain, 'webview')
- expect(type).to.equal('undefined', 'WebView still exists')
- })
- it('is enabled when the webviewTag option is enabled and the nodeIntegration option is disabled', async () => {
- const w = await openTheWindow({
- show: false,
- webPreferences: {
- nodeIntegration: false,
- preload: path.join(fixtures, 'module', 'preload-webview.js'),
- webviewTag: true
- }
- })
- w.loadURL(`file://${fixtures}/pages/webview-no-script.html`)
- const [, type] = await waitForOnce(ipcMain, 'webview')
- expect(type).to.not.equal('undefined', 'WebView is not created')
- })
- describe('src attribute', () => {
- it('specifies the page to load', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- src: `file://${fixtures}/pages/a.html`
- })
- expect(message).to.equal('a')
- })
- it('navigates to new page when changed', async () => {
- await loadWebView(webview, {
- src: `file://${fixtures}/pages/a.html`
- })
- webview.src = `file://${fixtures}/pages/b.html`
- const {message} = await waitForEvent(webview, 'console-message')
- expect(message).to.equal('b')
- })
- it('resolves relative URLs', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- src: '../fixtures/pages/e.html'
- })
- assert.equal(message, 'Window script is loaded before preload script')
- })
- it('ignores empty values', () => {
- assert.equal(webview.src, '')
- for (const emptyValue of ['', null, undefined]) {
- webview.src = emptyValue
- expect(webview.src).to.equal('')
- }
- })
- })
- describe('nodeintegration attribute', () => {
- it('inserts no node symbols when not set', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- src: `file://${fixtures}/pages/c.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- require: 'undefined',
- module: 'undefined',
- process: 'undefined',
- global: 'undefined'
- })
- })
- it('inserts node symbols when set', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- nodeintegration: 'on',
- src: `file://${fixtures}/pages/d.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- require: 'function',
- module: 'object',
- process: 'object'
- })
- })
- it('loads node symbols after POST navigation when set', async function () {
- // FIXME Figure out why this is timing out on AppVeyor
- if (process.env.APPVEYOR === 'True') {
- this.skip()
- return
- }
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- nodeintegration: 'on',
- src: `file://${fixtures}/pages/post.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- require: 'function',
- module: 'object',
- process: 'object'
- })
- })
- it('disables node integration on child windows when it is disabled on the webview', (done) => {
- app.once('browser-window-created', (event, window) => {
- assert.equal(window.webContents.getWebPreferences().nodeIntegration, false)
- done()
- })
- const src = url.format({
- pathname: `${fixtures}/pages/webview-opener-no-node-integration.html`,
- protocol: 'file',
- query: {
- p: `${fixtures}/pages/window-opener-node.html`
- },
- slashes: true
- })
- loadWebView(webview, {
- allowpopups: 'on',
- src
- })
- })
- it('loads native modules when navigation happens', async function () {
- if (!nativeModulesEnabled) {
- this.skip()
- return
- }
- await loadWebView(webview, {
- nodeintegration: 'on',
- src: `file://${fixtures}/pages/native-module.html`
- })
- webview.reload()
- const {message} = await waitForEvent(webview, 'console-message')
- assert.equal(message, 'function')
- })
- })
- describe('preload attribute', () => {
- it('loads the script before other scripts in window', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- preload: `${fixtures}/module/preload.js`,
- src: `file://${fixtures}/pages/e.html`
- })
- expect(message).to.be.a('string')
- expect(message).to.be.not.equal('Window script is loaded before preload script')
- })
- it('preload script can still use "process" and "Buffer" when nodeintegration is off', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- preload: `${fixtures}/module/preload-node-off.js`,
- src: `file://${fixtures}/api/blank.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- process: 'object',
- Buffer: 'function'
- })
- })
- it('preload script can require modules that still use "process" and "Buffer" when nodeintegration is off', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- preload: `${fixtures}/module/preload-node-off-wrapper.js`,
- src: `file://${fixtures}/api/blank.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- process: 'object',
- Buffer: 'function'
- })
- })
- it('receives ipc message in preload script', async () => {
- await loadWebView(webview, {
- preload: `${fixtures}/module/preload-ipc.js`,
- src: `file://${fixtures}/pages/e.html`
- })
- const message = 'boom!'
- webview.send('ping', message)
- const {channel, args} = await waitForEvent(webview, 'ipc-message')
- assert.equal(channel, 'pong')
- assert.deepEqual(args, [message])
- })
- it('works without script tag in page', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- preload: `${fixtures}/module/preload.js`,
- src: `file://${fixtures}pages/base-page.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- require: 'function',
- module: 'object',
- process: 'object',
- Buffer: 'function'
- })
- })
- it('resolves relative URLs', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- preload: '../fixtures/module/preload.js',
- src: `file://${fixtures}/pages/e.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- require: 'function',
- module: 'object',
- process: 'object',
- Buffer: 'function'
- })
- })
- it('ignores empty values', () => {
- assert.equal(webview.preload, '')
- for (const emptyValue of ['', null, undefined]) {
- webview.preload = emptyValue
- assert.equal(webview.preload, '')
- }
- })
- })
- describe('httpreferrer attribute', () => {
- it('sets the referrer url', (done) => {
- const referrer = 'http://github.com/'
- const server = http.createServer((req, res) => {
- res.end()
- server.close()
- assert.equal(req.headers.referer, referrer)
- done()
- }).listen(0, '127.0.0.1', () => {
- const port = server.address().port
- loadWebView(webview, {
- httpreferrer: referrer,
- src: `http://127.0.0.1:${port}`
- })
- })
- })
- })
- describe('useragent attribute', () => {
- it('sets the user agent', async () => {
- const referrer = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko'
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- src: `file://${fixtures}/pages/useragent.html`,
- useragent: referrer
- })
- expect(message).to.equal(referrer)
- })
- })
- describe('disablewebsecurity attribute', () => {
- it('does not disable web security when not set', async () => {
- const jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js')
- const src = `<script src='file://${jqueryPath}'></script> <script>console.log('ok');</script>`
- const encoded = btoa(unescape(encodeURIComponent(src)))
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- src: `data:text/html;base64,${encoded}`
- })
- expect(message).to.be.a('string')
- expect(message).to.contain('Not allowed to load local resource')
- })
- it('disables web security when set', async () => {
- const jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js')
- const src = `<script src='file://${jqueryPath}'></script> <script>console.log('ok');</script>`
- const encoded = btoa(unescape(encodeURIComponent(src)))
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- disablewebsecurity: '',
- src: `data:text/html;base64,${encoded}`
- })
- expect(message).to.equal('ok')
- })
- it('does not break node integration', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- disablewebsecurity: '',
- nodeintegration: 'on',
- src: `file://${fixtures}/pages/d.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- require: 'function',
- module: 'object',
- process: 'object'
- })
- })
- it('does not break preload script', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- disablewebsecurity: '',
- preload: `${fixtures}/module/preload.js`,
- src: `file://${fixtures}/pages/e.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- require: 'function',
- module: 'object',
- process: 'object',
- Buffer: 'function'
- })
- })
- })
- describe('partition attribute', () => {
- it('inserts no node symbols when not set', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- partition: 'test1',
- src: `file://${fixtures}/pages/c.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- require: 'undefined',
- module: 'undefined',
- process: 'undefined',
- global: 'undefined'
- })
- })
- it('inserts node symbols when set', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- nodeintegration: 'on',
- partition: 'test2',
- src: `file://${fixtures}/pages/d.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- require: 'function',
- module: 'object',
- process: 'object'
- })
- })
- it('isolates storage for different id', async () => {
- window.localStorage.setItem('test', 'one')
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- partition: 'test3',
- src: `file://${fixtures}/pages/partition/one.html`
- })
- const parsedMessage = JSON.parse(message)
- expect(parsedMessage).to.include({
- numberOfEntries: 0,
- testValue: null
- })
- })
- it('uses current session storage when no id is provided', async () => {
- const testValue = 'one'
- window.localStorage.setItem('test', testValue)
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- src: `file://${fixtures}/pages/partition/one.html`
- })
- const parsedMessage = JSON.parse(message)
- expect(parsedMessage).to.include({
- numberOfEntries: 1,
- testValue
- })
- })
- })
- describe('allowpopups attribute', () => {
- it('can not open new window when not set', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- src: `file://${fixtures}/pages/window-open-hide.html`
- })
- expect(message).to.equal('null')
- })
- it('can open new window when set', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- allowpopups: 'on',
- src: `file://${fixtures}/pages/window-open-hide.html`
- })
- expect(message).to.equal('window')
- })
- })
- describe('webpreferences attribute', () => {
- it('can enable nodeintegration', async () => {
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- src: `file://${fixtures}/pages/d.html`,
- webpreferences: 'nodeIntegration'
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- require: 'function',
- module: 'object',
- process: 'object'
- })
- })
- it('can disables web security and enable nodeintegration', async () => {
- const jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js')
- const src = `<script src='file://${jqueryPath}'></script> <script>console.log(typeof require);</script>`
- const encoded = btoa(unescape(encodeURIComponent(src)))
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- src: `data:text/html;base64,${encoded}`,
- webpreferences: 'webSecurity=no, nodeIntegration=yes'
- })
- expect(message).to.equal('function')
- })
- it('can enable context isolation', async () => {
- loadWebView(webview, {
- allowpopups: 'yes',
- preload: path.join(fixtures, 'api', 'isolated-preload.js'),
- src: `file://${fixtures}/api/isolated.html`,
- webpreferences: 'contextIsolation=yes'
- })
- const [, data] = await waitForOnce(ipcMain, 'isolated-world')
- assert.deepEqual(data, {
- preloadContext: {
- preloadProperty: 'number',
- pageProperty: 'undefined',
- typeofRequire: 'function',
- typeofProcess: 'object',
- typeofArrayPush: 'function',
- typeofFunctionApply: 'function'
- },
- pageContext: {
- preloadProperty: 'undefined',
- pageProperty: 'string',
- typeofRequire: 'undefined',
- typeofProcess: 'undefined',
- typeofArrayPush: 'number',
- typeofFunctionApply: 'boolean',
- typeofPreloadExecuteJavaScriptProperty: 'number',
- typeofOpenedWindow: 'object'
- }
- })
- })
- })
- describe('new-window event', () => {
- it('emits when window.open is called', async () => {
- loadWebView(webview, {
- src: `file://${fixtures}/pages/window-open.html`
- })
- const {url, frameName} = await waitForEvent(webview, 'new-window')
- assert.equal(url, 'http://host/')
- assert.equal(frameName, 'host')
- })
- it('emits when link with target is called', async () => {
- loadWebView(webview, {
- src: `file://${fixtures}/pages/target-name.html`
- })
- const {url, frameName} = await waitForEvent(webview, 'new-window')
- assert.equal(url, 'http://host/')
- assert.equal(frameName, 'target')
- })
- })
- describe('ipc-message event', () => {
- it('emits when guest sends a ipc message to browser', async () => {
- loadWebView(webview, {
- nodeintegration: 'on',
- src: `file://${fixtures}/pages/ipc-message.html`
- })
- const {channel, args} = await waitForEvent(webview, 'ipc-message')
- assert.equal(channel, 'channel')
- assert.deepEqual(args, ['arg1', 'arg2'])
- })
- })
- describe('page-title-set event', () => {
- it('emits when title is set', async () => {
- loadWebView(webview, {
- src: `file://${fixtures}/pages/a.html`
- })
- const {title, explicitSet} = await waitForEvent(webview, 'page-title-set')
- assert.equal(title, 'test')
- assert(explicitSet)
- })
- })
- describe('page-favicon-updated event', () => {
- it('emits when favicon urls are received', async () => {
- loadWebView(webview, {
- src: `file://${fixtures}/pages/a.html`
- })
- const {favicons} = await waitForEvent(webview, 'page-favicon-updated')
- assert(favicons)
- assert.equal(favicons.length, 2)
- if (process.platform === 'win32') {
- assert(/^file:\/\/\/[A-Z]:\/favicon.png$/i.test(favicons[0]))
- } else {
- assert.equal(favicons[0], 'file:///favicon.png')
- }
- })
- })
- describe('will-navigate event', () => {
- it('emits when a url that leads to oustide of the page is clicked', async () => {
- loadWebView(webview, {
- src: `file://${fixtures}/pages/webview-will-navigate.html`
- })
- const {url} = await waitForEvent(webview, 'will-navigate')
- assert.equal(url, 'http://host/')
- })
- })
- describe('did-navigate event', () => {
- let p = path.join(fixtures, 'pages', 'webview-will-navigate.html')
- p = p.replace(/\\/g, '/')
- const pageUrl = url.format({
- protocol: 'file',
- slashes: true,
- pathname: p
- })
- it('emits when a url that leads to outside of the page is clicked', async () => {
- loadWebView(webview, {src: pageUrl})
- const {url} = await waitForEvent(webview, 'did-navigate')
- assert.equal(url, pageUrl)
- })
- })
- describe('did-navigate-in-page event', () => {
- it('emits when an anchor link is clicked', async () => {
- let p = path.join(fixtures, 'pages', 'webview-did-navigate-in-page.html')
- p = p.replace(/\\/g, '/')
- const pageUrl = url.format({
- protocol: 'file',
- slashes: true,
- pathname: p
- })
- loadWebView(webview, {src: pageUrl})
- const event = await waitForEvent(webview, 'did-navigate-in-page')
- assert.equal(event.url, `${pageUrl}#test_content`)
- })
- it('emits when window.history.replaceState is called', async () => {
- loadWebView(webview, {
- src: `file://${fixtures}/pages/webview-did-navigate-in-page-with-history.html`
- })
- const {url} = await waitForEvent(webview, 'did-navigate-in-page')
- assert.equal(url, 'http://host/')
- })
- it('emits when window.location.hash is changed', async () => {
- let p = path.join(fixtures, 'pages', 'webview-did-navigate-in-page-with-hash.html')
- p = p.replace(/\\/g, '/')
- const pageUrl = url.format({
- protocol: 'file',
- slashes: true,
- pathname: p
- })
- loadWebView(webview, {src: pageUrl})
- const event = await waitForEvent(webview, 'did-navigate-in-page')
- assert.equal(event.url, `${pageUrl}#test`)
- })
- })
- describe('close event', () => {
- it('should fire when interior page calls window.close', async () => {
- loadWebView(webview, {src: `file://${fixtures}/pages/close.html`})
- await waitForEvent(webview, 'close')
- })
- })
- describe('setDevToolsWebContents() API', () => {
- it('sets webContents of webview as devtools', async () => {
- const webview2 = new WebView()
- loadWebView(webview2)
- await waitForEvent(webview2, 'did-attach')
- // Setup an event handler for further usage.
- const waitForDomReady = waitForEvent(webview2, 'dom-ready')
- loadWebView(webview, {src: 'about:blank'})
- await waitForEvent(webview, 'dom-ready')
- webview.getWebContents().setDevToolsWebContents(webview2.getWebContents())
- webview.getWebContents().openDevTools()
- await waitForDomReady
- // Its WebContents should be a DevTools.
- const devtools = webview2.getWebContents()
- assert.ok(devtools.getURL().startsWith('chrome-devtools://devtools'))
- const name = await new Promise((resolve) => {
- devtools.executeJavaScript('InspectorFrontendHost.constructor.name', (name) => {
- resolve(name)
- })
- })
- document.body.removeChild(webview2)
- expect(name).to.be.equal('InspectorFrontendHostImpl')
- })
- })
- describe('devtools-opened event', () => {
- it('should fire when webview.openDevTools() is called', async () => {
- loadWebView(webview, {
- src: `file://${fixtures}/pages/base-page.html`
- })
- await waitForEvent(webview, 'dom-ready')
- webview.openDevTools()
- await waitForEvent(webview, 'devtools-opened')
- webview.closeDevTools()
- })
- })
- describe('devtools-closed event', () => {
- it('should fire when webview.closeDevTools() is called', async () => {
- loadWebView(webview, {
- src: `file://${fixtures}/pages/base-page.html`
- })
- await waitForEvent(webview, 'dom-ready')
- webview.openDevTools()
- await waitForEvent(webview, 'devtools-opened')
- webview.closeDevTools()
- await waitForEvent(webview, 'devtools-closed')
- })
- })
- describe('devtools-focused event', () => {
- it('should fire when webview.openDevTools() is called', async () => {
- loadWebView(webview, {
- src: `file://${fixtures}/pages/base-page.html`
- })
- const waitForDevToolsFocused = waitForEvent(webview, 'devtools-focused')
- await waitForEvent(webview, 'dom-ready')
- webview.openDevTools()
- await waitForDevToolsFocused
- webview.closeDevTools()
- })
- })
- describe('<webview>.reload()', () => {
- it('should emit beforeunload handler', async () => {
- await loadWebView(webview, {
- nodeintegration: 'on',
- src: `file://${fixtures}/pages/beforeunload-false.html`
- })
- // Event handler has to be added before reload.
- const waitForOnbeforeunload = waitForEvent(webview, 'ipc-message')
- webview.reload()
- const {channel} = await waitForOnbeforeunload
- assert.equal(channel, 'onbeforeunload')
- })
- })
- describe('<webview>.goForward()', () => {
- it('should work after a replaced history entry', (done) => {
- let loadCount = 1
- const listener = (e) => {
- if (loadCount === 1) {
- assert.equal(e.channel, 'history')
- assert.equal(e.args[0], 1)
- assert(!webview.canGoBack())
- assert(!webview.canGoForward())
- } else if (loadCount === 2) {
- assert.equal(e.channel, 'history')
- assert.equal(e.args[0], 2)
- assert(!webview.canGoBack())
- assert(webview.canGoForward())
- webview.removeEventListener('ipc-message', listener)
- }
- }
- const loadListener = () => {
- if (loadCount === 1) {
- webview.src = `file://${fixtures}/pages/base-page.html`
- } else if (loadCount === 2) {
- assert(webview.canGoBack())
- assert(!webview.canGoForward())
- webview.goBack()
- } else if (loadCount === 3) {
- webview.goForward()
- } else if (loadCount === 4) {
- assert(webview.canGoBack())
- assert(!webview.canGoForward())
- webview.removeEventListener('did-finish-load', loadListener)
- done()
- }
- loadCount += 1
- }
- webview.addEventListener('ipc-message', listener)
- webview.addEventListener('did-finish-load', loadListener)
- loadWebView(webview, {
- nodeintegration: 'on',
- src: `file://${fixtures}/pages/history-replace.html`
- })
- })
- })
- describe('<webview>.clearHistory()', () => {
- it('should clear the navigation history', async () => {
- loadWebView(webview, {
- nodeintegration: 'on',
- src: `file://${fixtures}/pages/history.html`
- })
- const event = await waitForEvent(webview, 'ipc-message')
- assert.equal(event.channel, 'history')
- assert.equal(event.args[0], 2)
- assert(webview.canGoBack())
- webview.clearHistory()
- assert(!webview.canGoBack())
- })
- })
- describe('basic auth', () => {
- const auth = require('basic-auth')
- it('should authenticate with correct credentials', (done) => {
- const message = 'Authenticated'
- const server = http.createServer((req, res) => {
- const credentials = auth(req)
- if (credentials.name === 'test' && credentials.pass === 'test') {
- res.end(message)
- } else {
- res.end('failed')
- }
- server.close()
- })
- server.listen(0, '127.0.0.1', () => {
- const port = server.address().port
- webview.addEventListener('ipc-message', (e) => {
- assert.equal(e.channel, message)
- done()
- })
- loadWebView(webview, {
- nodeintegration: 'on',
- src: `file://${fixtures}/pages/basic-auth.html?port=${port}`
- })
- })
- })
- })
- describe('dom-ready event', () => {
- it('emits when document is loaded', (done) => {
- const server = http.createServer(() => {})
- server.listen(0, '127.0.0.1', () => {
- const port = server.address().port
- webview.addEventListener('dom-ready', () => {
- done()
- })
- loadWebView(webview, {
- src: `file://${fixtures}/pages/dom-ready.html?port=${port}`
- })
- })
- })
- it('throws a custom error when an API method is called before the event is emitted', () => {
- const expectedErrorMessage =
- 'Cannot call stop because the webContents is unavailable. ' +
- 'The WebView must be attached to the DOM ' +
- 'and the dom-ready event emitted before this method can be called.'
- expect(() => { webview.stop() }).to.throw(expectedErrorMessage)
- })
- })
- describe('executeJavaScript', () => {
- it('should support user gesture', async () => {
- await loadWebView(webview, {
- src: `file://${fixtures}/pages/fullscreen.html`
- })
- // Event handler has to be added before js execution.
- const waitForEnterHtmlFullScreen = waitForEvent(webview, 'enter-html-full-screen')
- const jsScript = "document.querySelector('video').webkitRequestFullscreen()"
- webview.executeJavaScript(jsScript, true)
- return waitForEnterHtmlFullScreen
- })
- it('can return the result of the executed script', async () => {
- await loadWebView(webview, {
- src: 'about:blank'
- })
- const jsScript = "'4'+2"
- const expectedResult = '42'
- const result = await new Promise((resolve) => {
- webview.executeJavaScript(jsScript, false, (result) => {
- resolve(result)
- })
- })
- assert.equal(result, expectedResult)
- })
- })
- describe('sendInputEvent', () => {
- it('can send keyboard event', async () => {
- loadWebView(webview, {
- nodeintegration: 'on',
- src: `file://${fixtures}/pages/onkeyup.html`
- })
- await waitForEvent(webview, 'dom-ready')
- const waitForIpcMessage = waitForEvent(webview, 'ipc-message')
- webview.sendInputEvent({
- type: 'keyup',
- keyCode: 'c',
- modifiers: ['shift']
- })
- const {channel, args} = await waitForIpcMessage
- assert.equal(channel, 'keyup')
- assert.deepEqual(args, ['C', 'KeyC', 67, true, false])
- })
- it('can send mouse event', async () => {
- loadWebView(webview, {
- nodeintegration: 'on',
- src: `file://${fixtures}/pages/onmouseup.html`
- })
- await waitForEvent(webview, 'dom-ready')
- const waitForIpcMessage = waitForEvent(webview, 'ipc-message')
- webview.sendInputEvent({
- type: 'mouseup',
- modifiers: ['ctrl'],
- x: 10,
- y: 20
- })
- const {channel, args} = await waitForIpcMessage
- assert.equal(channel, 'mouseup')
- assert.deepEqual(args, [10, 20, false, true])
- })
- })
- describe('media-started-playing media-paused events', () => {
- it('emits when audio starts and stops playing', async () => {
- loadWebView(webview, {src: `file://${fixtures}/pages/audio.html`})
- await waitForEvent(webview, 'media-started-playing')
- await waitForEvent(webview, 'media-paused')
- })
- })
- describe('found-in-page event', () => {
- it('emits when a request is made', (done) => {
- let requestId = null
- let activeMatchOrdinal = []
- const listener = (e) => {
- assert.equal(e.result.requestId, requestId)
- assert.equal(e.result.matches, 3)
- activeMatchOrdinal.push(e.result.activeMatchOrdinal)
- if (e.result.activeMatchOrdinal === e.result.matches) {
- assert.deepEqual(activeMatchOrdinal, [1, 2, 3])
- webview.stopFindInPage('clearSelection')
- done()
- } else {
- listener2()
- }
- }
- const listener2 = () => {
- requestId = webview.findInPage('virtual')
- }
- webview.addEventListener('found-in-page', listener)
- webview.addEventListener('did-finish-load', listener2)
- loadWebView(webview, {src: `file://${fixtures}/pages/content.html`})
- // TODO(deepak1556): With https://codereview.chromium.org/2836973002
- // focus of the webContents is required when triggering the api.
- // Remove this workaround after determining the cause for
- // incorrect focus.
- webview.focus()
- })
- })
- describe('did-change-theme-color event', () => {
- it('emits when theme color changes', async () => {
- loadWebView(webview, {
- src: `file://${fixtures}/pages/theme-color.html`
- })
- await waitForEvent(webview, 'did-change-theme-color')
- })
- })
- describe('permission-request event', () => {
- function setUpRequestHandler (webview, requestedPermission, completed) {
- assert.ok(webview.partition)
- const listener = function (webContents, permission, callback) {
- if (webContents.getId() === webview.getId()) {
- // requestMIDIAccess with sysex requests both midi and midiSysex so
- // grant the first midi one and then reject the midiSysex one
- if (requestedPermission === 'midiSysex' && permission === 'midi') {
- return callback(true)
- }
- assert.equal(permission, requestedPermission)
- callback(false)
- if (completed) completed()
- }
- }
- session.fromPartition(webview.partition).setPermissionRequestHandler(listener)
- }
- it('emits when using navigator.getUserMedia api', (done) => {
- if (isCI) return done()
- webview.addEventListener('ipc-message', (e) => {
- assert.equal(e.channel, 'message')
- assert.deepEqual(e.args, ['PermissionDeniedError'])
- done()
- })
- webview.src = `file://${fixtures}/pages/permissions/media.html`
- webview.partition = 'permissionTest'
- webview.setAttribute('nodeintegration', 'on')
- setUpRequestHandler(webview, 'media')
- document.body.appendChild(webview)
- })
- it('emits when using navigator.geolocation api', (done) => {
- webview.addEventListener('ipc-message', (e) => {
- assert.equal(e.channel, 'message')
- assert.deepEqual(e.args, ['User denied Geolocation'])
- done()
- })
- webview.src = `file://${fixtures}/pages/permissions/geolocation.html`
- webview.partition = 'permissionTest'
- webview.setAttribute('nodeintegration', 'on')
- setUpRequestHandler(webview, 'geolocation')
- document.body.appendChild(webview)
- })
- it('emits when using navigator.requestMIDIAccess without sysex api', (done) => {
- webview.addEventListener('ipc-message', (e) => {
- assert.equal(e.channel, 'message')
- assert.deepEqual(e.args, ['SecurityError'])
- done()
- })
- webview.src = `file://${fixtures}/pages/permissions/midi.html`
- webview.partition = 'permissionTest'
- webview.setAttribute('nodeintegration', 'on')
- setUpRequestHandler(webview, 'midi')
- document.body.appendChild(webview)
- })
- it('emits when using navigator.requestMIDIAccess with sysex api', (done) => {
- webview.addEventListener('ipc-message', (e) => {
- assert.equal(e.channel, 'message')
- assert.deepEqual(e.args, ['SecurityError'])
- done()
- })
- webview.src = `file://${fixtures}/pages/permissions/midi-sysex.html`
- webview.partition = 'permissionTest'
- webview.setAttribute('nodeintegration', 'on')
- setUpRequestHandler(webview, 'midiSysex')
- document.body.appendChild(webview)
- })
- it('emits when accessing external protocol', (done) => {
- webview.src = 'magnet:test'
- webview.partition = 'permissionTest'
- setUpRequestHandler(webview, 'openExternal', done)
- document.body.appendChild(webview)
- })
- it('emits when using Notification.requestPermission', (done) => {
- webview.addEventListener('ipc-message', (e) => {
- assert.equal(e.channel, 'message')
- assert.deepEqual(e.args, ['granted'])
- done()
- })
- webview.src = `file://${fixtures}/pages/permissions/notification.html`
- webview.partition = 'permissionTest'
- webview.setAttribute('nodeintegration', 'on')
- session.fromPartition(webview.partition).setPermissionRequestHandler((webContents, permission, callback) => {
- if (webContents.getId() === webview.getId()) {
- assert.equal(permission, 'notifications')
- setTimeout(() => { callback(true) }, 10)
- }
- })
- document.body.appendChild(webview)
- })
- })
- describe('<webview>.getWebContents', () => {
- it('can return the webcontents associated', async () => {
- const src = 'about:blank'
- await loadWebView(webview, {src})
- const webviewContents = webview.getWebContents()
- assert(webviewContents)
- expect(webviewContents.getURL()).to.equal(src)
- })
- })
- describe('did-get-response-details event (deprecated)', () => {
- it('emits for the page and its resources', (done) => {
- // expected {fileName: resourceType} pairs
- const expectedResources = {
- 'did-get-response-details.html': 'mainFrame',
- 'logo.png': 'image'
- }
- let responses = 0
- webview.addEventListener('-did-get-response-details', (event) => {
- responses += 1
- const fileName = event.newURL.slice(event.newURL.lastIndexOf('/') + 1)
- const expectedType = expectedResources[fileName]
- assert(!!expectedType, `Unexpected response details for ${event.newURL}`)
- assert(typeof event.status === 'boolean', 'status should be boolean')
- assert.equal(event.httpResponseCode, 200)
- assert.equal(event.requestMethod, 'GET')
- assert(typeof event.referrer === 'string', 'referrer should be string')
- assert(!!event.headers, 'headers should be present')
- assert(typeof event.headers === 'object', 'headers should be object')
- assert.equal(event.resourceType, expectedType, 'Incorrect resourceType')
- if (responses === Object.keys(expectedResources).length) done()
- })
- webview.src = `file://${path.join(fixtures, 'pages', 'did-get-response-details.html')}`
- document.body.appendChild(webview)
- })
- })
- describe('document.visibilityState/hidden', () => {
- afterEach(() => {
- ipcMain.removeAllListeners('pong')
- })
- it('updates when the window is shown after the ready-to-show event', async () => {
- const w = await openTheWindow({ show: false })
- w.loadURL(`file://${fixtures}/pages/webview-visibilitychange.html`)
- await waitForOnce(w, 'ready-to-show')
- w.show()
- const [, visibilityState, hidden] = await waitForOnce(ipcMain, 'pong')
- assert(!hidden)
- assert.equal(visibilityState, 'visible')
- })
- it('inherits the parent window visibility state and receives visibilitychange events', async () => {
- const w = await openTheWindow({ show: false })
- w.loadURL(`file://${fixtures}/pages/webview-visibilitychange.html`)
- let [, visibilityState, hidden] = await waitForOnce(ipcMain, 'pong')
- assert.equal(visibilityState, 'hidden')
- assert.equal(hidden, true)
- // We have to start waiting for the event
- // before we ask the webContents to resize.
- let getResponse = waitForOnce(ipcMain, 'pong')
- w.webContents.emit('-window-visibility-change', 'visible')
- return getResponse.then(([, visibilityState, hidden]) => {
- assert.equal(visibilityState, 'visible')
- assert.equal(hidden, false)
- })
- })
- })
- describe('will-attach-webview event', () => {
- it('supports changing the web preferences', async () => {
- ipcRenderer.send('disable-node-on-next-will-attach-webview')
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- nodeintegration: 'yes',
- src: `file://${fixtures}/pages/a.html`
- })
- const types = JSON.parse(message)
- expect(types).to.include({
- require: 'undefined',
- module: 'undefined',
- process: 'undefined',
- global: 'undefined'
- })
- })
- it('supports preventing a webview from being created', async () => {
- ipcRenderer.send('prevent-next-will-attach-webview')
- loadWebView(webview, {
- src: `file://${fixtures}/pages/c.html`
- })
- await waitForEvent(webview, 'destroyed')
- })
- it('supports removing the preload script', async () => {
- ipcRenderer.send('disable-preload-on-next-will-attach-webview')
- const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
- nodeintegration: 'yes',
- preload: path.join(fixtures, 'module', 'preload-set-global.js'),
- src: `file://${fixtures}/pages/a.html`
- })
- assert.equal(message, 'undefined')
- })
- })
- describe('did-attach-webview event', () => {
- it('is emitted when a webview has been attached', async () => {
- const w = await openTheWindow({ show: false })
- w.loadURL(`file://${fixtures}/pages/webview-did-attach-event.html`)
- const [, webContents] = await waitForOnce(w.webContents, 'did-attach-webview')
- const [, id] = await waitForOnce(ipcMain, 'webview-dom-ready')
- expect(webContents.id).to.equal(id)
- })
- })
- it('loads devtools extensions registered on the parent window', async () => {
- const w = await openTheWindow({ show: false })
- BrowserWindow.removeDevToolsExtension('foo')
- const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
- BrowserWindow.addDevToolsExtension(extensionPath)
- w.loadURL(`file://${fixtures}/pages/webview-devtools.html`)
- const [, {runtimeId, tabId}] = await waitForOnce(ipcMain, 'answer')
- expect(runtimeId).to.equal('foo')
- expect(tabId).to.be.not.equal(w.webContents.id)
- })
- describe('guestinstance attribute', () => {
- it('before loading there is no attribute', () => {
- loadWebView(webview) // Don't wait for loading to finish.
- assert(!webview.hasAttribute('guestinstance'))
- })
- it('loading a page sets the guest view', async () => {
- await loadWebView(webview, {
- src: `file://${fixtures}/api/blank.html`
- })
- const instance = webview.getAttribute('guestinstance')
- assert.equal(instance, parseInt(instance))
- const guest = getGuestWebContents(parseInt(instance))
- assert.equal(guest, webview.getWebContents())
- })
- it('deleting the attribute destroys the webview', async () => {
- await loadWebView(webview, {
- src: `file://${fixtures}/api/blank.html`
- })
- const instance = parseInt(webview.getAttribute('guestinstance'))
- const waitForDestroy = waitForEvent(webview, 'destroyed')
- webview.removeAttribute('guestinstance')
- await waitForDestroy
- expect(getGuestWebContents(instance)).to.equal(undefined)
- })
- it('setting the attribute on a new webview moves the contents', (done) => {
- const loadListener = () => {
- const webContents = webview.getWebContents()
- const instance = webview.getAttribute('guestinstance')
- const destroyListener = () => {
- assert.equal(webContents, webview2.getWebContents())
- // Make sure that events are hooked up to the right webview now
- webview2.addEventListener('console-message', (e) => {
- assert.equal(e.message, 'a')
- document.body.removeChild(webview2)
- done()
- })
- webview2.src = `file://${fixtures}/pages/a.html`
- }
- webview.addEventListener('destroyed', destroyListener, {once: true})
- const webview2 = new WebView()
- loadWebView(webview2, {
- guestinstance: instance
- })
- }
- webview.addEventListener('did-finish-load', loadListener, {once: true})
- loadWebView(webview, {
- src: `file://${fixtures}/api/blank.html`
- })
- })
- it('setting the attribute to an invalid guestinstance does nothing', async () => {
- await loadWebView(webview, {
- src: `file://${fixtures}/api/blank.html`
- })
- webview.setAttribute('guestinstance', 55)
- // Make sure that events are still hooked up to the webview
- const waitForMessage = waitForEvent(webview, 'console-message')
- webview.src = `file://${fixtures}/pages/a.html`
- const {message} = await waitForMessage
- assert.equal(message, 'a')
- })
- it('setting the attribute on an existing webview moves the contents', (done) => {
- const load1Listener = () => {
- const webContents = webview.getWebContents()
- const instance = webview.getAttribute('guestinstance')
- let destroyedInstance
- const destroyListener = () => {
- assert.equal(webContents, webview2.getWebContents())
- assert.equal(null, getGuestWebContents(parseInt(destroyedInstance)))
- // Make sure that events are hooked up to the right webview now
- webview2.addEventListener('console-message', (e) => {
- assert.equal(e.message, 'a')
- document.body.removeChild(webview2)
- done()
- })
- webview2.src = 'file://' + fixtures + '/pages/a.html'
- }
- webview.addEventListener('destroyed', destroyListener, {once: true})
- const webview2 = new WebView()
- const load2Listener = () => {
- destroyedInstance = webview2.getAttribute('guestinstance')
- assert.notEqual(instance, destroyedInstance)
- webview2.setAttribute('guestinstance', instance)
- }
- webview2.addEventListener('did-finish-load', load2Listener, {once: true})
- loadWebView(webview2, {
- src: `file://${fixtures}/api/blank.html`
- })
- }
- webview.addEventListener('did-finish-load', load1Listener, {once: true})
- loadWebView(webview, {
- src: `file://${fixtures}/api/blank.html`
- })
- })
- it('moving a guest back to its original webview should work', (done) => {
- loadWebView(webview, {
- src: `file://${fixtures}/api/blank.html`
- }).then(() => {
- const webContents = webview.getWebContents()
- const instance = webview.getAttribute('guestinstance')
- const destroy1Listener = () => {
- assert.equal(webContents, webview2.getWebContents())
- assert.equal(null, webview.getWebContents())
- const destroy2Listener = () => {
- assert.equal(webContents, webview.getWebContents())
- assert.equal(null, webview2.getWebContents())
- // Make sure that events are hooked up to the right webview now
- webview.addEventListener('console-message', (e) => {
- document.body.removeChild(webview2)
- assert.equal(e.message, 'a')
- done()
- })
- webview.src = `file://${fixtures}/pages/a.html`
- }
- webview2.addEventListener('destroyed', destroy2Listener, {once: true})
- webview.setAttribute('guestinstance', instance)
- }
- webview.addEventListener('destroyed', destroy1Listener, {once: true})
- const webview2 = new WebView()
- loadWebView(webview2, {guestinstance: instance})
- })
- })
- // FIXME(alexeykuzmin): This test only passes if the previous test ^
- // is run alongside.
- it('setting the attribute on a webview in a different window moves the contents', (done) => {
- loadWebView(webview, {
- src: `file://${fixtures}/api/blank.html`
- }).then(() => {
- const instance = webview.getAttribute('guestinstance')
- w = new BrowserWindow({ show: false })
- w.webContents.once('did-finish-load', () => {
- ipcMain.once('pong', () => {
- assert(!webview.hasAttribute('guestinstance'))
- done()
- })
- w.webContents.send('guestinstance', instance)
- })
- w.loadURL(`file://${fixtures}/pages/webview-move-to-window.html`)
- })
- })
- it('does not delete the guestinstance attribute when moving the webview to another parent node', (done) => {
- webview.addEventListener('dom-ready', function domReadyListener () {
- webview.addEventListener('did-attach', () => {
- assert(webview.guestinstance != null)
- assert(webview.getWebContents() != null)
- done()
- })
- document.body.replaceChild(webview, div)
- })
- webview.src = `file://${fixtures}/pages/a.html`
- const div = document.createElement('div')
- div.appendChild(webview)
- document.body.appendChild(div)
- })
- it('does not destroy the webContents when hiding/showing the webview (regression)', (done) => {
- webview.addEventListener('dom-ready', function () {
- const instance = webview.getAttribute('guestinstance')
- assert(instance != null)
- // Wait for event directly since attach happens asynchronously over IPC
- ipcMain.once('ELECTRON_GUEST_VIEW_MANAGER_ATTACH_GUEST', () => {
- assert(webview.getWebContents() != null)
- assert.equal(instance, webview.getAttribute('guestinstance'))
- done()
- })
- webview.style.display = 'none'
- webview.offsetHeight // eslint-disable-line
- webview.style.display = 'block'
- }, {once: true})
- loadWebView(webview, {src: `file://${fixtures}/pages/a.html`})
- })
- it('does not reload the webContents when hiding/showing the webview (regression)', (done) => {
- webview.addEventListener('dom-ready', function () {
- webview.addEventListener('did-start-loading', () => {
- done(new Error('webview started loading unexpectedly'))
- })
- // Wait for event directly since attach happens asynchronously over IPC
- webview.addEventListener('did-attach', () => {
- done()
- })
- webview.style.display = 'none'
- webview.offsetHeight // eslint-disable-line
- webview.style.display = 'block'
- }, {once: true})
- loadWebView(webview, {src: `file://${fixtures}/pages/a.html`})
- })
- })
- describe('DOM events', () => {
- let div
- beforeEach(() => {
- div = document.createElement('div')
- div.style.width = '100px'
- div.style.height = '10px'
- div.style.overflow = 'hidden'
- webview.style.height = '100%'
- webview.style.width = '100%'
- })
- afterEach(() => {
- if (div != null) div.remove()
- })
- it('emits resize events', (done) => {
- webview.addEventListener('dom-ready', () => {
- div.style.width = '1234px'
- div.style.height = '789px'
- })
- webview.addEventListener('resize', function onResize (event) {
- webview.removeEventListener('resize', onResize)
- assert.equal(event.newWidth, 100)
- assert.equal(event.newHeight, 10)
- assert.equal(event.target, webview)
- webview.addEventListener('resize', function onResizeAgain (event) {
- // This will be triggered after setting the new div width and height.
- webview.removeEventListener('resize', onResizeAgain)
- assert.equal(event.newWidth, 1234)
- assert.equal(event.newHeight, 789)
- assert.equal(event.target, webview)
- done()
- })
- })
- webview.src = `file://${fixtures}/pages/a.html`
- div.appendChild(webview)
- document.body.appendChild(div)
- })
- })
- describe('disableguestresize attribute', () => {
- it('does not have attribute by default', () => {
- loadWebView(webview)
- assert(!webview.hasAttribute('disableguestresize'))
- })
- it('resizes guest when attribute is not present', async () => {
- const INITIAL_SIZE = 200
- const w = await openTheWindow(
- {show: false, width: INITIAL_SIZE, height: INITIAL_SIZE})
- w.loadURL(`file://${fixtures}/pages/webview-guest-resize.html`)
- await waitForOnce(ipcMain, 'webview-loaded')
- const elementResize = waitForOnce(ipcMain, 'webview-element-resize')
- .then(([, width, height]) => {
- assert.equal(width, CONTENT_SIZE)
- assert.equal(height, CONTENT_SIZE)
- })
- const guestResize = waitForOnce(ipcMain, 'webview-guest-resize')
- .then(([, width, height]) => {
- assert.equal(width, CONTENT_SIZE)
- assert.equal(height, CONTENT_SIZE)
- })
- const CONTENT_SIZE = 300
- assert(CONTENT_SIZE !== INITIAL_SIZE)
- w.setContentSize(CONTENT_SIZE, CONTENT_SIZE)
- return Promise.all([elementResize, guestResize])
- })
- it('does not resize guest when attribute is present', async () => {
- const INITIAL_SIZE = 200
- const w = await openTheWindow(
- {show: false, width: INITIAL_SIZE, height: INITIAL_SIZE})
- w.loadURL(`file://${fixtures}/pages/webview-no-guest-resize.html`)
- await waitForOnce(ipcMain, 'webview-loaded')
- const noGuestResizePromise = Promise.race([
- waitForOnce(ipcMain, 'webview-guest-resize'),
- new Promise(resolve => setTimeout(() => resolve(), 500))
- ]).then((eventData = null) => {
- if (eventData !== null) {
- // Means we got the 'webview-guest-resize' event before the time out.
- return Promise.reject(new Error('Unexpected guest resize message'))
- }
- })
- const CONTENT_SIZE = 300
- assert(CONTENT_SIZE !== INITIAL_SIZE)
- w.setContentSize(CONTENT_SIZE, CONTENT_SIZE)
- return noGuestResizePromise
- })
- it('dispatches element resize event even when attribute is present', async () => {
- const INITIAL_SIZE = 200
- const w = await openTheWindow(
- {show: false, width: INITIAL_SIZE, height: INITIAL_SIZE})
- w.loadURL(`file://${fixtures}/pages/webview-no-guest-resize.html`)
- await waitForOnce(ipcMain, 'webview-loaded')
- const elementResizePromise = waitForOnce(ipcMain, 'webview-element-resize')
- .then(([, width, height]) => {
- expect(width).to.equal(CONTENT_SIZE)
- expect(height).to.equal(CONTENT_SIZE)
- })
- const CONTENT_SIZE = 300
- assert(CONTENT_SIZE !== INITIAL_SIZE)
- w.setContentSize(CONTENT_SIZE, CONTENT_SIZE)
- return elementResizePromise
- })
- it('can be manually resized with setSize even when attribute is present', async () => {
- const INITIAL_SIZE = 200
- const w = await openTheWindow(
- {show: false, width: INITIAL_SIZE, height: INITIAL_SIZE})
- w.loadURL(`file://${fixtures}/pages/webview-no-guest-resize.html`)
- await waitForOnce(ipcMain, 'webview-loaded')
- const GUEST_WIDTH = 10
- const GUEST_HEIGHT = 20
- const guestResizePromise = waitForOnce(ipcMain, 'webview-guest-resize')
- .then(([, width, height]) => {
- expect(width).to.be.equal(GUEST_WIDTH)
- expect(height).to.be.equal(GUEST_HEIGHT)
- })
- const wc = webContents.getAllWebContents().find((wc) => {
- return wc.hostWebContents &&
- wc.hostWebContents.id === w.webContents.id
- })
- assert(wc)
- wc.setSize({
- normal: {
- width: GUEST_WIDTH,
- height: GUEST_HEIGHT
- }
- })
- return guestResizePromise
- })
- })
- describe('zoom behavior', () => {
- const zoomScheme = remote.getGlobal('zoomScheme')
- const webviewSession = session.fromPartition('webview-temp')
- before((done) => {
- const protocol = webviewSession.protocol
- protocol.registerStringProtocol(zoomScheme, (request, callback) => {
- callback('hello')
- }, (error) => done(error))
- })
- after((done) => {
- const protocol = webviewSession.protocol
- protocol.unregisterProtocol(zoomScheme, (error) => done(error))
- })
- it('inherits the zoomFactor of the parent window', async () => {
- const w = await openTheWindow({
- show: false,
- webPreferences: {
- zoomFactor: 1.2
- }
- })
- w.loadURL(`file://${fixtures}/pages/webview-zoom-factor.html`)
- const [, zoomFactor, zoomLevel] = await waitForOnce(ipcMain, 'webview-parent-zoom-level')
- expect(zoomFactor).to.equal(1.2)
- expect(zoomLevel).to.equal(1)
- })
- it('maintains zoom level on navigation', async () => {
- return openTheWindow({
- show: false,
- webPreferences: {
- zoomFactor: 1.2
- }
- }).then((w) => {
- const promise = new Promise((resolve) => {
- ipcMain.on('webview-zoom-level', (event, zoomLevel, zoomFactor, newHost, final) => {
- if (!newHost) {
- expect(zoomFactor).to.equal(1.44)
- expect(zoomLevel).to.equal(2.0)
- } else {
- expect(zoomFactor).to.equal(1.2)
- expect(zoomLevel).to.equal(1)
- }
- if (final) {
- resolve()
- }
- })
- })
- w.loadURL(`file://${fixtures}/pages/webview-custom-zoom-level.html`)
- return promise
- })
- })
- it('maintains zoom level when navigating within same page', async () => {
- return openTheWindow({
- show: false,
- webPreferences: {
- zoomFactor: 1.2
- }
- }).then((w) => {
- const promise = new Promise((resolve) => {
- ipcMain.on('webview-zoom-in-page', (event, zoomLevel, zoomFactor, final) => {
- expect(zoomFactor).to.equal(1.44)
- expect(zoomLevel).to.equal(2.0)
- if (final) {
- resolve()
- }
- })
- })
- w.loadURL(`file://${fixtures}/pages/webview-in-page-navigate.html`)
- return promise
- })
- })
- it('inherits zoom level for the origin when available', async () => {
- const w = await openTheWindow({
- show: false,
- webPreferences: {
- zoomFactor: 1.2
- }
- })
- w.loadURL(`file://${fixtures}/pages/webview-origin-zoom-level.html`)
- const [, zoomLevel] = await waitForOnce(ipcMain, 'webview-origin-zoom-level')
- expect(zoomLevel).to.equal(2.0)
- })
- })
- describe('nativeWindowOpen option', () => {
- beforeEach(() => {
- webview.setAttribute('allowpopups', 'on')
- webview.setAttribute('nodeintegration', 'on')
- webview.setAttribute('webpreferences', 'nativeWindowOpen=1')
- })
- it('opens window of about:blank with cross-scripting enabled', async () => {
- // Don't wait for loading to finish.
- loadWebView(webview, {
- src: `file://${path.join(fixtures, 'api', 'native-window-open-blank.html')}`
- })
- const [, content] = await waitForOnce(ipcMain, 'answer')
- expect(content).to.equal('Hello')
- })
- it('opens window of same domain with cross-scripting enabled', async () => {
- // Don't wait for loading to finish.
- loadWebView(webview, {
- src: `file://${path.join(fixtures, 'api', 'native-window-open-file.html')}`
- })
- const [, content] = await waitForOnce(ipcMain, 'answer')
- expect(content).to.equal('Hello')
- })
- it('returns null from window.open when allowpopups is not set', async () => {
- webview.removeAttribute('allowpopups')
- // Don't wait for loading to finish.
- loadWebView(webview, {
- src: `file://${path.join(fixtures, 'api', 'native-window-open-no-allowpopups.html')}`
- })
- const [, {windowOpenReturnedNull}] = await waitForOnce(ipcMain, 'answer')
- expect(windowOpenReturnedNull).to.equal(true)
- })
- it('blocks accessing cross-origin frames', async () => {
- // Don't wait for loading to finish.
- loadWebView(webview, {
- src: `file://${path.join(fixtures, 'api', 'native-window-open-cross-origin.html')}`
- })
- const [, content] = await waitForOnce(ipcMain, 'answer')
- const expectedContent =
- 'Blocked a frame with origin "file://" from accessing a cross-origin frame.'
- expect(content).to.equal(expectedContent)
- })
- it('emits a new-window event', async () => {
- // Don't wait for loading to finish.
- loadWebView(webview, {
- src: `file://${fixtures}/pages/window-open.html`
- })
- const {url, frameName} = await waitForEvent(webview, 'new-window')
- expect(url).to.equal('http://host/')
- expect(frameName).to.equal('host')
- })
- it('emits a browser-window-created event', async () => {
- // Don't wait for loading to finish.
- loadWebView(webview, {
- src: `file://${fixtures}/pages/window-open.html`
- })
- await waitForOnce(app, 'browser-window-created')
- })
- it('emits a web-contents-created event', (done) => {
- app.on('web-contents-created', function listener (event, contents) {
- if (contents.getType() === 'window') {
- app.removeListener('web-contents-created', listener)
- done()
- }
- })
- loadWebView(webview, {
- src: `file://${fixtures}/pages/window-open.html`
- })
- })
- })
- })
|