From b795c2353e639eae57d3349b4a4deef749eaec18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Sun, 14 Sep 2025 22:26:57 +0200 Subject: [PATCH 1/3] Fix BrowserContexxt popup tracking --- src/Browser/BrowserContext.php | 19 ++++++++++++ .../Browser/BrowserContextPopupPagesTest.php | 31 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tests/Unit/Browser/BrowserContextPopupPagesTest.php 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..2a5342c --- /dev/null +++ b/tests/Unit/Browser/BrowserContextPopupPagesTest.php @@ -0,0 +1,31 @@ +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()); + } +} + From 40043c0c112d36a7af7fcf416d36c74982cd31e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Sun, 14 Sep 2025 22:29:33 +0200 Subject: [PATCH 2/3] Apply suggestion from @smnandre --- tests/Unit/Browser/BrowserContextPopupPagesTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Unit/Browser/BrowserContextPopupPagesTest.php b/tests/Unit/Browser/BrowserContextPopupPagesTest.php index 2a5342c..a63c7d7 100644 --- a/tests/Unit/Browser/BrowserContextPopupPagesTest.php +++ b/tests/Unit/Browser/BrowserContextPopupPagesTest.php @@ -28,4 +28,3 @@ public function testTracksPopupPagesViaEvents(): void $this->assertCount(0, $context->pages()); } } - From 31a0adf4c80de25867461a2e2207938fa432032b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Sun, 14 Sep 2025 22:31:15 +0200 Subject: [PATCH 3/3] Update BrowserContextPopupPagesTest.php --- tests/Unit/Browser/BrowserContextPopupPagesTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Unit/Browser/BrowserContextPopupPagesTest.php b/tests/Unit/Browser/BrowserContextPopupPagesTest.php index a63c7d7..ceabdab 100644 --- a/tests/Unit/Browser/BrowserContextPopupPagesTest.php +++ b/tests/Unit/Browser/BrowserContextPopupPagesTest.php @@ -2,6 +2,12 @@ declare(strict_types=1); +/* + * This file is part of the playwright-php/playwright package. + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + namespace PlaywrightPHP\Tests\Unit\Browser; use PHPUnit\Framework\Attributes\CoversClass;