fix(electron): consistently emit ready event after app is loaded (#18972)

Fixes: https://github.com/microsoft/playwright/issues/18928
This commit is contained in:
Pavel Feldman 2022-11-21 15:13:53 -08:00 committed by GitHub
parent c0daeaa291
commit b5d756686d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 1 deletions

View File

@ -32,6 +32,25 @@ for (const arg of chromiumSwitches) {
app.getAppPath = () => path.dirname(appPath);
}
(globalThis as any).__playwright_run = () => {
let launchInfoEventPayload: any;
app.on('ready', launchInfo => launchInfoEventPayload = launchInfo);
(globalThis as any).__playwright_run = async () => {
// Wait for app to be ready to avoid browser initialization races.
await app.whenReady();
// Override isReady pipeline.
let isReady = false;
let whenReadyCallback: () => void;
const whenReadyPromise = new Promise<void>(f => whenReadyCallback = f);
app.isReady = () => isReady;
app.whenReady = () => whenReadyPromise;
require(appPath);
// Trigger isReady.
isReady = true;
whenReadyCallback!();
app.emit('will-finish-launching');
app.emit('ready', launchInfoEventPayload);
};

View File

@ -0,0 +1,12 @@
const { app } = require('electron');
globalThis.__playwrightLog = [];
globalThis.__playwrightLog.push(`isReady == ${app.isReady()}`);
app.whenReady().then(() => {
globalThis.__playwrightLog.push(`whenReady resolved`);
globalThis.__playwrightLog.push(`isReady == ${app.isReady()}`);
});
app.on('will-finish-launching', () => globalThis.__playwrightLog.push('will-finish-launching fired'));
app.on('ready', () => globalThis.__playwrightLog.push('ready fired'));

View File

@ -33,6 +33,24 @@ test('should fire close event', async ({ playwright }) => {
expect(events.join('|')).toBe('context|application');
});
test('should dispatch ready event', async ({ playwright }) => {
const electronApp = await playwright._electron.launch({
args: [path.join(__dirname, 'electron-app-ready-event.js')],
});
try {
const events = await electronApp.evaluate(() => globalThis.__playwrightLog);
expect(events).toEqual([
'isReady == false',
'will-finish-launching fired',
'ready fired',
'whenReady resolved',
'isReady == true',
]);
} finally {
await electronApp.close();
}
});
test('should script application', async ({ electronApp }) => {
const appPath = await electronApp.evaluate(async ({ app }) => app.getAppPath());
expect(appPath).toBe(path.resolve(__dirname));