2023-06-15 14:42:27 +00:00
|
|
|
import * as path from 'node:path';
|
|
|
|
import * as url from 'node:url';
|
2024-02-28 03:54:20 +00:00
|
|
|
import { BrowserWindow, session, ipcMain, app, WebContents } from 'electron/main';
|
2023-01-25 21:01:25 +00:00
|
|
|
import { closeAllWindows } from './lib/window-helpers';
|
2023-02-23 23:53:53 +00:00
|
|
|
import { emittedUntil } from './lib/events-helpers';
|
|
|
|
import { ifit, ifdescribe, defer, itremote, useRemoteContext, listen } from './lib/spec-helpers';
|
2022-08-05 00:20:17 +00:00
|
|
|
import { expect } from 'chai';
|
2023-06-15 14:42:27 +00:00
|
|
|
import * as http from 'node:http';
|
2022-08-15 08:06:02 +00:00
|
|
|
import * as auth from 'basic-auth';
|
2023-06-15 14:42:27 +00:00
|
|
|
import { once } from 'node:events';
|
|
|
|
import { setTimeout } from 'node:timers/promises';
|
2024-07-17 00:16:25 +00:00
|
|
|
import { HexColors, ScreenCapture, hasCapturableScreen } from './lib/screen-helpers';
|
2022-08-03 16:59:00 +00:00
|
|
|
|
|
|
|
declare let WebView: any;
|
2022-08-15 08:06:02 +00:00
|
|
|
const features = process._linkedBinding('electron_common_features');
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2022-08-03 19:03:44 +00:00
|
|
|
async function loadWebView (w: WebContents, attributes: Record<string, string>, opts?: {openDevTools?: boolean}): Promise<void> {
|
|
|
|
const { openDevTools } = {
|
|
|
|
openDevTools: false,
|
|
|
|
...opts
|
|
|
|
};
|
2019-09-03 17:10:58 +00:00
|
|
|
await w.executeJavaScript(`
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const webview = new WebView()
|
2022-08-03 16:59:00 +00:00
|
|
|
webview.id = 'webview'
|
2019-09-03 17:10:58 +00:00
|
|
|
for (const [k, v] of Object.entries(${JSON.stringify(attributes)})) {
|
|
|
|
webview.setAttribute(k, v)
|
|
|
|
}
|
|
|
|
document.body.appendChild(webview)
|
2020-04-13 23:39:26 +00:00
|
|
|
webview.addEventListener('dom-ready', () => {
|
|
|
|
if (${openDevTools}) {
|
|
|
|
webview.openDevTools()
|
|
|
|
}
|
|
|
|
})
|
2019-09-03 17:10:58 +00:00
|
|
|
webview.addEventListener('did-finish-load', () => {
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
`);
|
|
|
|
}
|
2022-08-03 19:03:44 +00:00
|
|
|
async function loadWebViewAndWaitForEvent (w: WebContents, attributes: Record<string, string>, eventName: string): Promise<any> {
|
2022-08-03 16:59:00 +00:00
|
|
|
return await w.executeJavaScript(`new Promise((resolve, reject) => {
|
|
|
|
const webview = new WebView()
|
2022-08-03 19:03:44 +00:00
|
|
|
webview.id = 'webview'
|
2022-08-03 16:59:00 +00:00
|
|
|
for (const [k, v] of Object.entries(${JSON.stringify(attributes)})) {
|
|
|
|
webview.setAttribute(k, v)
|
|
|
|
}
|
2022-08-03 19:03:44 +00:00
|
|
|
webview.addEventListener(${JSON.stringify(eventName)}, (e) => resolve({...e}), {once: true})
|
2022-08-03 16:59:00 +00:00
|
|
|
document.body.appendChild(webview)
|
|
|
|
})`);
|
|
|
|
};
|
2022-08-03 19:03:44 +00:00
|
|
|
async function loadWebViewAndWaitForMessage (w: WebContents, attributes: Record<string, string>): Promise<string> {
|
|
|
|
const { message } = await loadWebViewAndWaitForEvent(w, attributes, 'console-message');
|
|
|
|
return message;
|
|
|
|
};
|
2022-08-03 16:59:00 +00:00
|
|
|
|
2021-04-09 07:09:17 +00:00
|
|
|
describe('<webview> tag', function () {
|
2022-08-16 19:23:13 +00:00
|
|
|
const fixtures = path.join(__dirname, 'fixtures');
|
2022-08-03 16:59:00 +00:00
|
|
|
const blankPageUrl = url.pathToFileURL(path.join(fixtures, 'pages', 'blank.html')).toString();
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-05-21 15:46:17 +00:00
|
|
|
function hideChildWindows (e: any, wc: WebContents) {
|
2022-06-13 17:07:47 +00:00
|
|
|
wc.setWindowOpenHandler(() => ({
|
|
|
|
action: 'allow',
|
|
|
|
overrideBrowserWindowOptions: {
|
|
|
|
show: false
|
|
|
|
}
|
|
|
|
}));
|
2020-05-21 15:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
before(() => {
|
|
|
|
app.on('web-contents-created', hideChildWindows);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
app.off('web-contents-created', hideChildWindows);
|
|
|
|
});
|
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
describe('behavior', () => {
|
|
|
|
afterEach(closeAllWindows);
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
it('works without script tag in page', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-no-script.html'));
|
2023-02-23 23:53:53 +00:00
|
|
|
await once(ipcMain, 'pong');
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
it('works with sandbox', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-isolated.html'));
|
2023-02-23 23:53:53 +00:00
|
|
|
await once(ipcMain, 'pong');
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
it('works with contextIsolation', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
contextIsolation: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-isolated.html'));
|
2023-02-23 23:53:53 +00:00
|
|
|
await once(ipcMain, 'pong');
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
it('works with contextIsolation + sandbox', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
contextIsolation: true,
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-isolated.html'));
|
2023-02-23 23:53:53 +00:00
|
|
|
await once(ipcMain, 'pong');
|
2021-01-25 01:29:25 +00:00
|
|
|
});
|
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
it('works with Trusted Types', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-trusted-types.html'));
|
2023-02-23 23:53:53 +00:00
|
|
|
await once(ipcMain, 'pong');
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
it('is disabled by default', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
preload: path.join(fixtures, 'module', 'preload-webview.js'),
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const webview = once(ipcMain, 'webview');
|
2022-08-03 16:59:00 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-no-script.html'));
|
|
|
|
const [, type] = await webview;
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
expect(type).to.equal('undefined', 'WebView still exists');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// FIXME(deepak1556): Ch69 follow up.
|
|
|
|
xdescribe('document.visibilityState/hidden', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
ipcMain.removeAllListeners('pong');
|
|
|
|
});
|
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
it('updates when the window is shown after the ready-to-show event', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
2023-02-23 23:53:53 +00:00
|
|
|
const readyToShowSignal = once(w, 'ready-to-show');
|
|
|
|
const pongSignal1 = once(ipcMain, 'pong');
|
2019-09-03 17:10:58 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-visibilitychange.html'));
|
|
|
|
await pongSignal1;
|
2023-02-23 23:53:53 +00:00
|
|
|
const pongSignal2 = once(ipcMain, 'pong');
|
2019-09-03 17:10:58 +00:00
|
|
|
await readyToShowSignal;
|
|
|
|
w.show();
|
2020-03-20 20:28:31 +00:00
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
const [, visibilityState, hidden] = await pongSignal2;
|
|
|
|
expect(visibilityState).to.equal('visible');
|
|
|
|
expect(hidden).to.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inherits the parent window visibility state and receives visibilitychange events', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-visibilitychange.html'));
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, visibilityState, hidden] = await once(ipcMain, 'pong');
|
2019-09-03 17:10:58 +00:00
|
|
|
expect(visibilityState).to.equal('hidden');
|
|
|
|
expect(hidden).to.be.true();
|
|
|
|
|
|
|
|
// We have to start waiting for the event
|
|
|
|
// before we ask the webContents to resize.
|
2023-02-23 23:53:53 +00:00
|
|
|
const getResponse = once(ipcMain, 'pong');
|
2019-09-03 17:10:58 +00:00
|
|
|
w.webContents.emit('-window-visibility-change', 'visible');
|
|
|
|
|
|
|
|
return getResponse.then(([, visibilityState, hidden]) => {
|
|
|
|
expect(visibilityState).to.equal('visible');
|
|
|
|
expect(hidden).to.be.false();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('did-attach-webview event', () => {
|
2022-08-03 16:59:00 +00:00
|
|
|
afterEach(closeAllWindows);
|
2019-09-03 17:10:58 +00:00
|
|
|
it('is emitted when a webview has been attached', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
2021-03-01 21:52:29 +00:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
|
|
|
});
|
2023-06-22 18:38:52 +00:00
|
|
|
const didAttachWebview = once(w.webContents, 'did-attach-webview') as Promise<[any, WebContents]>;
|
2023-02-23 23:53:53 +00:00
|
|
|
const webviewDomReady = once(ipcMain, 'webview-dom-ready');
|
2019-09-03 17:10:58 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-did-attach-event.html'));
|
|
|
|
|
|
|
|
const [, webContents] = await didAttachWebview;
|
|
|
|
const [, id] = await webviewDomReady;
|
|
|
|
expect(webContents.id).to.equal(id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-14 11:59:20 +00:00
|
|
|
describe('did-attach event', () => {
|
2022-08-03 16:59:00 +00:00
|
|
|
afterEach(closeAllWindows);
|
2021-07-14 11:59:20 +00:00
|
|
|
it('is emitted when a webview has been attached', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const message = await w.webContents.executeJavaScript(`new Promise((resolve, reject) => {
|
|
|
|
const webview = new WebView()
|
|
|
|
webview.setAttribute('src', 'about:blank')
|
|
|
|
webview.addEventListener('did-attach', (e) => {
|
|
|
|
resolve('ok')
|
|
|
|
})
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})`);
|
|
|
|
expect(message).to.equal('ok');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-16 22:21:40 +00:00
|
|
|
describe('did-change-theme-color event', () => {
|
2022-08-03 16:59:00 +00:00
|
|
|
afterEach(closeAllWindows);
|
2021-02-16 22:21:40 +00:00
|
|
|
it('emits when theme color changes', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const src = url.format({
|
2023-09-07 06:50:14 +00:00
|
|
|
pathname: `${fixtures.replaceAll('\\', '/')}/pages/theme-color.html`,
|
2021-02-16 22:21:40 +00:00
|
|
|
protocol: 'file',
|
|
|
|
slashes: true
|
|
|
|
});
|
|
|
|
const message = await w.webContents.executeJavaScript(`new Promise((resolve, reject) => {
|
|
|
|
const webview = new WebView()
|
|
|
|
webview.setAttribute('src', '${src}')
|
|
|
|
webview.addEventListener('did-change-theme-color', (e) => {
|
|
|
|
resolve('ok')
|
|
|
|
})
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})`);
|
|
|
|
expect(message).to.equal('ok');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
describe('devtools', () => {
|
|
|
|
afterEach(closeAllWindows);
|
2023-04-04 13:48:51 +00:00
|
|
|
// FIXME: This test is flaky on WOA, so skip it there.
|
2022-08-03 16:59:00 +00:00
|
|
|
ifit(process.platform !== 'win32' || process.arch !== 'arm64')('loads devtools extensions registered on the parent window', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
2019-11-14 22:09:03 +00:00
|
|
|
});
|
2022-08-03 16:59:00 +00:00
|
|
|
w.webContents.session.removeExtension('foo');
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo');
|
2023-07-31 17:47:32 +00:00
|
|
|
await w.webContents.session.loadExtension(extensionPath, {
|
|
|
|
allowFileAccess: true
|
|
|
|
});
|
2022-08-03 16:59:00 +00:00
|
|
|
|
|
|
|
w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'webview-devtools.html'));
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
src: `file://${path.join(__dirname, 'fixtures', 'blank.html')}`
|
2022-08-03 19:03:44 +00:00
|
|
|
}, { openDevTools: true });
|
2022-08-03 16:59:00 +00:00
|
|
|
let childWebContentsId = 0;
|
|
|
|
app.once('web-contents-created', (e, webContents) => {
|
|
|
|
childWebContentsId = webContents.id;
|
|
|
|
webContents.on('devtools-opened', function () {
|
|
|
|
const showPanelIntervalId = setInterval(function () {
|
|
|
|
if (!webContents.isDestroyed() && webContents.devToolsWebContents) {
|
|
|
|
webContents.devToolsWebContents.executeJavaScript('(' + function () {
|
2023-10-26 18:36:04 +00:00
|
|
|
const { EUI } = (window as any);
|
|
|
|
const instance = EUI.InspectorView.InspectorView.instance();
|
|
|
|
const tabs = instance.tabbedPane.tabs;
|
2022-08-03 16:59:00 +00:00
|
|
|
const lastPanelId: any = tabs[tabs.length - 1].id;
|
2023-10-26 18:36:04 +00:00
|
|
|
instance.showPanel(lastPanelId);
|
2022-08-03 16:59:00 +00:00
|
|
|
}.toString() + ')()');
|
|
|
|
} else {
|
|
|
|
clearInterval(showPanelIntervalId);
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, { runtimeId, tabId }] = await once(ipcMain, 'answer');
|
2022-08-03 16:59:00 +00:00
|
|
|
expect(runtimeId).to.match(/^[a-z]{32}$/);
|
|
|
|
expect(tabId).to.equal(childWebContentsId);
|
2022-10-17 05:57:44 +00:00
|
|
|
await w.webContents.executeJavaScript('webview.closeDevTools()');
|
2022-08-03 16:59:00 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('zoom behavior', () => {
|
|
|
|
const zoomScheme = standardScheme;
|
|
|
|
const webviewSession = session.fromPartition('webview-temp');
|
|
|
|
|
2022-08-03 16:59:00 +00:00
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
before(() => {
|
2019-09-03 17:10:58 +00:00
|
|
|
const protocol = webviewSession.protocol;
|
|
|
|
protocol.registerStringProtocol(zoomScheme, (request, callback) => {
|
|
|
|
callback('hello');
|
2020-06-02 16:46:18 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
after(() => {
|
2019-09-03 17:10:58 +00:00
|
|
|
const protocol = webviewSession.protocol;
|
2020-06-02 16:46:18 +00:00
|
|
|
protocol.unregisterProtocol(zoomScheme);
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('inherits the zoomFactor of the parent window', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
2021-03-01 21:52:29 +00:00
|
|
|
zoomFactor: 1.2,
|
|
|
|
contextIsolation: false
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
|
|
|
});
|
2023-02-23 23:53:53 +00:00
|
|
|
const zoomEventPromise = once(ipcMain, 'webview-parent-zoom-level');
|
2019-09-03 17:10:58 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-zoom-factor.html'));
|
|
|
|
|
|
|
|
const [, zoomFactor, zoomLevel] = await zoomEventPromise;
|
|
|
|
expect(zoomFactor).to.equal(1.2);
|
|
|
|
expect(zoomLevel).to.equal(1);
|
|
|
|
});
|
|
|
|
|
2023-12-04 15:39:20 +00:00
|
|
|
it('maintains the zoom level for a given host in the same session after navigation', () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const zoomPromise = new Promise<void>((resolve) => {
|
|
|
|
ipcMain.on('webview-zoom-persist-level', (_event, values) => {
|
|
|
|
resolve(values);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-zoom-change-persist-host.html'));
|
|
|
|
|
|
|
|
expect(zoomPromise).to.eventually.deep.equal({
|
|
|
|
initialZoomLevel: 2,
|
|
|
|
switchZoomLevel: 3,
|
|
|
|
finalZoomLevel: 2
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
it('maintains zoom level on navigation', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
2021-03-01 21:52:29 +00:00
|
|
|
zoomFactor: 1.2,
|
|
|
|
contextIsolation: false
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
const promise = new Promise<void>((resolve) => {
|
2019-09-03 17:10:58 +00:00
|
|
|
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.loadFile(path.join(fixtures, 'pages', 'webview-custom-zoom-level.html'));
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('maintains zoom level when navigating within same page', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
2021-03-01 21:52:29 +00:00
|
|
|
zoomFactor: 1.2,
|
|
|
|
contextIsolation: false
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
const promise = new Promise<void>((resolve) => {
|
2019-09-03 17:10:58 +00:00
|
|
|
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.loadFile(path.join(fixtures, 'pages', 'webview-in-page-navigate.html'));
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inherits zoom level for the origin when available', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
2021-03-01 21:52:29 +00:00
|
|
|
zoomFactor: 1.2,
|
|
|
|
contextIsolation: false
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-origin-zoom-level.html'));
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, zoomLevel] = await once(ipcMain, 'webview-origin-zoom-level');
|
2019-09-03 17:10:58 +00:00
|
|
|
expect(zoomLevel).to.equal(2.0);
|
|
|
|
});
|
2020-07-28 20:00:44 +00:00
|
|
|
|
|
|
|
it('does not crash when navigating with zoom level inherited from parent', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
zoomFactor: 1.2,
|
2021-03-01 21:52:29 +00:00
|
|
|
session: webviewSession,
|
|
|
|
contextIsolation: false
|
2020-07-28 20:00:44 +00:00
|
|
|
}
|
|
|
|
});
|
2023-06-22 18:38:52 +00:00
|
|
|
const attachPromise = once(w.webContents, 'did-attach-webview') as Promise<[any, WebContents]>;
|
2023-02-23 23:53:53 +00:00
|
|
|
const readyPromise = once(ipcMain, 'dom-ready');
|
2020-07-28 20:00:44 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-zoom-inherited.html'));
|
|
|
|
const [, webview] = await attachPromise;
|
|
|
|
await readyPromise;
|
|
|
|
expect(webview.getZoomFactor()).to.equal(1.2);
|
|
|
|
await w.loadURL(`${zoomScheme}://host1`);
|
|
|
|
});
|
2021-10-11 00:33:35 +00:00
|
|
|
|
|
|
|
it('does not crash when changing zoom level after webview is destroyed', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
session: webviewSession,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
2023-06-22 18:38:52 +00:00
|
|
|
const attachPromise = once(w.webContents, 'did-attach-webview') as Promise<[any, WebContents]>;
|
2021-10-11 00:33:35 +00:00
|
|
|
await w.loadFile(path.join(fixtures, 'pages', 'webview-zoom-inherited.html'));
|
|
|
|
await attachPromise;
|
|
|
|
await w.webContents.executeJavaScript('view.remove()');
|
|
|
|
w.webContents.setZoomLevel(0.5);
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
2021-07-02 00:56:29 +00:00
|
|
|
describe('requestFullscreen from webview', () => {
|
2022-08-03 16:59:00 +00:00
|
|
|
afterEach(closeAllWindows);
|
2023-06-22 18:38:52 +00:00
|
|
|
async function loadWebViewWindow (): Promise<[BrowserWindow, WebContents]> {
|
2021-07-02 00:56:29 +00:00
|
|
|
const w = new BrowserWindow({
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
2022-06-07 16:59:50 +00:00
|
|
|
|
2023-06-22 18:38:52 +00:00
|
|
|
const attachPromise = once(w.webContents, 'did-attach-webview') as Promise<[any, WebContents]>;
|
2023-02-23 23:53:53 +00:00
|
|
|
const loadPromise = once(w.webContents, 'did-finish-load');
|
|
|
|
const readyPromise = once(ipcMain, 'webview-ready');
|
2022-06-07 16:59:50 +00:00
|
|
|
|
2021-07-02 00:56:29 +00:00
|
|
|
w.loadFile(path.join(__dirname, 'fixtures', 'webview', 'fullscreen', 'main.html'));
|
2022-06-07 16:59:50 +00:00
|
|
|
|
2021-07-02 00:56:29 +00:00
|
|
|
const [, webview] = await attachPromise;
|
2022-06-07 16:59:50 +00:00
|
|
|
await Promise.all([readyPromise, loadPromise]);
|
|
|
|
|
2021-07-02 00:56:29 +00:00
|
|
|
return [w, webview];
|
|
|
|
};
|
|
|
|
|
2022-05-05 18:42:38 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
// The leaving animation is un-observable but can interfere with future tests
|
|
|
|
// Specifically this is async on macOS but can be on other platforms too
|
2023-02-23 23:53:53 +00:00
|
|
|
await setTimeout(1000);
|
2022-05-31 08:43:42 +00:00
|
|
|
|
|
|
|
closeAllWindows();
|
2022-05-05 18:42:38 +00:00
|
|
|
});
|
2021-07-02 00:56:29 +00:00
|
|
|
|
2022-06-07 16:59:50 +00:00
|
|
|
ifit(process.platform !== 'darwin')('should make parent frame element fullscreen too (non-macOS)', async () => {
|
2021-07-02 00:56:29 +00:00
|
|
|
const [w, webview] = await loadWebViewWindow();
|
|
|
|
expect(await w.webContents.executeJavaScript('isIframeFullscreen()')).to.be.false();
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const parentFullscreen = once(ipcMain, 'fullscreenchange');
|
2021-07-02 00:56:29 +00:00
|
|
|
await webview.executeJavaScript('document.getElementById("div").requestFullscreen()', true);
|
|
|
|
await parentFullscreen;
|
2022-06-07 16:59:50 +00:00
|
|
|
|
|
|
|
expect(await w.webContents.executeJavaScript('isIframeFullscreen()')).to.be.true();
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const close = once(w, 'closed');
|
2022-06-07 16:59:50 +00:00
|
|
|
w.close();
|
|
|
|
await close;
|
|
|
|
});
|
|
|
|
|
|
|
|
ifit(process.platform === 'darwin')('should make parent frame element fullscreen too (macOS)', async () => {
|
|
|
|
const [w, webview] = await loadWebViewWindow();
|
|
|
|
expect(await w.webContents.executeJavaScript('isIframeFullscreen()')).to.be.false();
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const parentFullscreen = once(ipcMain, 'fullscreenchange');
|
|
|
|
const enterHTMLFS = once(w.webContents, 'enter-html-full-screen');
|
|
|
|
const leaveHTMLFS = once(w.webContents, 'leave-html-full-screen');
|
2022-06-07 16:59:50 +00:00
|
|
|
|
|
|
|
await webview.executeJavaScript('document.getElementById("div").requestFullscreen()', true);
|
2021-07-02 00:56:29 +00:00
|
|
|
expect(await w.webContents.executeJavaScript('isIframeFullscreen()')).to.be.true();
|
2022-05-31 08:43:42 +00:00
|
|
|
|
2022-06-07 16:59:50 +00:00
|
|
|
await webview.executeJavaScript('document.exitFullscreen()');
|
|
|
|
await Promise.all([enterHTMLFS, leaveHTMLFS, parentFullscreen]);
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const close = once(w, 'closed');
|
2022-05-31 08:43:42 +00:00
|
|
|
w.close();
|
2022-06-07 16:59:50 +00:00
|
|
|
await close;
|
2021-07-02 00:56:29 +00:00
|
|
|
});
|
2021-07-14 11:51:26 +00:00
|
|
|
|
|
|
|
// FIXME(zcbenz): Fullscreen events do not work on Linux.
|
2022-05-31 08:43:42 +00:00
|
|
|
ifit(process.platform !== 'linux')('exiting fullscreen should unfullscreen window', async () => {
|
2021-07-14 11:51:26 +00:00
|
|
|
const [w, webview] = await loadWebViewWindow();
|
2023-02-23 23:53:53 +00:00
|
|
|
const enterFullScreen = once(w, 'enter-full-screen');
|
2021-07-14 11:51:26 +00:00
|
|
|
await webview.executeJavaScript('document.getElementById("div").requestFullscreen()', true);
|
|
|
|
await enterFullScreen;
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const leaveFullScreen = once(w, 'leave-full-screen');
|
2021-07-14 11:51:26 +00:00
|
|
|
await webview.executeJavaScript('document.exitFullscreen()', true);
|
|
|
|
await leaveFullScreen;
|
2023-02-23 23:53:53 +00:00
|
|
|
await setTimeout();
|
2021-07-14 11:51:26 +00:00
|
|
|
expect(w.isFullScreen()).to.be.false();
|
2022-05-31 08:43:42 +00:00
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const close = once(w, 'closed');
|
2022-05-31 08:43:42 +00:00
|
|
|
w.close();
|
2022-06-07 16:59:50 +00:00
|
|
|
await close;
|
2021-07-14 11:51:26 +00:00
|
|
|
});
|
|
|
|
|
2023-10-31 15:59:39 +00:00
|
|
|
it('pressing ESC should unfullscreen window', async () => {
|
2021-07-14 11:51:26 +00:00
|
|
|
const [w, webview] = await loadWebViewWindow();
|
2023-02-23 23:53:53 +00:00
|
|
|
const enterFullScreen = once(w, 'enter-full-screen');
|
2021-07-14 11:51:26 +00:00
|
|
|
await webview.executeJavaScript('document.getElementById("div").requestFullscreen()', true);
|
|
|
|
await enterFullScreen;
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const leaveFullScreen = once(w, 'leave-full-screen');
|
2023-10-31 15:59:39 +00:00
|
|
|
webview.sendInputEvent({ type: 'keyDown', keyCode: 'Escape' });
|
2021-07-14 11:51:26 +00:00
|
|
|
await leaveFullScreen;
|
2023-10-31 15:59:39 +00:00
|
|
|
await setTimeout(1000);
|
2021-07-14 11:51:26 +00:00
|
|
|
expect(w.isFullScreen()).to.be.false();
|
2022-05-31 08:43:42 +00:00
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const close = once(w, 'closed');
|
2022-05-31 08:43:42 +00:00
|
|
|
w.close();
|
2022-06-07 16:59:50 +00:00
|
|
|
await close;
|
2021-07-14 11:51:26 +00:00
|
|
|
});
|
2021-08-17 07:03:45 +00:00
|
|
|
|
|
|
|
it('pressing ESC should emit the leave-html-full-screen event', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-22 18:38:52 +00:00
|
|
|
const didAttachWebview = once(w.webContents, 'did-attach-webview') as Promise<[any, WebContents]>;
|
2021-08-17 07:03:45 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-did-attach-event.html'));
|
|
|
|
|
|
|
|
const [, webContents] = await didAttachWebview;
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const enterFSWindow = once(w, 'enter-html-full-screen');
|
|
|
|
const enterFSWebview = once(webContents, 'enter-html-full-screen');
|
2021-08-17 07:03:45 +00:00
|
|
|
await webContents.executeJavaScript('document.getElementById("div").requestFullscreen()', true);
|
|
|
|
await enterFSWindow;
|
|
|
|
await enterFSWebview;
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const leaveFSWindow = once(w, 'leave-html-full-screen');
|
|
|
|
const leaveFSWebview = once(webContents, 'leave-html-full-screen');
|
2021-08-17 07:03:45 +00:00
|
|
|
webContents.sendInputEvent({ type: 'keyDown', keyCode: 'Escape' });
|
|
|
|
await leaveFSWebview;
|
2022-06-07 16:59:50 +00:00
|
|
|
await leaveFSWindow;
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const close = once(w, 'closed');
|
2022-06-07 16:59:50 +00:00
|
|
|
w.close();
|
|
|
|
await close;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support user gesture', async () => {
|
|
|
|
const [w, webview] = await loadWebViewWindow();
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const waitForEnterHtmlFullScreen = once(webview, 'enter-html-full-screen');
|
2022-06-07 16:59:50 +00:00
|
|
|
|
|
|
|
const jsScript = "document.querySelector('video').webkitRequestFullscreen()";
|
|
|
|
webview.executeJavaScript(jsScript, true);
|
|
|
|
|
|
|
|
await waitForEnterHtmlFullScreen;
|
2022-05-31 08:43:42 +00:00
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const close = once(w, 'closed');
|
2022-05-31 08:43:42 +00:00
|
|
|
w.close();
|
2022-06-07 16:59:50 +00:00
|
|
|
await close;
|
2021-08-17 07:03:45 +00:00
|
|
|
});
|
2021-07-02 00:56:29 +00:00
|
|
|
});
|
|
|
|
|
2022-01-06 17:28:03 +00:00
|
|
|
describe('child windows', () => {
|
2019-09-03 17:10:58 +00:00
|
|
|
let w: BrowserWindow;
|
|
|
|
beforeEach(async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, webviewTag: true, contextIsolation: false } });
|
2019-09-03 17:10:58 +00:00
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
it('opens window of about:blank with cross-scripting enabled', async () => {
|
|
|
|
// Don't wait for loading to finish.
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
nodeintegration: 'on',
|
2022-01-06 17:28:03 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2019-09-03 17:10:58 +00:00
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-blank.html')}`
|
|
|
|
});
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, content] = await once(ipcMain, 'answer');
|
2019-09-03 17:10:58 +00:00
|
|
|
expect(content).to.equal('Hello');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('opens window of same domain with cross-scripting enabled', async () => {
|
|
|
|
// Don't wait for loading to finish.
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
nodeintegration: 'on',
|
2022-01-06 17:28:03 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2019-09-03 17:10:58 +00:00
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-file.html')}`
|
|
|
|
});
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, content] = await once(ipcMain, 'answer');
|
2019-09-03 17:10:58 +00:00
|
|
|
expect(content).to.equal('Hello');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null from window.open when allowpopups is not set', async () => {
|
|
|
|
// Don't wait for loading to finish.
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
nodeintegration: 'on',
|
2022-01-06 17:28:03 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2019-09-03 17:10:58 +00:00
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-no-allowpopups.html')}`
|
|
|
|
});
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, { windowOpenReturnedNull }] = await once(ipcMain, 'answer');
|
2019-09-03 17:10:58 +00:00
|
|
|
expect(windowOpenReturnedNull).to.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('blocks accessing cross-origin frames', async () => {
|
|
|
|
// Don't wait for loading to finish.
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
nodeintegration: 'on',
|
2022-01-06 17:28:03 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2019-09-03 17:10:58 +00:00
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-cross-origin.html')}`
|
|
|
|
});
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, content] = await once(ipcMain, 'answer');
|
2019-09-03 17:10:58 +00:00
|
|
|
const expectedContent =
|
2023-09-18 20:44:09 +00:00
|
|
|
/Failed to read a named property 'toString' from 'Location': Blocked a frame with origin "(.*?)" from accessing a cross-origin frame./;
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2023-09-18 20:44:09 +00:00
|
|
|
expect(content).to.match(expectedContent);
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('emits a browser-window-created event', async () => {
|
|
|
|
// Don't wait for loading to finish.
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'on',
|
2022-01-06 17:28:03 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2019-09-03 17:10:58 +00:00
|
|
|
src: `file://${fixtures}/pages/window-open.html`
|
|
|
|
});
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
await once(app, 'browser-window-created');
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
2020-07-01 19:14:38 +00:00
|
|
|
it('emits a web-contents-created event', async () => {
|
|
|
|
const webContentsCreated = emittedUntil(app, 'web-contents-created',
|
|
|
|
(event: Electron.Event, contents: Electron.WebContents) => contents.getType() === 'window');
|
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'on',
|
2022-01-06 17:28:03 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2019-09-03 17:10:58 +00:00
|
|
|
src: `file://${fixtures}/pages/window-open.html`
|
|
|
|
});
|
2020-07-01 19:14:38 +00:00
|
|
|
|
|
|
|
await webContentsCreated;
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
2021-06-30 01:10:18 +00:00
|
|
|
|
|
|
|
it('does not crash when creating window with noopener', async () => {
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-noopener.html')}`
|
|
|
|
});
|
2023-02-23 23:53:53 +00:00
|
|
|
await once(app, 'browser-window-created');
|
2021-06-30 01:10:18 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('webpreferences attribute', () => {
|
|
|
|
let w: BrowserWindow;
|
|
|
|
beforeEach(async () => {
|
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, webviewTag: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
2019-11-01 20:37:02 +00:00
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
it('can enable context isolation', async () => {
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'yes',
|
|
|
|
preload: `file://${fixtures}/api/isolated-preload.js`,
|
|
|
|
src: `file://${fixtures}/api/isolated.html`,
|
|
|
|
webpreferences: 'contextIsolation=yes'
|
|
|
|
});
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const [, data] = await once(ipcMain, 'isolated-world');
|
2019-09-03 17:10:58 +00:00
|
|
|
expect(data).to.deep.equal({
|
|
|
|
preloadContext: {
|
|
|
|
preloadProperty: 'number',
|
|
|
|
pageProperty: 'undefined',
|
|
|
|
typeofRequire: 'function',
|
|
|
|
typeofProcess: 'object',
|
|
|
|
typeofArrayPush: 'function',
|
|
|
|
typeofFunctionApply: 'function',
|
|
|
|
typeofPreloadExecuteJavaScriptProperty: 'undefined'
|
|
|
|
},
|
|
|
|
pageContext: {
|
|
|
|
preloadProperty: 'undefined',
|
|
|
|
pageProperty: 'string',
|
|
|
|
typeofRequire: 'undefined',
|
|
|
|
typeofProcess: 'undefined',
|
|
|
|
typeofArrayPush: 'number',
|
|
|
|
typeofFunctionApply: 'boolean',
|
|
|
|
typeofPreloadExecuteJavaScriptProperty: 'number',
|
|
|
|
typeofOpenedWindow: 'object'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-01-05 04:00:27 +00:00
|
|
|
describe('webpreferences attribute', () => {
|
|
|
|
const WINDOW_BACKGROUND_COLOR = '#55ccbb';
|
|
|
|
|
|
|
|
let w: BrowserWindow;
|
|
|
|
before(async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await w.loadURL(`file://${fixtures}/pages/flex-webview.html`);
|
|
|
|
w.setBackgroundColor(WINDOW_BACKGROUND_COLOR);
|
|
|
|
});
|
|
|
|
afterEach(async () => {
|
|
|
|
await w.webContents.executeJavaScript(`{
|
|
|
|
for (const el of document.querySelectorAll('webview')) el.remove();
|
|
|
|
}`);
|
|
|
|
});
|
|
|
|
after(() => w.close());
|
|
|
|
|
2024-07-17 00:16:25 +00:00
|
|
|
ifit(hasCapturableScreen())('is transparent by default', async () => {
|
2024-01-05 04:00:27 +00:00
|
|
|
await loadWebView(w.webContents, {
|
|
|
|
src: 'data:text/html,foo'
|
|
|
|
});
|
|
|
|
|
2024-07-17 00:16:25 +00:00
|
|
|
const screenCapture = new ScreenCapture();
|
2024-02-28 03:54:20 +00:00
|
|
|
await screenCapture.expectColorAtCenterMatches(WINDOW_BACKGROUND_COLOR);
|
2024-01-05 04:00:27 +00:00
|
|
|
});
|
|
|
|
|
2024-07-17 00:16:25 +00:00
|
|
|
ifit(hasCapturableScreen())('remains transparent when set', async () => {
|
2024-01-05 04:00:27 +00:00
|
|
|
await loadWebView(w.webContents, {
|
|
|
|
src: 'data:text/html,foo',
|
|
|
|
webpreferences: 'transparent=yes'
|
|
|
|
});
|
|
|
|
|
2024-07-17 00:16:25 +00:00
|
|
|
const screenCapture = new ScreenCapture();
|
2024-02-28 03:54:20 +00:00
|
|
|
await screenCapture.expectColorAtCenterMatches(WINDOW_BACKGROUND_COLOR);
|
2024-01-05 04:00:27 +00:00
|
|
|
});
|
|
|
|
|
2024-07-17 00:16:25 +00:00
|
|
|
ifit(hasCapturableScreen())('can disable transparency', async () => {
|
2024-01-05 04:00:27 +00:00
|
|
|
await loadWebView(w.webContents, {
|
|
|
|
src: 'data:text/html,foo',
|
|
|
|
webpreferences: 'transparent=no'
|
|
|
|
});
|
|
|
|
|
2024-07-17 00:16:25 +00:00
|
|
|
const screenCapture = new ScreenCapture();
|
2024-02-28 03:54:20 +00:00
|
|
|
await screenCapture.expectColorAtCenterMatches(HexColors.WHITE);
|
2024-01-05 04:00:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
describe('permission request handlers', () => {
|
|
|
|
let w: BrowserWindow;
|
|
|
|
beforeEach(async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, webviewTag: true, contextIsolation: false } });
|
2019-09-03 17:10:58 +00:00
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
const partition = 'permissionTest';
|
|
|
|
|
2019-11-01 20:37:02 +00:00
|
|
|
function setUpRequestHandler (webContentsId: number, requestedPermission: string) {
|
2021-01-22 19:25:47 +00:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2019-09-03 17:10:58 +00:00
|
|
|
session.fromPartition(partition).setPermissionRequestHandler(function (webContents, permission, callback) {
|
|
|
|
if (webContents.id === webContentsId) {
|
2024-06-07 21:18:35 +00:00
|
|
|
// All midi permission requests are blocked or allowed as midiSysex permissions
|
|
|
|
// since https://chromium-review.googlesource.com/c/chromium/src/+/5154368
|
|
|
|
if (permission === 'midiSysex') {
|
|
|
|
const allowed = requestedPermission === 'midi' || requestedPermission === 'midiSysex';
|
|
|
|
return callback(!allowed);
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
expect(permission).to.equal(requestedPermission);
|
|
|
|
} catch (e) {
|
|
|
|
return reject(e);
|
|
|
|
}
|
|
|
|
callback(false);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
afterEach(() => {
|
|
|
|
session.fromPartition(partition).setPermissionRequestHandler(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
// This is disabled because CI machines don't have cameras or microphones,
|
|
|
|
// so Chrome responds with "NotFoundError" instead of
|
|
|
|
// "PermissionDeniedError". It should be re-enabled if we find a way to mock
|
|
|
|
// the presence of a microphone & camera.
|
|
|
|
xit('emits when using navigator.getUserMedia api', async () => {
|
2023-02-23 23:53:53 +00:00
|
|
|
const errorFromRenderer = once(ipcMain, 'message');
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
src: `file://${fixtures}/pages/permissions/media.html`,
|
|
|
|
partition,
|
|
|
|
nodeintegration: 'on'
|
|
|
|
});
|
2023-06-22 18:38:52 +00:00
|
|
|
const [, webViewContents] = await once(app, 'web-contents-created') as [any, WebContents];
|
2019-09-03 17:10:58 +00:00
|
|
|
setUpRequestHandler(webViewContents.id, 'media');
|
|
|
|
const [, errorName] = await errorFromRenderer;
|
|
|
|
expect(errorName).to.equal('PermissionDeniedError');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits when using navigator.geolocation api', async () => {
|
2023-02-23 23:53:53 +00:00
|
|
|
const errorFromRenderer = once(ipcMain, 'message');
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
src: `file://${fixtures}/pages/permissions/geolocation.html`,
|
|
|
|
partition,
|
2021-03-01 21:52:29 +00:00
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no'
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
2023-06-22 18:38:52 +00:00
|
|
|
const [, webViewContents] = await once(app, 'web-contents-created') as [any, WebContents];
|
2019-09-03 17:10:58 +00:00
|
|
|
setUpRequestHandler(webViewContents.id, 'geolocation');
|
|
|
|
const [, error] = await errorFromRenderer;
|
|
|
|
expect(error).to.equal('User denied Geolocation');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits when using navigator.requestMIDIAccess without sysex api', async () => {
|
2023-02-23 23:53:53 +00:00
|
|
|
const errorFromRenderer = once(ipcMain, 'message');
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
src: `file://${fixtures}/pages/permissions/midi.html`,
|
|
|
|
partition,
|
2021-03-01 21:52:29 +00:00
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no'
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
2023-06-22 18:38:52 +00:00
|
|
|
const [, webViewContents] = await once(app, 'web-contents-created') as [any, WebContents];
|
2019-09-03 17:10:58 +00:00
|
|
|
setUpRequestHandler(webViewContents.id, 'midi');
|
|
|
|
const [, error] = await errorFromRenderer;
|
2024-09-26 09:12:12 +00:00
|
|
|
expect(error).to.equal('NotAllowedError');
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('emits when using navigator.requestMIDIAccess with sysex api', async () => {
|
2023-02-23 23:53:53 +00:00
|
|
|
const errorFromRenderer = once(ipcMain, 'message');
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
src: `file://${fixtures}/pages/permissions/midi-sysex.html`,
|
|
|
|
partition,
|
2021-03-01 21:52:29 +00:00
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no'
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
2023-06-22 18:38:52 +00:00
|
|
|
const [, webViewContents] = await once(app, 'web-contents-created') as [any, WebContents];
|
2019-09-03 17:10:58 +00:00
|
|
|
setUpRequestHandler(webViewContents.id, 'midiSysex');
|
|
|
|
const [, error] = await errorFromRenderer;
|
2024-09-26 09:12:12 +00:00
|
|
|
expect(error).to.equal('NotAllowedError');
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('emits when accessing external protocol', async () => {
|
|
|
|
loadWebView(w.webContents, {
|
2020-03-20 15:12:18 +00:00
|
|
|
src: 'magnet:test',
|
2019-11-01 20:37:02 +00:00
|
|
|
partition
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
2023-06-22 18:38:52 +00:00
|
|
|
const [, webViewContents] = await once(app, 'web-contents-created') as [any, WebContents];
|
2019-09-03 17:10:58 +00:00
|
|
|
await setUpRequestHandler(webViewContents.id, 'openExternal');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits when using Notification.requestPermission', async () => {
|
2023-02-23 23:53:53 +00:00
|
|
|
const errorFromRenderer = once(ipcMain, 'message');
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
src: `file://${fixtures}/pages/permissions/notification.html`,
|
|
|
|
partition,
|
2021-03-01 21:52:29 +00:00
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no'
|
2019-09-03 17:10:58 +00:00
|
|
|
});
|
2023-06-22 18:38:52 +00:00
|
|
|
const [, webViewContents] = await once(app, 'web-contents-created') as [any, WebContents];
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
await setUpRequestHandler(webViewContents.id, 'notifications');
|
|
|
|
|
|
|
|
const [, error] = await errorFromRenderer;
|
|
|
|
expect(error).to.equal('denied');
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-11-04 02:15:20 +00:00
|
|
|
describe('DOM events', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('receives extra properties on DOM events when contextIsolation is enabled', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
contextIsolation: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const message = await w.webContents.executeJavaScript(`new Promise((resolve, reject) => {
|
|
|
|
const webview = new WebView()
|
|
|
|
webview.setAttribute('src', 'data:text/html,<script>console.log("hi")</script>')
|
|
|
|
webview.addEventListener('console-message', (e) => {
|
|
|
|
resolve(e.message)
|
|
|
|
})
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})`);
|
|
|
|
expect(message).to.equal('hi');
|
|
|
|
});
|
2021-05-05 18:31:44 +00:00
|
|
|
|
|
|
|
it('emits focus event when contextIsolation is enabled', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
contextIsolation: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
await w.webContents.executeJavaScript(`new Promise((resolve, reject) => {
|
|
|
|
const webview = new WebView()
|
|
|
|
webview.setAttribute('src', 'about:blank')
|
|
|
|
webview.addEventListener('dom-ready', () => {
|
|
|
|
webview.focus()
|
|
|
|
})
|
|
|
|
webview.addEventListener('focus', () => {
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})`);
|
|
|
|
});
|
2020-11-04 02:15:20 +00:00
|
|
|
});
|
2022-08-03 16:59:00 +00:00
|
|
|
|
|
|
|
describe('attributes', () => {
|
|
|
|
let w: WebContents;
|
|
|
|
before(async () => {
|
|
|
|
const window = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await window.loadURL(`file://${fixtures}/pages/blank.html`);
|
|
|
|
w = window.webContents;
|
|
|
|
});
|
|
|
|
afterEach(async () => {
|
|
|
|
await w.executeJavaScript(`{
|
2023-08-31 14:36:43 +00:00
|
|
|
for (const el of document.querySelectorAll('webview')) el.remove();
|
2022-08-03 16:59:00 +00:00
|
|
|
}`);
|
|
|
|
});
|
|
|
|
after(closeAllWindows);
|
|
|
|
|
|
|
|
describe('src attribute', () => {
|
|
|
|
it('specifies the page to load', async () => {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
|
|
|
});
|
|
|
|
expect(message).to.equal('a');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('navigates to new page when changed', async () => {
|
|
|
|
await loadWebView(w, {
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
|
|
|
});
|
|
|
|
|
|
|
|
const { message } = await w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
webview.addEventListener('console-message', e => resolve({message: e.message}))
|
|
|
|
webview.src = ${JSON.stringify(`file://${fixtures}/pages/b.html`)}
|
|
|
|
})`);
|
|
|
|
|
|
|
|
expect(message).to.equal('b');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('resolves relative URLs', async () => {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
src: './e.html'
|
|
|
|
});
|
|
|
|
expect(message).to.equal('Window script is loaded before preload script');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ignores empty values', async () => {
|
|
|
|
loadWebView(w, {});
|
|
|
|
|
|
|
|
for (const emptyValue of ['""', 'null', 'undefined']) {
|
|
|
|
const src = await w.executeJavaScript(`webview.src = ${emptyValue}, webview.src`);
|
|
|
|
expect(src).to.equal('');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not wait until loadURL is resolved', async () => {
|
|
|
|
await loadWebView(w, { src: 'about:blank' });
|
|
|
|
|
|
|
|
const delay = await w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
const before = Date.now();
|
|
|
|
webview.src = 'file://${fixtures}/pages/blank.html';
|
|
|
|
const now = Date.now();
|
|
|
|
resolve(now - before);
|
|
|
|
})`);
|
|
|
|
|
|
|
|
// Setting src is essentially sending a sync IPC message, which should
|
|
|
|
// not exceed more than a few ms.
|
|
|
|
//
|
|
|
|
// This is for testing #18638.
|
|
|
|
expect(delay).to.be.below(100);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('nodeintegration attribute', () => {
|
|
|
|
it('inserts no node symbols when not set', async () => {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
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 loadWebViewAndWaitForMessage(w, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
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 () {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
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', async () => {
|
|
|
|
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
|
|
|
|
});
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
src
|
|
|
|
});
|
|
|
|
expect(JSON.parse(message).isProcessGlobalUndefined).to.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
ifit(!process.env.ELECTRON_SKIP_NATIVE_MODULE_TESTS)('loads native modules when navigation happens', async function () {
|
|
|
|
await loadWebView(w, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
src: `file://${fixtures}/pages/native-module.html`
|
|
|
|
});
|
|
|
|
|
|
|
|
const message = await w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
webview.addEventListener('console-message', e => resolve(e.message))
|
|
|
|
webview.reload();
|
|
|
|
})`);
|
|
|
|
|
|
|
|
expect(message).to.equal('function');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('preload attribute', () => {
|
2022-08-05 00:20:17 +00:00
|
|
|
useRemoteContext({ webPreferences: { webviewTag: true } });
|
2022-08-03 16:59:00 +00:00
|
|
|
it('loads the script before other scripts in window', async () => {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
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 loadWebViewAndWaitForMessage(w, {
|
|
|
|
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('runs in the correct scope when sandboxed', async () => {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
preload: `${fixtures}/module/preload-context.js`,
|
|
|
|
src: `file://${fixtures}/api/blank.html`,
|
|
|
|
webpreferences: 'sandbox=yes'
|
|
|
|
});
|
|
|
|
|
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
require: 'function', // arguments passed to it should be available
|
|
|
|
electron: 'undefined', // objects from the scope it is called from should not be available
|
|
|
|
window: 'object', // the window object should be available
|
|
|
|
localVar: 'undefined' // but local variables should not be exposed to the window
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('preload script can require modules that still use "process" and "Buffer" when nodeintegration is off', async () => {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
preload: `${fixtures}/module/preload-node-off-wrapper.js`,
|
|
|
|
webpreferences: 'sandbox=no',
|
|
|
|
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(w, {
|
|
|
|
preload: `${fixtures}/module/preload-ipc.js`,
|
|
|
|
src: `file://${fixtures}/pages/e.html`
|
|
|
|
});
|
|
|
|
|
|
|
|
const message = 'boom!';
|
|
|
|
const { channel, args } = await w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
webview.send('ping', ${JSON.stringify(message)})
|
|
|
|
webview.addEventListener('ipc-message', ({channel, args}) => resolve({channel, args}))
|
|
|
|
})`);
|
|
|
|
|
|
|
|
expect(channel).to.equal('pong');
|
|
|
|
expect(args).to.deep.equal([message]);
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('<webview>.sendToFrame()', async (fixtures: string) => {
|
|
|
|
const w = new WebView();
|
|
|
|
w.setAttribute('nodeintegration', 'on');
|
|
|
|
w.setAttribute('webpreferences', 'contextIsolation=no');
|
|
|
|
w.setAttribute('preload', `file://${fixtures}/module/preload-ipc.js`);
|
|
|
|
w.setAttribute('src', `file://${fixtures}/pages/ipc-message.html`);
|
|
|
|
document.body.appendChild(w);
|
2023-02-23 23:53:53 +00:00
|
|
|
const { frameId } = await new Promise<any>(resolve => w.addEventListener('ipc-message', resolve, { once: true }));
|
2022-08-03 16:59:00 +00:00
|
|
|
|
|
|
|
const message = 'boom!';
|
|
|
|
|
|
|
|
w.sendToFrame(frameId, 'ping', message);
|
2023-02-23 23:53:53 +00:00
|
|
|
const { channel, args } = await new Promise<any>(resolve => w.addEventListener('ipc-message', resolve, { once: true }));
|
2022-08-03 16:59:00 +00:00
|
|
|
|
|
|
|
expect(channel).to.equal('pong');
|
|
|
|
expect(args).to.deep.equal([message]);
|
|
|
|
}, [fixtures]);
|
|
|
|
|
|
|
|
it('works without script tag in page', async () => {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
preload: `${fixtures}/module/preload.js`,
|
|
|
|
webpreferences: 'sandbox=no',
|
|
|
|
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 loadWebViewAndWaitForMessage(w, {
|
|
|
|
preload: '../module/preload.js',
|
|
|
|
webpreferences: 'sandbox=no',
|
|
|
|
src: `file://${fixtures}/pages/e.html`
|
|
|
|
});
|
|
|
|
|
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
require: 'function',
|
|
|
|
module: 'object',
|
|
|
|
process: 'object',
|
|
|
|
Buffer: 'function'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('ignores empty values', async () => {
|
|
|
|
const webview = new WebView();
|
|
|
|
|
|
|
|
for (const emptyValue of ['', null, undefined]) {
|
|
|
|
webview.preload = emptyValue;
|
|
|
|
expect(webview.preload).to.equal('');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('httpreferrer attribute', () => {
|
|
|
|
it('sets the referrer url', async () => {
|
|
|
|
const referrer = 'http://github.com/';
|
|
|
|
const received = await new Promise<string | undefined>((resolve, reject) => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
try {
|
|
|
|
resolve(req.headers.referer);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
} finally {
|
|
|
|
res.end();
|
|
|
|
server.close();
|
|
|
|
}
|
2023-02-20 11:30:57 +00:00
|
|
|
});
|
|
|
|
listen(server).then(({ url }) => {
|
2022-08-03 16:59:00 +00:00
|
|
|
loadWebView(w, {
|
|
|
|
httpreferrer: referrer,
|
2023-02-20 11:30:57 +00:00
|
|
|
src: url
|
2022-08-03 16:59:00 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
expect(received).to.equal(referrer);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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 loadWebViewAndWaitForMessage(w, {
|
|
|
|
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 () => {
|
|
|
|
await loadWebView(w, { src: 'about:blank' });
|
|
|
|
const result = await w.executeJavaScript(`webview.executeJavaScript(\`fetch(${JSON.stringify(blankPageUrl)}).then(() => 'ok', () => 'failed')\`)`);
|
|
|
|
expect(result).to.equal('failed');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('disables web security when set', async () => {
|
|
|
|
await loadWebView(w, { src: 'about:blank', disablewebsecurity: '' });
|
|
|
|
const result = await w.executeJavaScript(`webview.executeJavaScript(\`fetch(${JSON.stringify(blankPageUrl)}).then(() => 'ok', () => 'failed')\`)`);
|
|
|
|
expect(result).to.equal('ok');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not break node integration', async () => {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
disablewebsecurity: '',
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
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 loadWebViewAndWaitForMessage(w, {
|
|
|
|
disablewebsecurity: '',
|
|
|
|
preload: `${fixtures}/module/preload.js`,
|
|
|
|
webpreferences: 'sandbox=no',
|
|
|
|
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 loadWebViewAndWaitForMessage(w, {
|
|
|
|
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 loadWebViewAndWaitForMessage(w, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
partition: 'test2',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
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 () => {
|
|
|
|
await w.executeJavaScript('localStorage.setItem(\'test\', \'one\')');
|
|
|
|
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
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 () => {
|
|
|
|
await w.executeJavaScript('localStorage.setItem(\'test\', \'two\')');
|
|
|
|
const testValue = 'two';
|
|
|
|
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
src: `file://${fixtures}/pages/partition/one.html`
|
|
|
|
});
|
|
|
|
|
|
|
|
const parsedMessage = JSON.parse(message);
|
|
|
|
expect(parsedMessage).to.include({
|
|
|
|
testValue
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('allowpopups attribute', () => {
|
|
|
|
const generateSpecs = (description: string, webpreferences = '') => {
|
|
|
|
describe(description, () => {
|
|
|
|
it('can not open new window when not set', async () => {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
webpreferences,
|
|
|
|
src: `file://${fixtures}/pages/window-open-hide.html`
|
|
|
|
});
|
|
|
|
expect(message).to.equal('null');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can open new window when set', async () => {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
webpreferences,
|
|
|
|
allowpopups: 'on',
|
|
|
|
src: `file://${fixtures}/pages/window-open-hide.html`
|
|
|
|
});
|
|
|
|
expect(message).to.equal('window');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
generateSpecs('without sandbox');
|
|
|
|
generateSpecs('with sandbox', 'sandbox=yes');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('webpreferences attribute', () => {
|
|
|
|
it('can enable nodeintegration', async () => {
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
src: `file://${fixtures}/pages/d.html`,
|
|
|
|
webpreferences: 'nodeIntegration,contextIsolation=no'
|
|
|
|
});
|
|
|
|
|
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
require: 'function',
|
|
|
|
module: 'object',
|
|
|
|
process: 'object'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can disable web security and enable nodeintegration', async () => {
|
|
|
|
await loadWebView(w, { src: 'about:blank', webpreferences: 'webSecurity=no, nodeIntegration=yes, contextIsolation=no' });
|
|
|
|
const result = await w.executeJavaScript(`webview.executeJavaScript(\`fetch(${JSON.stringify(blankPageUrl)}).then(() => 'ok', () => 'failed')\`)`);
|
|
|
|
expect(result).to.equal('ok');
|
|
|
|
const type = await w.executeJavaScript('webview.executeJavaScript("typeof require")');
|
|
|
|
expect(type).to.equal('function');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-08-03 19:03:44 +00:00
|
|
|
|
|
|
|
describe('events', () => {
|
2022-08-05 00:20:17 +00:00
|
|
|
useRemoteContext({ webPreferences: { webviewTag: true } });
|
2022-08-03 19:03:44 +00:00
|
|
|
let w: WebContents;
|
|
|
|
before(async () => {
|
|
|
|
const window = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await window.loadURL(`file://${fixtures}/pages/blank.html`);
|
|
|
|
w = window.webContents;
|
|
|
|
});
|
|
|
|
afterEach(async () => {
|
|
|
|
await w.executeJavaScript(`{
|
2023-08-31 14:36:43 +00:00
|
|
|
for (const el of document.querySelectorAll('webview')) el.remove();
|
2022-08-03 19:03:44 +00:00
|
|
|
}`);
|
|
|
|
});
|
|
|
|
after(closeAllWindows);
|
|
|
|
|
|
|
|
describe('ipc-message event', () => {
|
|
|
|
it('emits when guest sends an ipc message to browser', async () => {
|
|
|
|
const { frameId, channel, args } = await loadWebViewAndWaitForEvent(w, {
|
|
|
|
src: `file://${fixtures}/pages/ipc-message.html`,
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no'
|
|
|
|
}, 'ipc-message');
|
|
|
|
|
|
|
|
expect(frameId).to.be.an('array').that.has.lengthOf(2);
|
|
|
|
expect(channel).to.equal('channel');
|
|
|
|
expect(args).to.deep.equal(['arg1', 'arg2']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('page-title-updated event', () => {
|
|
|
|
it('emits when title is set', async () => {
|
|
|
|
const { title, explicitSet } = await loadWebViewAndWaitForEvent(w, {
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
|
|
|
}, 'page-title-updated');
|
|
|
|
|
|
|
|
expect(title).to.equal('test');
|
|
|
|
expect(explicitSet).to.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('page-favicon-updated event', () => {
|
|
|
|
it('emits when favicon urls are received', async () => {
|
|
|
|
const { favicons } = await loadWebViewAndWaitForEvent(w, {
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
|
|
|
}, 'page-favicon-updated');
|
|
|
|
|
|
|
|
expect(favicons).to.be.an('array').of.length(2);
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
expect(favicons[0]).to.match(/^file:\/\/\/[A-Z]:\/favicon.png$/i);
|
|
|
|
} else {
|
|
|
|
expect(favicons[0]).to.equal('file:///favicon.png');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('did-redirect-navigation event', () => {
|
|
|
|
it('is emitted on redirects', async () => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
if (req.url === '/302') {
|
|
|
|
res.setHeader('Location', '/200');
|
|
|
|
res.statusCode = 302;
|
|
|
|
res.end();
|
|
|
|
} else {
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
});
|
2023-02-20 11:30:57 +00:00
|
|
|
const { url } = await listen(server);
|
2022-08-03 19:03:44 +00:00
|
|
|
defer(() => { server.close(); });
|
|
|
|
const event = await loadWebViewAndWaitForEvent(w, {
|
2023-02-20 11:30:57 +00:00
|
|
|
src: `${url}/302`
|
2022-08-03 19:03:44 +00:00
|
|
|
}, 'did-redirect-navigation');
|
|
|
|
|
2023-02-20 11:30:57 +00:00
|
|
|
expect(event.url).to.equal(`${url}/200`);
|
2022-08-03 19:03:44 +00:00
|
|
|
expect(event.isInPlace).to.be.false();
|
|
|
|
expect(event.isMainFrame).to.be.true();
|
|
|
|
expect(event.frameProcessId).to.be.a('number');
|
|
|
|
expect(event.frameRoutingId).to.be.a('number');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('will-navigate event', () => {
|
2023-03-28 14:55:41 +00:00
|
|
|
it('emits when a url that leads to outside of the page is loaded', async () => {
|
2022-08-03 19:03:44 +00:00
|
|
|
const { url } = await loadWebViewAndWaitForEvent(w, {
|
|
|
|
src: `file://${fixtures}/pages/webview-will-navigate.html`
|
|
|
|
}, 'will-navigate');
|
|
|
|
|
|
|
|
expect(url).to.equal('http://host/');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-28 14:55:41 +00:00
|
|
|
describe('will-frame-navigate event', () => {
|
|
|
|
it('emits when a link that leads to outside of the page is loaded', async () => {
|
|
|
|
const { url, isMainFrame } = await loadWebViewAndWaitForEvent(w, {
|
|
|
|
src: `file://${fixtures}/pages/webview-will-navigate.html`
|
|
|
|
}, 'will-frame-navigate');
|
|
|
|
expect(url).to.equal('http://host/');
|
|
|
|
expect(isMainFrame).to.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits when a link within an iframe, which leads to outside of the page, is loaded', async () => {
|
|
|
|
await loadWebView(w, {
|
|
|
|
src: `file://${fixtures}/pages/webview-will-navigate-in-frame.html`,
|
|
|
|
nodeIntegration: ''
|
|
|
|
});
|
|
|
|
|
|
|
|
const { url, frameProcessId, frameRoutingId } = await w.executeJavaScript(`
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
let hasFrameNavigatedOnce = false;
|
|
|
|
const webview = document.getElementById('webview');
|
|
|
|
webview.addEventListener('will-frame-navigate', ({url, isMainFrame, frameProcessId, frameRoutingId}) => {
|
|
|
|
if (isMainFrame) return;
|
|
|
|
if (hasFrameNavigatedOnce) resolve({
|
|
|
|
url,
|
|
|
|
isMainFrame,
|
|
|
|
frameProcessId,
|
|
|
|
frameRoutingId,
|
|
|
|
});
|
|
|
|
|
|
|
|
// First navigation is the initial iframe load within the <webview>
|
|
|
|
hasFrameNavigatedOnce = true;
|
|
|
|
});
|
|
|
|
webview.executeJavaScript('loadSubframe()');
|
|
|
|
});
|
|
|
|
`);
|
|
|
|
|
|
|
|
expect(url).to.equal('http://host/');
|
|
|
|
expect(frameProcessId).to.be.a('number');
|
|
|
|
expect(frameRoutingId).to.be.a('number');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-03 19:03:44 +00:00
|
|
|
describe('did-navigate event', () => {
|
|
|
|
it('emits when a url that leads to outside of the page is clicked', async () => {
|
|
|
|
const pageUrl = url.pathToFileURL(path.join(fixtures, 'pages', 'webview-will-navigate.html')).toString();
|
|
|
|
const event = await loadWebViewAndWaitForEvent(w, { src: pageUrl }, 'did-navigate');
|
|
|
|
expect(event.url).to.equal(pageUrl);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('did-navigate-in-page event', () => {
|
|
|
|
it('emits when an anchor link is clicked', async () => {
|
|
|
|
const pageUrl = url.pathToFileURL(path.join(fixtures, 'pages', 'webview-did-navigate-in-page.html')).toString();
|
|
|
|
const event = await loadWebViewAndWaitForEvent(w, { src: pageUrl }, 'did-navigate-in-page');
|
|
|
|
expect(event.url).to.equal(`${pageUrl}#test_content`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits when window.history.replaceState is called', async () => {
|
|
|
|
const { url } = await loadWebViewAndWaitForEvent(w, {
|
|
|
|
src: `file://${fixtures}/pages/webview-did-navigate-in-page-with-history.html`
|
|
|
|
}, 'did-navigate-in-page');
|
|
|
|
expect(url).to.equal('http://host/');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits when window.location.hash is changed', async () => {
|
|
|
|
const pageUrl = url.pathToFileURL(path.join(fixtures, 'pages', 'webview-did-navigate-in-page-with-hash.html')).toString();
|
|
|
|
const event = await loadWebViewAndWaitForEvent(w, { src: pageUrl }, 'did-navigate-in-page');
|
|
|
|
expect(event.url).to.equal(`${pageUrl}#test`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('close event', () => {
|
|
|
|
it('should fire when interior page calls window.close', async () => {
|
|
|
|
await loadWebViewAndWaitForEvent(w, { src: `file://${fixtures}/pages/close.html` }, 'close');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('devtools-opened event', () => {
|
|
|
|
it('should fire when webview.openDevTools() is called', async () => {
|
|
|
|
await loadWebViewAndWaitForEvent(w, {
|
|
|
|
src: `file://${fixtures}/pages/base-page.html`
|
|
|
|
}, 'dom-ready');
|
|
|
|
|
|
|
|
await w.executeJavaScript(`new Promise((resolve) => {
|
|
|
|
webview.openDevTools()
|
|
|
|
webview.addEventListener('devtools-opened', () => resolve(), {once: true})
|
|
|
|
})`);
|
2022-10-17 05:57:44 +00:00
|
|
|
await w.executeJavaScript('webview.closeDevTools()');
|
2022-08-03 19:03:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('devtools-closed event', () => {
|
|
|
|
itremote('should fire when webview.closeDevTools() is called', async (fixtures: string) => {
|
|
|
|
const webview = new WebView();
|
|
|
|
webview.src = `file://${fixtures}/pages/base-page.html`;
|
|
|
|
document.body.appendChild(webview);
|
|
|
|
await new Promise(resolve => webview.addEventListener('dom-ready', resolve, { once: true }));
|
|
|
|
|
|
|
|
webview.openDevTools();
|
|
|
|
await new Promise(resolve => webview.addEventListener('devtools-opened', resolve, { once: true }));
|
|
|
|
|
|
|
|
webview.closeDevTools();
|
|
|
|
await new Promise(resolve => webview.addEventListener('devtools-closed', resolve, { once: true }));
|
|
|
|
}, [fixtures]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('devtools-focused event', () => {
|
|
|
|
itremote('should fire when webview.openDevTools() is called', async (fixtures: string) => {
|
|
|
|
const webview = new WebView();
|
|
|
|
webview.src = `file://${fixtures}/pages/base-page.html`;
|
|
|
|
document.body.appendChild(webview);
|
|
|
|
|
|
|
|
const waitForDevToolsFocused = new Promise(resolve => webview.addEventListener('devtools-focused', resolve, { once: true }));
|
|
|
|
await new Promise(resolve => webview.addEventListener('dom-ready', resolve, { once: true }));
|
|
|
|
webview.openDevTools();
|
|
|
|
|
|
|
|
await waitForDevToolsFocused;
|
|
|
|
webview.closeDevTools();
|
|
|
|
}, [fixtures]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('dom-ready event', () => {
|
|
|
|
it('emits when document is loaded', async () => {
|
|
|
|
const server = http.createServer(() => {});
|
2023-02-20 11:30:57 +00:00
|
|
|
const { port } = await listen(server);
|
2022-08-03 19:03:44 +00:00
|
|
|
await loadWebViewAndWaitForEvent(w, {
|
|
|
|
src: `file://${fixtures}/pages/dom-ready.html?port=${port}`
|
|
|
|
}, 'dom-ready');
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('throws a custom error when an API method is called before the event is emitted', () => {
|
|
|
|
const expectedErrorMessage =
|
|
|
|
'The WebView must be attached to the DOM ' +
|
|
|
|
'and the dom-ready event emitted before this method can be called.';
|
|
|
|
const webview = new WebView();
|
|
|
|
expect(() => { webview.stop(); }).to.throw(expectedErrorMessage);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('context-menu event', () => {
|
|
|
|
it('emits when right-clicked in page', async () => {
|
|
|
|
await loadWebView(w, { src: 'about:blank' });
|
|
|
|
|
|
|
|
const { params, url } = await w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
webview.addEventListener('context-menu', (e) => resolve({...e, url: webview.getURL() }), {once: true})
|
|
|
|
// Simulate right-click to create context-menu event.
|
|
|
|
const opts = { x: 0, y: 0, button: 'right' };
|
|
|
|
webview.sendInputEvent({ ...opts, type: 'mouseDown' });
|
|
|
|
webview.sendInputEvent({ ...opts, type: 'mouseUp' });
|
|
|
|
})`);
|
|
|
|
|
|
|
|
expect(params.pageURL).to.equal(url);
|
|
|
|
expect(params.frame).to.be.undefined();
|
|
|
|
expect(params.x).to.be.a('number');
|
|
|
|
expect(params.y).to.be.a('number');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('found-in-page event', () => {
|
|
|
|
itremote('emits when a request is made', async (fixtures: string) => {
|
|
|
|
const webview = new WebView();
|
|
|
|
const didFinishLoad = new Promise(resolve => webview.addEventListener('did-finish-load', resolve, { once: true }));
|
|
|
|
webview.src = `file://${fixtures}/pages/content.html`;
|
|
|
|
document.body.appendChild(webview);
|
|
|
|
// 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();
|
|
|
|
await didFinishLoad;
|
|
|
|
|
|
|
|
const activeMatchOrdinal = [];
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const foundInPage = new Promise<any>(resolve => webview.addEventListener('found-in-page', resolve, { once: true }));
|
|
|
|
const requestId = webview.findInPage('virtual');
|
|
|
|
const event = await foundInPage;
|
|
|
|
|
|
|
|
expect(event.result.requestId).to.equal(requestId);
|
|
|
|
expect(event.result.matches).to.equal(3);
|
|
|
|
|
|
|
|
activeMatchOrdinal.push(event.result.activeMatchOrdinal);
|
|
|
|
|
|
|
|
if (event.result.activeMatchOrdinal === event.result.matches) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(activeMatchOrdinal).to.deep.equal([1, 2, 3]);
|
|
|
|
webview.stopFindInPage('clearSelection');
|
|
|
|
}, [fixtures]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('will-attach-webview event', () => {
|
|
|
|
itremote('does not emit when src is not changed', async () => {
|
|
|
|
const webview = new WebView();
|
|
|
|
document.body.appendChild(webview);
|
2023-02-23 23:53:53 +00:00
|
|
|
await setTimeout();
|
2022-08-03 19:03:44 +00:00
|
|
|
const expectedErrorMessage = '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);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports changing the web preferences', async () => {
|
|
|
|
w.once('will-attach-webview', (event, webPreferences, params) => {
|
|
|
|
params.src = `file://${path.join(fixtures, 'pages', 'c.html')}`;
|
|
|
|
webPreferences.nodeIntegration = false;
|
|
|
|
});
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
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('handler modifying params.instanceId does not break <webview>', async () => {
|
|
|
|
w.once('will-attach-webview', (event, webPreferences, params) => {
|
|
|
|
params.instanceId = null as any;
|
|
|
|
});
|
|
|
|
|
|
|
|
await loadWebViewAndWaitForMessage(w, {
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports preventing a webview from being created', async () => {
|
|
|
|
w.once('will-attach-webview', event => event.preventDefault());
|
|
|
|
|
|
|
|
await loadWebViewAndWaitForEvent(w, {
|
|
|
|
src: `file://${fixtures}/pages/c.html`
|
|
|
|
}, 'destroyed');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports removing the preload script', async () => {
|
|
|
|
w.once('will-attach-webview', (event, webPreferences, params) => {
|
|
|
|
params.src = url.pathToFileURL(path.join(fixtures, 'pages', 'webview-stripped-preload.html')).toString();
|
|
|
|
delete webPreferences.preload;
|
|
|
|
});
|
|
|
|
|
|
|
|
const message = await loadWebViewAndWaitForMessage(w, {
|
|
|
|
nodeintegration: 'yes',
|
|
|
|
preload: path.join(fixtures, 'module', 'preload-set-global.js'),
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(message).to.equal('undefined');
|
|
|
|
});
|
|
|
|
});
|
2022-08-15 08:06:02 +00:00
|
|
|
|
|
|
|
describe('media-started-playing and media-paused events', () => {
|
|
|
|
it('emits when audio starts and stops playing', async function () {
|
2023-04-04 13:48:51 +00:00
|
|
|
if (!await w.executeJavaScript('document.createElement(\'audio\').canPlayType(\'audio/wav\')')) {
|
|
|
|
return this.skip();
|
|
|
|
}
|
2022-08-15 08:06:02 +00:00
|
|
|
|
|
|
|
await loadWebView(w, { src: blankPageUrl });
|
|
|
|
|
|
|
|
// With the new autoplay policy, audio elements must be unmuted
|
|
|
|
// see https://goo.gl/xX8pDD.
|
|
|
|
await w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
webview.executeJavaScript(\`
|
|
|
|
const audio = document.createElement("audio")
|
|
|
|
audio.src = "../assets/tone.wav"
|
|
|
|
document.body.appendChild(audio);
|
|
|
|
audio.play()
|
|
|
|
\`, true)
|
|
|
|
webview.addEventListener('media-started-playing', () => resolve(), {once: true})
|
|
|
|
})`);
|
|
|
|
|
|
|
|
await w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
webview.executeJavaScript(\`
|
|
|
|
document.querySelector("audio").pause()
|
|
|
|
\`, true)
|
|
|
|
webview.addEventListener('media-paused', () => resolve(), {once: true})
|
|
|
|
})`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('methods', () => {
|
|
|
|
let w: WebContents;
|
|
|
|
before(async () => {
|
|
|
|
const window = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await window.loadURL(`file://${fixtures}/pages/blank.html`);
|
|
|
|
w = window.webContents;
|
|
|
|
});
|
|
|
|
afterEach(async () => {
|
|
|
|
await w.executeJavaScript(`{
|
2023-08-31 14:36:43 +00:00
|
|
|
for (const el of document.querySelectorAll('webview')) el.remove();
|
2022-08-15 08:06:02 +00:00
|
|
|
}`);
|
|
|
|
});
|
|
|
|
after(closeAllWindows);
|
|
|
|
|
|
|
|
describe('<webview>.reload()', () => {
|
|
|
|
it('should emit beforeunload handler', async () => {
|
|
|
|
await loadWebView(w, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
src: `file://${fixtures}/pages/beforeunload-false.html`
|
|
|
|
});
|
|
|
|
|
|
|
|
// Event handler has to be added before reload.
|
|
|
|
const channel = await w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
webview.addEventListener('ipc-message', e => resolve(e.channel))
|
|
|
|
webview.reload();
|
|
|
|
})`);
|
|
|
|
|
|
|
|
expect(channel).to.equal('onbeforeunload');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('<webview>.goForward()', () => {
|
|
|
|
useRemoteContext({ webPreferences: { webviewTag: true } });
|
|
|
|
itremote('should work after a replaced history entry', async (fixtures: string) => {
|
2023-02-17 18:32:39 +00:00
|
|
|
function waitForEvent (target: EventTarget, event: string) {
|
|
|
|
return new Promise<any>(resolve => target.addEventListener(event, resolve, { once: true }));
|
|
|
|
}
|
|
|
|
|
|
|
|
function waitForEvents (target: EventTarget, ...events: string[]) {
|
|
|
|
return Promise.all(events.map(event => waitForEvent(webview, event)));
|
|
|
|
}
|
|
|
|
|
2022-08-15 08:06:02 +00:00
|
|
|
const webview = new WebView();
|
|
|
|
|
|
|
|
webview.setAttribute('nodeintegration', 'on');
|
|
|
|
webview.setAttribute('webpreferences', 'contextIsolation=no');
|
|
|
|
webview.src = `file://${fixtures}/pages/history-replace.html`;
|
|
|
|
document.body.appendChild(webview);
|
|
|
|
|
|
|
|
{
|
2023-02-17 18:32:39 +00:00
|
|
|
const [e] = await waitForEvents(webview, 'ipc-message', 'did-stop-loading');
|
2022-08-15 08:06:02 +00:00
|
|
|
expect(e.channel).to.equal('history');
|
|
|
|
expect(e.args[0]).to.equal(1);
|
|
|
|
expect(webview.canGoBack()).to.be.false();
|
|
|
|
expect(webview.canGoForward()).to.be.false();
|
|
|
|
}
|
|
|
|
|
|
|
|
webview.src = `file://${fixtures}/pages/base-page.html`;
|
|
|
|
|
|
|
|
await new Promise<void>(resolve => webview.addEventListener('did-stop-loading', resolve, { once: true }));
|
|
|
|
|
|
|
|
expect(webview.canGoBack()).to.be.true();
|
|
|
|
expect(webview.canGoForward()).to.be.false();
|
|
|
|
|
|
|
|
webview.goBack();
|
|
|
|
|
|
|
|
{
|
2023-02-17 18:32:39 +00:00
|
|
|
const [e] = await waitForEvents(webview, 'ipc-message', 'did-stop-loading');
|
2022-08-15 08:06:02 +00:00
|
|
|
expect(e.channel).to.equal('history');
|
|
|
|
expect(e.args[0]).to.equal(2);
|
|
|
|
expect(webview.canGoBack()).to.be.false();
|
|
|
|
expect(webview.canGoForward()).to.be.true();
|
|
|
|
}
|
|
|
|
|
|
|
|
webview.goForward();
|
|
|
|
|
|
|
|
await new Promise<void>(resolve => webview.addEventListener('did-stop-loading', resolve, { once: true }));
|
|
|
|
|
|
|
|
expect(webview.canGoBack()).to.be.true();
|
|
|
|
expect(webview.canGoForward()).to.be.false();
|
|
|
|
}, [fixtures]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('<webview>.clearHistory()', () => {
|
|
|
|
it('should clear the navigation history', async () => {
|
|
|
|
await loadWebView(w, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
src: blankPageUrl
|
|
|
|
});
|
|
|
|
|
|
|
|
// Navigation must be triggered by a user gesture to make canGoBack() return true
|
|
|
|
await w.executeJavaScript('webview.executeJavaScript(`history.pushState(null, "", "foo.html")`, true)');
|
|
|
|
|
|
|
|
expect(await w.executeJavaScript('webview.canGoBack()')).to.be.true();
|
|
|
|
|
|
|
|
await w.executeJavaScript('webview.clearHistory()');
|
|
|
|
expect(await w.executeJavaScript('webview.canGoBack()')).to.be.false();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('executeJavaScript', () => {
|
|
|
|
it('can return the result of the executed script', async () => {
|
|
|
|
await loadWebView(w, {
|
|
|
|
src: 'about:blank'
|
|
|
|
});
|
|
|
|
|
|
|
|
const jsScript = "'4'+2";
|
|
|
|
const expectedResult = '42';
|
|
|
|
|
|
|
|
const result = await w.executeJavaScript(`webview.executeJavaScript(${JSON.stringify(jsScript)})`);
|
|
|
|
expect(result).to.equal(expectedResult);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports inserting CSS', async () => {
|
|
|
|
await loadWebView(w, { src: `file://${fixtures}/pages/base-page.html` });
|
|
|
|
await w.executeJavaScript('webview.insertCSS(\'body { background-repeat: round; }\')');
|
|
|
|
const result = await w.executeJavaScript('webview.executeJavaScript(\'window.getComputedStyle(document.body).getPropertyValue("background-repeat")\')');
|
|
|
|
expect(result).to.equal('round');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports removing inserted CSS', async () => {
|
|
|
|
await loadWebView(w, { src: `file://${fixtures}/pages/base-page.html` });
|
|
|
|
const key = await w.executeJavaScript('webview.insertCSS(\'body { background-repeat: round; }\')');
|
|
|
|
await w.executeJavaScript(`webview.removeInsertedCSS(${JSON.stringify(key)})`);
|
|
|
|
const result = await w.executeJavaScript('webview.executeJavaScript(\'window.getComputedStyle(document.body).getPropertyValue("background-repeat")\')');
|
|
|
|
expect(result).to.equal('repeat');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('sendInputEvent', () => {
|
|
|
|
it('can send keyboard event', async () => {
|
|
|
|
await loadWebViewAndWaitForEvent(w, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
src: `file://${fixtures}/pages/onkeyup.html`
|
|
|
|
}, 'dom-ready');
|
|
|
|
|
|
|
|
const waitForIpcMessage = w.executeJavaScript('new Promise(resolve => webview.addEventListener("ipc-message", e => resolve({...e})), {once: true})');
|
|
|
|
w.executeJavaScript(`webview.sendInputEvent({
|
|
|
|
type: 'keyup',
|
|
|
|
keyCode: 'c',
|
|
|
|
modifiers: ['shift']
|
|
|
|
})`);
|
|
|
|
|
|
|
|
const { channel, args } = await waitForIpcMessage;
|
|
|
|
expect(channel).to.equal('keyup');
|
|
|
|
expect(args).to.deep.equal(['C', 'KeyC', 67, true, false]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can send mouse event', async () => {
|
|
|
|
await loadWebViewAndWaitForEvent(w, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
src: `file://${fixtures}/pages/onmouseup.html`
|
|
|
|
}, 'dom-ready');
|
|
|
|
|
|
|
|
const waitForIpcMessage = w.executeJavaScript('new Promise(resolve => webview.addEventListener("ipc-message", e => resolve({...e})), {once: true})');
|
|
|
|
w.executeJavaScript(`webview.sendInputEvent({
|
|
|
|
type: 'mouseup',
|
|
|
|
modifiers: ['ctrl'],
|
|
|
|
x: 10,
|
|
|
|
y: 20
|
|
|
|
})`);
|
|
|
|
|
|
|
|
const { channel, args } = await waitForIpcMessage;
|
|
|
|
expect(channel).to.equal('mouseup');
|
|
|
|
expect(args).to.deep.equal([10, 20, false, true]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('<webview>.getWebContentsId', () => {
|
|
|
|
it('can return the WebContents ID', async () => {
|
|
|
|
await loadWebView(w, { src: 'about:blank' });
|
|
|
|
|
|
|
|
expect(await w.executeJavaScript('webview.getWebContentsId()')).to.be.a('number');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
ifdescribe(features.isPrintingEnabled())('<webview>.printToPDF()', () => {
|
|
|
|
it('rejects on incorrectly typed parameters', async () => {
|
|
|
|
const badTypes = {
|
|
|
|
landscape: [],
|
|
|
|
displayHeaderFooter: '123',
|
|
|
|
printBackground: 2,
|
|
|
|
scale: 'not-a-number',
|
|
|
|
pageSize: 'IAmAPageSize',
|
|
|
|
margins: 'terrible',
|
|
|
|
pageRanges: { oops: 'im-not-the-right-key' },
|
|
|
|
headerTemplate: [1, 2, 3],
|
|
|
|
footerTemplate: [4, 5, 6],
|
|
|
|
preferCSSPageSize: 'no'
|
|
|
|
};
|
|
|
|
|
|
|
|
// These will hard crash in Chromium unless we type-check
|
|
|
|
for (const [key, value] of Object.entries(badTypes)) {
|
|
|
|
const param = { [key]: value };
|
|
|
|
|
|
|
|
const src = 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E';
|
|
|
|
await loadWebView(w, { src });
|
|
|
|
await expect(w.executeJavaScript(`webview.printToPDF(${JSON.stringify(param)})`)).to.eventually.be.rejected();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can print to PDF', async () => {
|
|
|
|
const src = 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E';
|
|
|
|
await loadWebView(w, { src });
|
|
|
|
|
|
|
|
const data = await w.executeJavaScript('webview.printToPDF({})');
|
|
|
|
expect(data).to.be.an.instanceof(Uint8Array).that.is.not.empty();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('DOM events', () => {
|
|
|
|
for (const [description, sandbox] of [
|
|
|
|
['without sandbox', false] as const,
|
|
|
|
['with sandbox', true] as const
|
|
|
|
]) {
|
|
|
|
describe(description, () => {
|
|
|
|
it('emits focus event', async () => {
|
|
|
|
await loadWebViewAndWaitForEvent(w, {
|
|
|
|
src: `file://${fixtures}/pages/a.html`,
|
|
|
|
webpreferences: `sandbox=${sandbox ? 'yes' : 'no'}`
|
|
|
|
}, 'dom-ready');
|
|
|
|
|
|
|
|
// If this test fails, check if webview.focus() still works.
|
|
|
|
await w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
webview.addEventListener('focus', () => resolve(), {once: true});
|
|
|
|
webview.focus();
|
|
|
|
})`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-28 21:37:14 +00:00
|
|
|
// FIXME: This test is flaking constantly on Linux and macOS.
|
|
|
|
xdescribe('<webview>.capturePage()', () => {
|
2023-10-18 14:21:42 +00:00
|
|
|
it('returns a Promise with a NativeImage', async function () {
|
|
|
|
this.retries(5);
|
|
|
|
|
2022-08-15 08:06:02 +00:00
|
|
|
const src = 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E';
|
|
|
|
await loadWebViewAndWaitForEvent(w, { src }, 'did-stop-loading');
|
|
|
|
|
2023-10-18 14:21:42 +00:00
|
|
|
const image = await w.executeJavaScript('webview.capturePage()');
|
|
|
|
expect(image.isEmpty()).to.be.false();
|
|
|
|
|
|
|
|
// Check the 25th byte in the PNG.
|
|
|
|
// Values can be 0,2,3,4, or 6. We want 6, which is RGB + Alpha
|
|
|
|
const imgBuffer = image.toPNG();
|
|
|
|
expect(imgBuffer[25]).to.equal(6);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a Promise with a NativeImage in the renderer', async function () {
|
|
|
|
this.retries(5);
|
|
|
|
|
|
|
|
const src = 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E';
|
|
|
|
await loadWebViewAndWaitForEvent(w, { src }, 'did-stop-loading');
|
|
|
|
|
|
|
|
const byte = await w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
webview.capturePage().then(image => {
|
|
|
|
resolve(image.toPNG()[25])
|
|
|
|
});
|
|
|
|
})`);
|
|
|
|
|
|
|
|
expect(byte).to.equal(6);
|
2022-08-15 08:06:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// FIXME(zcbenz): Disabled because of moving to OOPIF webview.
|
|
|
|
xdescribe('setDevToolsWebContents() API', () => {
|
|
|
|
/*
|
|
|
|
it('sets webContents of webview as devtools', async () => {
|
|
|
|
const webview2 = new WebView();
|
|
|
|
loadWebView(webview2);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
expect(devtools.getURL().startsWith('devtools://devtools')).to.be.true();
|
|
|
|
|
|
|
|
const name = await devtools.executeJavaScript('InspectorFrontendHost.constructor.name');
|
|
|
|
document.body.removeChild(webview2);
|
|
|
|
|
|
|
|
expect(name).to.be.equal('InspectorFrontendHostImpl');
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('basic auth', () => {
|
|
|
|
let w: WebContents;
|
|
|
|
before(async () => {
|
|
|
|
const window = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await window.loadURL(`file://${fixtures}/pages/blank.html`);
|
|
|
|
w = window.webContents;
|
|
|
|
});
|
|
|
|
afterEach(async () => {
|
|
|
|
await w.executeJavaScript(`{
|
2023-08-31 14:36:43 +00:00
|
|
|
for (const el of document.querySelectorAll('webview')) el.remove();
|
2022-08-15 08:06:02 +00:00
|
|
|
}`);
|
|
|
|
});
|
|
|
|
after(closeAllWindows);
|
|
|
|
|
|
|
|
it('should authenticate with correct credentials', async () => {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
defer(() => {
|
|
|
|
server.close();
|
|
|
|
});
|
2023-02-20 11:30:57 +00:00
|
|
|
const { port } = await listen(server);
|
2022-08-15 08:06:02 +00:00
|
|
|
const e = await loadWebViewAndWaitForEvent(w, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
src: `file://${fixtures}/pages/basic-auth.html?port=${port}`
|
|
|
|
}, 'ipc-message');
|
|
|
|
expect(e.channel).to.equal(message);
|
|
|
|
});
|
2022-08-03 19:03:44 +00:00
|
|
|
});
|
2019-10-09 17:59:08 +00:00
|
|
|
});
|