diff --git a/bin/lib/handlers.js b/bin/lib/handlers.js index 9bb5ee6..67721ff 100644 --- a/bin/lib/handlers.js +++ b/bin/lib/handlers.js @@ -12,7 +12,7 @@ class ContextHandler extends BaseHandler { setOffline: () => context.setOffline(!!command.offline), setGeolocation: () => context.setGeolocation(command.geolocation), addCookies: () => context.addCookies(command.cookies), - clearCookies: () => context.clearCookies(), + clearCookies: () => context.clearCookies(command.options || {}), grantPermissions: () => context.grantPermissions(command.permissions, command.origin ? { origin: command.origin } : undefined), clearPermissions: () => context.clearPermissions(), startTracing: () => context.tracing.start(command.options || {}), diff --git a/bin/package.json b/bin/package.json index aa19725..c0c1afb 100644 --- a/bin/package.json +++ b/bin/package.json @@ -4,7 +4,7 @@ "description": "Playwright server for PHP", "main": "playwright-server.js", "dependencies": { - "playwright": "^1.40.0" + "playwright": "^1.58.2" }, "scripts": { "install-browsers": "npx playwright install", diff --git a/src/Browser/BrowserContext.php b/src/Browser/BrowserContext.php index 8c63f98..3bf034b 100644 --- a/src/Browser/BrowserContext.php +++ b/src/Browser/BrowserContext.php @@ -220,11 +220,15 @@ public function addInitScript(string $script): void ]); } - public function clearCookies(): void + /** + * @param array $options + */ + public function clearCookies(array $options = []): void { $this->transport->send([ 'action' => 'context.clearCookies', 'contextId' => $this->contextId, + 'options' => $options, ]); } diff --git a/src/Browser/BrowserContextInterface.php b/src/Browser/BrowserContextInterface.php index 4df6982..9f7ae1f 100644 --- a/src/Browser/BrowserContextInterface.php +++ b/src/Browser/BrowserContextInterface.php @@ -27,7 +27,10 @@ public function addCookies(array $cookies): void; public function addInitScript(string $script): void; - public function clearCookies(): void; + /** + * @param array $options + */ + public function clearCookies(array $options = []): void; /** * Delete all cookies with the given name across domain and path variants. diff --git a/tests/Functional/Auth/CookieTest.php b/tests/Functional/Auth/CookieTest.php index 0d3ee4e..5c01785 100644 --- a/tests/Functional/Auth/CookieTest.php +++ b/tests/Functional/Auth/CookieTest.php @@ -99,26 +99,24 @@ public function testCanGetCookies(): void self::assertSame('my_value', $myCookie['value']); } - public function testCanDeleteCookies(): void + public function testCanDeleteSingleCookie(): void { $this->goto('/cookies.html'); $this->context->addCookies([ - [ - 'name' => 'to_delete', - 'value' => 'delete_me', - 'url' => $this->getBaseUrl(), - ], + ['name' => 'to_delete', 'value' => 'delete_me', 'url' => $this->getBaseUrl()], + ['name' => 'to_keep', 'value' => 'keep_me', 'url' => $this->getBaseUrl()], ]); $cookies = $this->context->cookies(); $cookieNames = \array_column($cookies, 'name'); self::assertContains('to_delete', $cookieNames); - $this->context->clearCookies(); + $this->context->clearCookies(['name' => 'to_delete']); $cookiesAfter = $this->context->cookies(); - self::assertEmpty($cookiesAfter); + self::assertCount(1, $cookiesAfter); + self::assertSame('keep_me', $cookiesAfter[0]['value']); } public function testCanSetCookieWithExpiration(): void diff --git a/tests/Unit/Browser/BrowserContextTest.php b/tests/Unit/Browser/BrowserContextTest.php index 607c821..e497225 100644 --- a/tests/Unit/Browser/BrowserContextTest.php +++ b/tests/Unit/Browser/BrowserContextTest.php @@ -147,11 +147,26 @@ public function testClearCookies(): void ->with([ 'action' => 'context.clearCookies', 'contextId' => 'context_1', + 'options' => [], ]); $this->context->clearCookies(); } + public function testClearCookiesWithOptions(): void + { + $this->mockTransport + ->expects($this->once()) + ->method('send') + ->with([ + 'action' => 'context.clearCookies', + 'contextId' => 'context_1', + 'options' => ['name' => 'name_1'], + ]); + + $this->context->clearCookies(['name' => 'name_1']); + } + public function testClearPermissions(): void { $this->mockTransport