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
42 changes: 42 additions & 0 deletions src/Browser/BrowserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,48 @@ public function clearCookies(): void
]);
}

public function deleteCookie(string $name): void
{
$cookies = $this->cookies();
if (empty($cookies)) {
return;
}

$toDelete = [];
foreach ($cookies as $cookie) {
if (!isset($cookie['name']) || !is_string($cookie['name'])) {
continue;
}
if ($cookie['name'] !== $name) {
continue;
}

$domain = $cookie['domain'] ?? null;
$path = $cookie['path'] ?? null;
if (!is_string($domain) || !is_string($path)) {
continue;
}

$toDelete[] = [
'name' => $name,
'value' => '',
'domain' => $domain,
'path' => $path,
'expires' => 0,
];
}

if (empty($toDelete)) {
return;
}

$this->transport->send([
'action' => 'context.addCookies',
'contextId' => $this->contextId,
'cookies' => $toDelete,
]);
}

public function clearPermissions(): void
{
$this->transport->send([
Expand Down
5 changes: 5 additions & 0 deletions src/Browser/BrowserContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function addInitScript(string $script): void;

public function clearCookies(): void;

/**
* Delete all cookies with the given name across domain and path variants.
*/
public function deleteCookie(string $name): void;

public function clearPermissions(): void;

public function close(): void;
Expand Down
75 changes: 75 additions & 0 deletions tests/Unit/Browser/BrowserContextDeleteCookieTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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 BrowserContextDeleteCookieTest extends TestCase
{
public function testDeleteCookieSendsAddCookiesWithExpiry(): void
{
$transport = $this->createMock(TransportInterface::class);

$calledAdd = false;
$that = $this;
$transport->method('send')->willReturnCallback(function (array $payload) use (&$calledAdd, $that) {
if (($payload['action'] ?? null) === 'context.cookies') {
return [
'cookies' => [
[
'name' => 'foo',
'value' => '123',
'domain' => 'example.com',
'path' => '/',
'expires' => time() + 3600,
'httpOnly' => false,
'secure' => false,
'sameSite' => 'Lax',
],
[
'name' => 'bar',
'value' => 'x',
'domain' => 'example.com',
'path' => '/',
'expires' => time() + 3600,
'httpOnly' => false,
'secure' => false,
'sameSite' => 'Lax',
],
],
];
}

if (($payload['action'] ?? null) === 'context.addCookies') {
$that->assertArrayHasKey('cookies', $payload);
$that->assertIsArray($payload['cookies']);
$that->assertSame('foo', $payload['cookies'][0]['name']);
$that->assertSame('example.com', $payload['cookies'][0]['domain']);
$that->assertSame('/', $payload['cookies'][0]['path']);
$that->assertSame(0, $payload['cookies'][0]['expires']);
$calledAdd = true;

return [];
}

return [];
});

$context = new BrowserContext($transport, 'ctx');
$context->deleteCookie('foo');

$this->assertTrue($calledAdd, 'context.addCookies should be called to expire matching cookies');
}
}