Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

browser(firefox): make context close wait for sessions to finish (#3550)

Yury Semikhatsky854d755db57b
diff --git a/browser_patches/firefox/BUILD_NUMBER b/browser_patches/firefox/BUILD_NUMBER
index 01f579d081..03fb4e1920 100644
--- a/browser_patches/firefox/BUILD_NUMBER
+++ b/browser_patches/firefox/BUILD_NUMBER
@@ -1,2 +1,2 @@
-1163
-Changed: [email protected] Wed Aug 19 15:35:02 PDT 2020
+1164
+Changed: [email protected] Thu Aug 20 10:30:04 PDT 2020
diff --git a/browser_patches/firefox/juggler/TargetRegistry.js b/browser_patches/firefox/juggler/TargetRegistry.js
index cd47cabe7e..ff857eccbc 100644
--- a/browser_patches/firefox/juggler/TargetRegistry.js
+++ b/browser_patches/firefox/juggler/TargetRegistry.js
@@ -134,8 +134,7 @@ class TargetRegistry {
if (!target)
return;
target.emit('crashed');
- target.dispose();
- this.emit(TargetRegistry.Events.TargetDestroyed, target);
+ this._destroyTarget(target).catch(e => dump(`Failed to destroy target: ${e}`));
}
}, 'oop-frameloader-crashed');
@@ -182,7 +181,6 @@ class TargetRegistry {
const sessions = [];
const readyData = { sessions, target };
this.emit(TargetRegistry.Events.TargetCreated, readyData);
- sessions.forEach(session => target._initSession(session));
return {
scriptsToEvaluateOnNewDocument: browserContext ? browserContext.scriptsToEvaluateOnNewDocument : [],
bindings: browserContext ? browserContext.bindings : [],
@@ -204,10 +202,7 @@ class TargetRegistry {
const tab = event.target;
const linkedBrowser = tab.linkedBrowser;
const target = this._browserToTarget.get(linkedBrowser);
- if (target) {
- target.dispose();
- this.emit(TargetRegistry.Events.TargetDestroyed, target);
- }
+ this._destroyTarget(target).catch(e => dump(`Failed to destroy target: ${e}`));
};
Services.wm.addListener({
@@ -245,6 +240,16 @@ class TargetRegistry {
extHelperAppSvc.setDownloadInterceptor(new DownloadInterceptor(this));
}
+ async _destroyTarget(target) {
+ if (!target)
+ return;
+ const event = { pendingActivity: [], target };
+ this.emit(TargetRegistry.Events.TargetWillBeDestroyed, event);
+ await Promise.all(event.pendingActivity);
+ target.dispose();
+ this.emit(TargetRegistry.Events.TargetDestroyed, target);
+ }
+
setBrowserProxy(proxy) {
this._browserProxy = proxy;
}
@@ -400,7 +405,6 @@ class PageTarget {
}
connectSession(session) {
- this._initSession(session);
this._channel.connect('').send('attach', { sessionId: session.sessionId() });
}
@@ -415,7 +419,7 @@ class PageTarget {
});
}
- _initSession(session) {
+ initSession(session) {
const pageHandler = new PageHandler(this, session, this._channel);
const networkHandler = new NetworkHandler(this, session, this._channel);
session.registerHandler('Page', pageHandler);
@@ -738,6 +742,7 @@ function setViewportSizeForBrowser(viewportSize, browser, window) {
TargetRegistry.Events = {
TargetCreated: Symbol('TargetRegistry.Events.TargetCreated'),
+ TargetWillBeDestroyed: Symbol('TargetRegistry.Events.TargetWillBeDestroyed'),
TargetDestroyed: Symbol('TargetRegistry.Events.TargetDestroyed'),
DownloadCreated: Symbol('TargetRegistry.Events.DownloadCreated'),
DownloadFinished: Symbol('TargetRegistry.Events.DownloadFinished'),
diff --git a/browser_patches/firefox/juggler/protocol/BrowserHandler.js b/browser_patches/firefox/juggler/protocol/BrowserHandler.js
index 9b960add61..63908ecf1e 100644
--- a/browser_patches/firefox/juggler/protocol/BrowserHandler.js
+++ b/browser_patches/firefox/juggler/protocol/BrowserHandler.js
@@ -33,6 +33,7 @@ class BrowserHandler {
if (!this._shouldAttachToTarget(target))
continue;
const session = this._dispatcher.createSession();
+ target.initSession(session);
target.connectSession(session);
this._attachedSessions.set(target, session);
this._session.emitEvent('Browser.attachedToTarget', {
@@ -43,7 +44,7 @@ class BrowserHandler {
this._eventListeners = [
helper.on(this._targetRegistry, TargetRegistry.Events.TargetCreated, this._onTargetCreated.bind(this)),
- helper.on(this._targetRegistry, TargetRegistry.Events.TargetDestroyed, this._onTargetDestroyed.bind(this)),
+ helper.on(this._targetRegistry, TargetRegistry.Events.TargetWillBeDestroyed, this._onTargetWillBeDestroyed.bind(this)),
helper.on(this._targetRegistry, TargetRegistry.Events.DownloadCreated, this._onDownloadCreated.bind(this)),
helper.on(this._targetRegistry, TargetRegistry.Events.DownloadFinished, this._onDownloadFinished.bind(this)),
];
@@ -91,6 +92,7 @@ class BrowserHandler {
if (!this._shouldAttachToTarget(target))
return;
const session = this._dispatcher.createSession();
+ target.initSession(session);
this._attachedSessions.set(target, session);
this._session.emitEvent('Browser.attachedToTarget', {
sessionId: session.sessionId(),
@@ -99,7 +101,11 @@ class BrowserHandler {
sessions.push(session);
}
- async _onTargetDestroyed(target) {
+ _onTargetWillBeDestroyed({target, pendingActivity}) {
+ pendingActivity.push(this._detachFromTarget(target));
+ }
+
+ async _detachFromTarget(target) {
const session = this._attachedSessions.get(target);
if (!session)
return;
diff --git a/browser_patches/firefox/juggler/protocol/Dispatcher.js b/browser_patches/firefox/juggler/protocol/Dispatcher.js
index c90903fcb4..242d7cb8a0 100644
--- a/browser_patches/firefox/juggler/protocol/Dispatcher.js
+++ b/browser_patches/firefox/juggler/protocol/Dispatcher.js
@@ -31,7 +31,7 @@ class Dispatcher {
async destroySession(session) {
this._sessions.delete(session.sessionId());
- await session.dispose();
+ await session._dispose();
}
_dispose() {
@@ -108,7 +108,7 @@ class ProtocolSession {
this._handlers.set(domainName, handler);
}
- async dispose() {
+ async _dispose() {
const promises = [];
for (const [domainName, handler] of this._handlers) {
if (typeof handler.dispose !== 'function')