Skip to content
Open
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
9 changes: 3 additions & 6 deletions bin/playwright-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
65 changes: 65 additions & 0 deletions tests/Integration/Browser/BrowserResourceCleanupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Playwright\Tests\Integration\Browser;

use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Playwright\Exception\PlaywrightException;
use Playwright\Testing\PlaywrightTestCaseTrait;

#[CoversNothing]
final class BrowserResourceCleanupTest extends TestCase
{
use PlaywrightTestCaseTrait;

protected function setUp(): void
{
$this->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('<title>Still open</title>');

$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();
}
}
}
Loading