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
74 changes: 74 additions & 0 deletions src/Api/Concerns/MakesCookieAssertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Pest\Browser\Api\Concerns;

use Pest\Browser\Api\Webpage;
use Pest\Browser\Support\AccessibilityFormatter;
use Pest\Matchers\Any;

/**
* @mixin Webpage
*
* @phpstan-import-type Violations from AccessibilityFormatter
*/
trait MakesCookieAssertions
{
/**
* Asserts the page has a cookie.
*
* @param string $key The name of the cookie.
* @param mixed $value The value of the cookie.
*/
public function assertHasCookie(string $key, mixed $value = new Any()): Webpage
{
$cookies = $this->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;
}
}
1 change: 1 addition & 0 deletions src/Api/Webpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class Webpage
Concerns\InteractsWithToolbar,
Concerns\InteractsWithViewPort,
Concerns\MakesConsoleAssertions,
Concerns\MakesCookieAssertions,
Concerns\MakesElementAssertions,
Concerns\MakesScreenshotAssertions,
Concerns\MakesUrlAssertions;
Expand Down
23 changes: 23 additions & 0 deletions src/Playwright/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,29 @@ public function consoleLogs(): array
return $consoleLogs;
}

/**
* Get the cookies from the page, if any.
*
* @return array<string, string>
*/
public function cookies(): array
{
$cookieString = $this->evaluate('document.cookie || []');

/** @var array<string, string> $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.
*
Expand Down
57 changes: 57 additions & 0 deletions tests/Browser/Webpage/AssertCookieMissingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\ExpectationFailedException;

it('may assert cookie is missing', function (): void {
Route::get('/', fn (): string => '
<script>
document.cookie = "test=1";
document.cookie = "test2=2";
</script>
');

$page = visit('/');

$page->assertCookieMissing('nonexistent');
});

it('may assert cookie is missing with value', function (): void {
Route::get('/', fn (): string => '
<script>
document.cookie = "test=1";
document.cookie = "test2=2";
</script>
');

$page = visit('/');

$page->assertCookieMissing('test', 5);
});

it('may fail when asserting cookie is missing but it exists', function (): void {
Route::get('/', fn (): string => '
<script>
document.cookie = "test=1";
document.cookie = "test2=2";
</script>
');

$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 => '
<script>
document.cookie = "test=1";
document.cookie = "test2=2";
</script>
');

$page = visit('/');

$page->assertCookieMissing('test', 1);
})->throws(ExpectationFailedException::class, 'Expected cookie');
57 changes: 57 additions & 0 deletions tests/Browser/Webpage/AssertHasCookieTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\ExpectationFailedException;

it('may assert cookie exists', function (): void {
Route::get('/', fn (): string => '
<script>
document.cookie = "test=1";
document.cookie = "test2=2";
</script>
');

$page = visit('/');

$page->assertHasCookie('test');
});

it('may assert cookie exists with value', function (): void {
Route::get('/', fn (): string => '
<script>
document.cookie = "test=1";
document.cookie = "test2=2";
</script>
');

$page = visit('/');

$page->assertHasCookie('test', 1);
});

it('may fail when asserting cookie exists but it does not', function (): void {
Route::get('/', fn (): string => '
<script>
document.cookie = "test=1";
document.cookie = "test2=2";
</script>
');

$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 => '
<script>
document.cookie = "test=1";
document.cookie = "test2=2";
</script>
');

$page = visit('/');

$page->assertHasCookie('test', 2);
})->throws(ExpectationFailedException::class, 'but it was not found');
26 changes: 26 additions & 0 deletions tests/Browser/Webpage/AssertNoCookiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\ExpectationFailedException;

it('may assert there are no cookies', function (): void {
Route::get('/', fn (): string => '<h1>Page with no cookies</h1>');

$page = visit('/');

$page->assertNoCookies();
});

it('may fail when asserting no cookies but there are cookies', function (): void {
Route::get('/', fn (): string => '
<script>
document.cookie = "test=1";
document.cookie = "test2=2";
</script>
');

$page = visit('/');

$page->assertNoCookies();
})->throws(ExpectationFailedException::class, 'but found 2');