diff --git a/lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json b/lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json index 901790ce9..2f5fa6b58 100644 --- a/lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json +++ b/lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json @@ -975,21 +975,6 @@ "FAIL" ] }, - { - "comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one", - "testIdPattern": "[page.spec] *Page.Events.Close*", - "platforms": [ - "darwin", - "linux", - "win32" - ], - "parameters": [ - "webDriverBiDi" - ], - "expectations": [ - "FAIL" - ] - }, { "comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one", "testIdPattern": "[page.spec] *Page.Events.Console*", diff --git a/lib/PuppeteerSharp/Bidi/BidiPage.cs b/lib/PuppeteerSharp/Bidi/BidiPage.cs index 5c3d70967..4b91e0218 100644 --- a/lib/PuppeteerSharp/Bidi/BidiPage.cs +++ b/lib/PuppeteerSharp/Bidi/BidiPage.cs @@ -547,5 +547,9 @@ private async Task GoAsync(int delta, NavigationOptions options) private void Initialize() { + BidiMainFrame.BrowsingContext.Closed += (_, _) => + { + OnClose(); + }; } } diff --git a/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs b/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs index f5f9cb2e0..aabe1c555 100644 --- a/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs +++ b/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs @@ -71,7 +71,7 @@ private BrowsingContext(UserContext userContext, BrowsingContext parent, string public string Url { get; private set; } - public bool IsClosed => _reason != null; + public bool IsClosed { get; private set; } public Session Session => UserContext.Browser.Session; @@ -107,6 +107,13 @@ public static BrowsingContext From(UserContext userContext, BrowsingContext pare public void Dispose() { + if (IsClosed) + { + return; + } + + IsClosed = true; + _reason ??= "Browser was disconnected, probably because the session ended."; OnClosed(_reason); foreach (var context in _children.Values) @@ -223,7 +230,7 @@ private void Initialize() Session.BrowsingContextContextDestroyed += (_, args) => { - if (args.UserContextId != Id) + if (args.BrowsingContextId != Id) { return; }