Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Browser/BrowserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Browser/BrowserContextPopupPagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

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;
use PHPUnit\Framework\TestCase;
use PlaywrightPHP\Browser\BrowserContext;
use PlaywrightPHP\Transport\TransportInterface;

#[CoversClass(BrowserContext::class)]
final class BrowserContextPopupPagesTest extends TestCase
{
public function testTracksPopupPagesViaEvents(): void
{
$transport = $this->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());
}
}