Skip to content

Commit

Permalink
fix(electron): consistently emit ready event after app is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Nov 21, 2022
1 parent 8ad3bc7 commit 3b42f5b
Show file tree
Hide file tree
Showing 2 changed files with 38 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);
};
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 3b42f5b

Please sign in to comment.