mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
fix(electron): emit close events in the correct order (#3111)
This commit is contained in:
parent
30e21e0bee
commit
2bed312911
@ -71,7 +71,10 @@ export class ElectronApplication extends EventEmitter {
|
|||||||
super();
|
super();
|
||||||
this._apiLogger = logger.api;
|
this._apiLogger = logger.api;
|
||||||
this._browserContext = browser._defaultContext as CRBrowserContext;
|
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._browserContext.on(Events.BrowserContext.Page, event => this._onPage(event));
|
||||||
this._nodeConnection = nodeConnection;
|
this._nodeConnection = nodeConnection;
|
||||||
this._nodeSession = nodeConnection.rootSession;
|
this._nodeSession = nodeConnection.rootSession;
|
||||||
@ -125,8 +128,10 @@ export class ElectronApplication extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async close() {
|
async close() {
|
||||||
|
const closed = this.waitForEvent(ElectronEvents.ElectronApplication.Close);
|
||||||
await this.evaluate(({ app }) => app.quit());
|
await this.evaluate(({ app }) => app.quit());
|
||||||
this._nodeConnection.close();
|
this._nodeConnection.close();
|
||||||
|
await closed;
|
||||||
}
|
}
|
||||||
|
|
||||||
async waitForEvent(event: string, optionsOrPredicate: types.WaitForEventOptions = {}): Promise<any> {
|
async waitForEvent(event: string, optionsOrPredicate: types.WaitForEventOptions = {}): Promise<any> {
|
||||||
@ -188,10 +193,7 @@ export class Electron {
|
|||||||
cwd: options.cwd,
|
cwd: options.cwd,
|
||||||
tempDirectories: [],
|
tempDirectories: [],
|
||||||
attemptToGracefullyClose: () => app!.close(),
|
attemptToGracefullyClose: () => app!.close(),
|
||||||
onExit: (exitCode, signal) => {
|
onExit: (exitCode, signal) => {},
|
||||||
if (app)
|
|
||||||
app.emit(ElectronEvents.ElectronApplication.Close, exitCode, signal);
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const nodeMatch = await waitForLine(progress, launchedProcess, launchedProcess.stderr, /^Debugger listening on (ws:\/\/.*)$/);
|
const nodeMatch = await waitForLine(progress, launchedProcess, launchedProcess.stderr, /^Debugger listening on (ws:\/\/.*)$/);
|
||||||
|
@ -41,6 +41,20 @@ registerFixture('window', async ({application}, test) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe.skip(!CHROMIUM)('Electron', function() {
|
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 }) => {
|
it('should script application', async ({ application }) => {
|
||||||
const appPath = await application.evaluate(async ({ app }) => app.getAppPath());
|
const appPath = await application.evaluate(async ({ app }) => app.getAppPath());
|
||||||
expect(appPath).toContain('electron');
|
expect(appPath).toContain('electron');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user