diff --git a/bin/playwright-server.js b/bin/playwright-server.js index 05fde22..4613d0d 100644 --- a/bin/playwright-server.js +++ b/bin/playwright-server.js @@ -317,12 +317,9 @@ class PlaywrightServer extends BaseHandler { cleanupBrowserResources(browserId) { for (const [contextId, contextInfo] of this.contexts.entries()) { - if (contextInfo.browserId === browserId) this.contexts.delete(contextId); - } - for (const [pageId, page] of this.pages.entries()) { - try { - if (page.context().browser() === this.browsers.get(browserId)) this.pages.delete(pageId); - } catch { this.pages.delete(pageId); } + if (contextInfo.browserId === browserId) { + this.contextHandler.cleanupContextResources(contextId); + } } } diff --git a/tests/Integration/Browser/BrowserResourceCleanupTest.php b/tests/Integration/Browser/BrowserResourceCleanupTest.php new file mode 100644 index 0000000..541e4d0 --- /dev/null +++ b/tests/Integration/Browser/BrowserResourceCleanupTest.php @@ -0,0 +1,65 @@ +setUpPlaywright(); + } + + protected function tearDown(): void + { + $this->tearDownPlaywright(); + } + + public function testClosingABrowserRemovesItsPagesWithoutAffectingAnotherBrowser(): void + { + $browser = $this->playwright->chromium()->launch(); + + try { + $context = $browser->newContext(); + $pages = [$browser->newPage(), $context->newPage(), $context->newPage()]; + $this->page->setContent('Still open'); + + $browser->close(); + $browser->close(); + + foreach ($pages as $page) { + try { + $page->title(); + $this->fail('The closed browser\'s page must no longer be registered in the bridge.'); + } catch (PlaywrightException $e) { + $this->assertStringContainsString('Page not found:', $e->getMessage()); + } + } + + $this->assertTrue($this->browser->isConnected()); + $this->assertSame('Still open', $this->page->title()); + $this->assertSame('about:blank', $this->context->newPage()->url()); + } finally { + $browser->close(); + } + } +}