diff --git a/src/Api/Concerns/MakesCookieAssertions.php b/src/Api/Concerns/MakesCookieAssertions.php new file mode 100644 index 00000000..0b01430e --- /dev/null +++ b/src/Api/Concerns/MakesCookieAssertions.php @@ -0,0 +1,74 @@ +page->cookies(); + + expect($cookies) + ->toHaveKey($key, $value, sprintf( + 'Expected cookie [%s] to be present on the page initially with the url [%s], but it was not found', + $key, + $this->initialUrl, + )); + + return $this; + } + + /** + * Asserts the page does not have a specific cookie. + * + * @param string $key The name of the cookie. + * @param mixed $value The value of the cookie. + */ + public function assertCookieMissing(string $key, mixed $value = new Any()): Webpage + { + $cookies = $this->page->cookies(); + + expect($cookies) + ->not()->toHaveKey($key, $value, sprintf( + 'Expected cookie [%s] to not be present on the page initially with the url [%s], but it was found', + $key, + $this->initialUrl, + )); + + return $this; + } + + /** + * Asserts there are no cookies on the page. + */ + public function assertNoCookies(): Webpage + { + $cookies = $this->page->cookies(); + + expect($cookies)->toBeEmpty(sprintf( + 'Expected no cookies on the page initially with the url [%s], but found %s: %s', + $this->initialUrl, + count($cookies), + implode(', ', array_keys($cookies)), + )); + + return $this; + } +} diff --git a/src/Api/Webpage.php b/src/Api/Webpage.php index 37271685..2312fc8e 100644 --- a/src/Api/Webpage.php +++ b/src/Api/Webpage.php @@ -19,6 +19,7 @@ final class Webpage Concerns\InteractsWithToolbar, Concerns\InteractsWithViewPort, Concerns\MakesConsoleAssertions, + Concerns\MakesCookieAssertions, Concerns\MakesElementAssertions, Concerns\MakesScreenshotAssertions, Concerns\MakesUrlAssertions; diff --git a/src/Playwright/Page.php b/src/Playwright/Page.php index d97f3db3..34ffca83 100644 --- a/src/Playwright/Page.php +++ b/src/Playwright/Page.php @@ -463,6 +463,29 @@ public function consoleLogs(): array return $consoleLogs; } + /** + * Get the cookies from the page, if any. + * + * @return array + */ + public function cookies(): array + { + $cookieString = $this->evaluate('document.cookie || []'); + + /** @var array $cookies */ + $cookies = []; + $cookiePairs = explode(';', is_string($cookieString) ? $cookieString : ''); + + foreach ($cookiePairs as $cookie) { + $value = explode('=', $cookie, 2); + if (count($value) >= 2) { + $cookies[mb_trim($value[0])] = mb_trim($value[1]); + } + } + + return $cookies; + } + /** * Get the broken images from the page, if any. * diff --git a/tests/Browser/Webpage/AssertCookieMissingTest.php b/tests/Browser/Webpage/AssertCookieMissingTest.php new file mode 100644 index 00000000..e12e54ce --- /dev/null +++ b/tests/Browser/Webpage/AssertCookieMissingTest.php @@ -0,0 +1,57 @@ + ' + + '); + + $page = visit('/'); + + $page->assertCookieMissing('nonexistent'); +}); + +it('may assert cookie is missing with value', function (): void { + Route::get('/', fn (): string => ' + + '); + + $page = visit('/'); + + $page->assertCookieMissing('test', 5); +}); + +it('may fail when asserting cookie is missing but it exists', function (): void { + Route::get('/', fn (): string => ' + + '); + + $page = visit('/'); + + $page->assertCookieMissing('test'); +})->throws(ExpectationFailedException::class, 'Expected cookie'); + +it('may fail when asserting cookie is missing with value but it exists with that value', function (): void { + Route::get('/', fn (): string => ' + + '); + + $page = visit('/'); + + $page->assertCookieMissing('test', 1); +})->throws(ExpectationFailedException::class, 'Expected cookie'); diff --git a/tests/Browser/Webpage/AssertHasCookieTest.php b/tests/Browser/Webpage/AssertHasCookieTest.php new file mode 100644 index 00000000..19f2b507 --- /dev/null +++ b/tests/Browser/Webpage/AssertHasCookieTest.php @@ -0,0 +1,57 @@ + ' + + '); + + $page = visit('/'); + + $page->assertHasCookie('test'); +}); + +it('may assert cookie exists with value', function (): void { + Route::get('/', fn (): string => ' + + '); + + $page = visit('/'); + + $page->assertHasCookie('test', 1); +}); + +it('may fail when asserting cookie exists but it does not', function (): void { + Route::get('/', fn (): string => ' + + '); + + $page = visit('/'); + + $page->assertHasCookie('foobar'); +})->throws(ExpectationFailedException::class, 'but it was not found'); + +it('may fail when asserting cookie exists with value but value does not match', function (): void { + Route::get('/', fn (): string => ' + + '); + + $page = visit('/'); + + $page->assertHasCookie('test', 2); +})->throws(ExpectationFailedException::class, 'but it was not found'); diff --git a/tests/Browser/Webpage/AssertNoCookiesTest.php b/tests/Browser/Webpage/AssertNoCookiesTest.php new file mode 100644 index 00000000..4703f62f --- /dev/null +++ b/tests/Browser/Webpage/AssertNoCookiesTest.php @@ -0,0 +1,26 @@ + '

Page with no cookies

'); + + $page = visit('/'); + + $page->assertNoCookies(); +}); + +it('may fail when asserting no cookies but there are cookies', function (): void { + Route::get('/', fn (): string => ' + + '); + + $page = visit('/'); + + $page->assertNoCookies(); +})->throws(ExpectationFailedException::class, 'but found 2');