chore(electron): move loader to be preload (#19650)
Fixes https://github.com/microsoft/playwright/issues/19648
This commit is contained in:
parent
83418aa8ab
commit
fe989d95eb
|
|
@ -108,7 +108,7 @@ export class ElectronApplication extends ChannelOwner<channels.ElectronApplicati
|
||||||
async close() {
|
async close() {
|
||||||
if (this._isClosed)
|
if (this._isClosed)
|
||||||
return;
|
return;
|
||||||
await this._channel.close();
|
await this._channel.close().catch(() => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
async waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions = {}): Promise<any> {
|
async waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions = {}): Promise<any> {
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,7 @@ export class Electron extends SdkObject {
|
||||||
controller.setLogName('browser');
|
controller.setLogName('browser');
|
||||||
return controller.run(async progress => {
|
return controller.run(async progress => {
|
||||||
let app: ElectronApplication | undefined = undefined;
|
let app: ElectronApplication | undefined = undefined;
|
||||||
const electronArguments = [require.resolve('./loader'), '--inspect=0', '--remote-debugging-port=0', options.cwd || process.cwd(), ...args];
|
const electronArguments = ['-r', require.resolve('./loader'), '--inspect=0', '--remote-debugging-port=0', ...args];
|
||||||
|
|
||||||
if (os.platform() === 'linux') {
|
if (os.platform() === 'linux') {
|
||||||
const runningAsRoot = process.geteuid && process.geteuid() === 0;
|
const runningAsRoot = process.geteuid && process.geteuid() === 0;
|
||||||
|
|
|
||||||
|
|
@ -15,17 +15,11 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const { app } = require('electron');
|
const { app } = require('electron');
|
||||||
const fs = require('fs');
|
|
||||||
const path = require('path');
|
|
||||||
const { chromiumSwitches } = require('../chromium/chromiumSwitches');
|
const { chromiumSwitches } = require('../chromium/chromiumSwitches');
|
||||||
|
|
||||||
// Command line is like:
|
// [Electron, -r, loader.js, --inspect=0, --remote-debugging-port=0, ...args]
|
||||||
// [Electron, loader.js, --inspect=0, --remote-debugging-port=0, options.cwd, app.js, ...args]
|
process.argv.splice(1, 4);
|
||||||
const appPath = path.resolve(process.argv[4], process.argv[5]);
|
|
||||||
process.argv.splice(2, 4);
|
|
||||||
process.argv[1] = appPath;
|
|
||||||
// Now it is like
|
|
||||||
// [Electron, app.js, ...args]
|
|
||||||
|
|
||||||
for (const arg of chromiumSwitches) {
|
for (const arg of chromiumSwitches) {
|
||||||
const match = arg.match(/--([^=]*)=?(.*)/)!;
|
const match = arg.match(/--([^=]*)=?(.*)/)!;
|
||||||
|
|
@ -44,20 +38,12 @@ app.emit = (event: string | symbol, ...args: any[]): boolean => {
|
||||||
return originalEmit(event, ...args);
|
return originalEmit(event, ...args);
|
||||||
};
|
};
|
||||||
|
|
||||||
app.getAppPath = () => {
|
|
||||||
if (fs.statSync(appPath).isFile())
|
|
||||||
return path.dirname(appPath);
|
|
||||||
return appPath;
|
|
||||||
};
|
|
||||||
|
|
||||||
let isReady = false;
|
let isReady = false;
|
||||||
let whenReadyCallback: (event: any) => any;
|
let whenReadyCallback: (event: any) => any;
|
||||||
const whenReadyPromise = new Promise<void>(f => whenReadyCallback = f);
|
const whenReadyPromise = new Promise<void>(f => whenReadyCallback = f);
|
||||||
app.isReady = () => isReady;
|
app.isReady = () => isReady;
|
||||||
app.whenReady = () => whenReadyPromise;
|
app.whenReady = () => whenReadyPromise;
|
||||||
|
|
||||||
require(appPath);
|
|
||||||
|
|
||||||
(globalThis as any).__playwright_run = async () => {
|
(globalThis as any).__playwright_run = async () => {
|
||||||
// Wait for app to be ready to avoid browser initialization races.
|
// Wait for app to be ready to avoid browser initialization races.
|
||||||
const event = await originalWhenReady;
|
const event = await originalWhenReady;
|
||||||
|
|
|
||||||
|
|
@ -197,12 +197,25 @@ test('should detach debugger on app-initiated exit', async ({ launchElectronApp
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should run pre-ready apis', async ({ launchElectronApp }) => {
|
test('should run pre-ready apis', async ({ launchElectronApp }) => {
|
||||||
await launchElectronApp('electron-pre-ready-app.js');
|
await launchElectronApp('electron-app-pre-ready.js');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should resolve app path for folder apps', async ({ launchElectronApp }) => {
|
test('should resolve app path for folder apps', async ({ launchElectronApp }) => {
|
||||||
const electronApp = await launchElectronApp('.');
|
const electronApp = await launchElectronApp('.');
|
||||||
const appPath = await electronApp.evaluate(async ({ app }) => app.getAppPath());
|
const appPath = await electronApp.evaluate(async ({ app }) => app.getAppPath());
|
||||||
expect(appPath).toBe(path.resolve(__dirname));
|
expect(appPath).toBe(path.resolve(__dirname));
|
||||||
await electronApp.close();
|
});
|
||||||
|
|
||||||
|
test('should return app name / version from manifest', async ({ launchElectronApp }) => {
|
||||||
|
const electronApp = await launchElectronApp('.');
|
||||||
|
const data = await electronApp.evaluate(async ({ app }) => {
|
||||||
|
return {
|
||||||
|
name: app.getName(),
|
||||||
|
version: app.getVersion(),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
expect(data).toEqual({
|
||||||
|
name: 'my-electron-app',
|
||||||
|
version: '1.0.0'
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
8
tests/electron/package.json
Normal file
8
tests/electron/package.json
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"name": "my-electron-app",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Hello World!",
|
||||||
|
"main": "main.js",
|
||||||
|
"author": "Jane Doe",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue