fix(electron): emit close events in the correct order (#3111)

This commit is contained in:
Dmitry Gozman 2020-07-23 11:02:43 -07:00 committed by GitHub
parent 30e21e0bee
commit 2bed312911
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -71,7 +71,10 @@ export class ElectronApplication extends EventEmitter {
super();
this._apiLogger = logger.api;
this._browserContext = browser._defaultContext as CRBrowserContext;
this._browserContext.on(Events.BrowserContext.Close, () => this.emit(ElectronEvents.ElectronApplication.Close));
this._browserContext.on(Events.BrowserContext.Close, () => {
// Emit application closed after context closed.
Promise.resolve().then(() => this.emit(ElectronEvents.ElectronApplication.Close));
});
this._browserContext.on(Events.BrowserContext.Page, event => this._onPage(event));
this._nodeConnection = nodeConnection;
this._nodeSession = nodeConnection.rootSession;
@ -125,8 +128,10 @@ export class ElectronApplication extends EventEmitter {
}
async close() {
const closed = this.waitForEvent(ElectronEvents.ElectronApplication.Close);
await this.evaluate(({ app }) => app.quit());
this._nodeConnection.close();
await closed;
}
async waitForEvent(event: string, optionsOrPredicate: types.WaitForEventOptions = {}): Promise<any> {
@ -188,10 +193,7 @@ export class Electron {
cwd: options.cwd,
tempDirectories: [],
attemptToGracefullyClose: () => app!.close(),
onExit: (exitCode, signal) => {
if (app)
app.emit(ElectronEvents.ElectronApplication.Close, exitCode, signal);
},
onExit: (exitCode, signal) => {},
});
const nodeMatch = await waitForLine(progress, launchedProcess, launchedProcess.stderr, /^Debugger listening on (ws:\/\/.*)$/);

View File

@ -41,6 +41,20 @@ registerFixture('window', async ({application}, test) => {
});
describe.skip(!CHROMIUM)('Electron', function() {
it('should fire close event', async ({ playwright }) => {
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
const application = await playwright.electron.launch(electronPath, {
args: [path.join(__dirname, 'testApp.js')],
});
const events = [];
application.on('close', () => events.push('application'));
application.context().on('close', () => events.push('context'));
await application.close();
expect(events.join('|')).toBe('context|application');
// Give it some time to fire more events - there should not be any.
await new Promise(f => setTimeout(f, 1000));
expect(events.join('|')).toBe('context|application');
});
it('should script application', async ({ application }) => {
const appPath = await application.evaluate(async ({ app }) => app.getAppPath());
expect(appPath).toContain('electron');