Skip to content

Commit 2bed312

Browse files
authored
fix(electron): emit close events in the correct order (#3111)
1 parent 30e21e0 commit 2bed312

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/server/electron.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ export class ElectronApplication extends EventEmitter {
7171
super();
7272
this._apiLogger = logger.api;
7373
this._browserContext = browser._defaultContext as CRBrowserContext;
74-
this._browserContext.on(Events.BrowserContext.Close, () => this.emit(ElectronEvents.ElectronApplication.Close));
74+
this._browserContext.on(Events.BrowserContext.Close, () => {
75+
// Emit application closed after context closed.
76+
Promise.resolve().then(() => this.emit(ElectronEvents.ElectronApplication.Close));
77+
});
7578
this._browserContext.on(Events.BrowserContext.Page, event => this._onPage(event));
7679
this._nodeConnection = nodeConnection;
7780
this._nodeSession = nodeConnection.rootSession;
@@ -125,8 +128,10 @@ export class ElectronApplication extends EventEmitter {
125128
}
126129

127130
async close() {
131+
const closed = this.waitForEvent(ElectronEvents.ElectronApplication.Close);
128132
await this.evaluate(({ app }) => app.quit());
129133
this._nodeConnection.close();
134+
await closed;
130135
}
131136

132137
async waitForEvent(event: string, optionsOrPredicate: types.WaitForEventOptions = {}): Promise<any> {
@@ -188,10 +193,7 @@ export class Electron {
188193
cwd: options.cwd,
189194
tempDirectories: [],
190195
attemptToGracefullyClose: () => app!.close(),
191-
onExit: (exitCode, signal) => {
192-
if (app)
193-
app.emit(ElectronEvents.ElectronApplication.Close, exitCode, signal);
194-
},
196+
onExit: (exitCode, signal) => {},
195197
});
196198

197199
const nodeMatch = await waitForLine(progress, launchedProcess, launchedProcess.stderr, /^Debugger listening on (ws:\/\/.*)$/);

test/electron/electron.jest.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ registerFixture('window', async ({application}, test) => {
4141
});
4242

4343
describe.skip(!CHROMIUM)('Electron', function() {
44+
it('should fire close event', async ({ playwright }) => {
45+
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
46+
const application = await playwright.electron.launch(electronPath, {
47+
args: [path.join(__dirname, 'testApp.js')],
48+
});
49+
const events = [];
50+
application.on('close', () => events.push('application'));
51+
application.context().on('close', () => events.push('context'));
52+
await application.close();
53+
expect(events.join('|')).toBe('context|application');
54+
// Give it some time to fire more events - there should not be any.
55+
await new Promise(f => setTimeout(f, 1000));
56+
expect(events.join('|')).toBe('context|application');
57+
});
4458
it('should script application', async ({ application }) => {
4559
const appPath = await application.evaluate(async ({ app }) => app.getAppPath());
4660
expect(appPath).toContain('electron');

0 commit comments

Comments
 (0)