diff --git a/src/Browser/BrowserContext.php b/src/Browser/BrowserContext.php index 9c42670..31ebcf8 100644 --- a/src/Browser/BrowserContext.php +++ b/src/Browser/BrowserContext.php @@ -82,6 +82,25 @@ public function dispatchEvent(string $eventName, array $params): void $route->continue(); } + // Track popup/new page lifecycle if server emits such events. + if (in_array($eventName, ['page', 'popup', 'pageCreated'], true)) { + $pageId = $params['pageId'] ?? null; + if (is_string($pageId) && !isset($this->pages[$pageId])) { + $this->pages[$pageId] = new Page($this->transport, $this, $pageId, $this->config); + } + + return; + } + + if (in_array($eventName, ['pageClosed', 'page-closed'], true)) { + $pageId = $params['pageId'] ?? null; + if (is_string($pageId)) { + unset($this->pages[$pageId]); + } + + return; + } + if ('binding' === $eventName) { $bindingName = $params['name']; if (is_string($bindingName) && isset($this->bindings[$bindingName]) && is_callable($this->bindings[$bindingName])) { diff --git a/tests/Unit/Browser/BrowserContextPopupPagesTest.php b/tests/Unit/Browser/BrowserContextPopupPagesTest.php new file mode 100644 index 0000000..ceabdab --- /dev/null +++ b/tests/Unit/Browser/BrowserContextPopupPagesTest.php @@ -0,0 +1,36 @@ +createMock(TransportInterface::class); + $context = new BrowserContext($transport, 'ctx1'); + + $this->assertCount(0, $context->pages()); + + // Simulate server emitting a popup event with a pageId + $context->dispatchEvent('popup', ['pageId' => 'p-123']); + $this->assertCount(1, $context->pages()); + + // Simulate server notifying page close + $context->dispatchEvent('pageClosed', ['pageId' => 'p-123']); + $this->assertCount(0, $context->pages()); + } +}