Skip to content

Commit

Permalink
fix(electron): consistently emit ready event after app is loaded (#18972
Browse files Browse the repository at this point in the history
)

Fixes: #18928
  • Loading branch information
pavelfeldman committed Nov 21, 2022
1 parent c0daeaa commit b5d7566
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/playwright-core/src/server/electron/loader.ts
Expand Up @@ -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);
};
12 changes: 12 additions & 0 deletions tests/electron/electron-app-ready-event.js
@@ -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'));
18 changes: 18 additions & 0 deletions tests/electron/electron-app.spec.ts
Expand Up @@ -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));
Expand Down

0 comments on commit b5d7566

Please sign in to comment.