Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/playwright-core/src/server/electron/loader.ts
Original file line number Diff line number Diff line change
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();
Copy link
Contributor

@andersk andersk Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has caused a regression for apps using Electron APIs that need to be called before the ready event fires, such as protocol.registerSchemesAsPrivileged and app.disableHardwareAcceleration.

We need to give the user a chance to run synchronous code before the ready event fires for real—the Electron API is not fooled by our attempt to fake it.


// 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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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