Skip to content

Commit 0697b05

Browse files
authored
Merge pull request #93 from MrPunyapal/feat/assert-checked
feat: add assertChecked method and corresponding tests
2 parents 57e830b + 2a85598 commit 0697b05

4 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/Autoload.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Pest\Browser;
66

77
use Pest\Browser\Playwright\Client;
8+
use Pest\Browser\Playwright\Page;
9+
use Pest\Browser\Playwright\Playwright;
810
use Pest\Browser\Playwright\Server;
911
use Pest\Plugin;
1012
use Pest\Plugins\Parallel;
@@ -24,6 +26,23 @@ function visit(string $url): PendingTest
2426
}
2527
}
2628

29+
if (! function_exists('\Pest\Browser\page')) {
30+
/**
31+
* Visits the given URL, and starts a new browser test.
32+
*/
33+
function page(string $url = '/'): Page
34+
{
35+
Server::instance()->start();
36+
Client::instance()->connectTo(Server::instance()->url('?browser=chromium'));
37+
38+
$browser = Playwright::chromium()->launch();
39+
$page = $browser->newPage();
40+
$page->goto($url);
41+
42+
return $page;
43+
}
44+
}
45+
2746
register_shutdown_function(function (): void {
2847
if (Parallel::isEnabled() || ! Parallel::isWorker()) {
2948
Server::instance()->stop();

src/Browser.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Pest\Browser;
66

7+
use Pest\Browser\Playwright\Page;
8+
79
/**
810
* @internal
911
*/
@@ -16,4 +18,12 @@ public function visit(string $url): PendingTest
1618
{
1719
return visit($url);
1820
}
21+
22+
/**
23+
* gets the page instance for given URL.
24+
*/
25+
public function page(string $url): Page
26+
{
27+
return page($url);
28+
}
1929
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPUnit\Framework\ExpectationFailedException;
6+
7+
describe('assertChecked', function () {
8+
it('passes when checkbox is checked', function () {
9+
$page = $this->page(playgroundUrl('/test/form-inputs'));
10+
11+
expect($page->querySelector('input[name="checked-checkbox"]'))->toBeChecked();
12+
});
13+
14+
it('fails when checkbox is not checked', function () {
15+
$page = $this->page(playgroundUrl('/test/form-inputs'));
16+
17+
expect($page->querySelector('input[name="unchecked-checkbox"]'))->toBeChecked();
18+
})->throws(ExpectationFailedException::class);
19+
});

tests/Pest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
declare(strict_types=1);
44

5+
use Pest\Browser\Playwright\Element;
6+
use Pest\Expectation;
57
use Pest\TestSuite;
68

79
pest()
@@ -27,3 +29,12 @@ function playgroundUrl(string $path = '/'): string
2729
{
2830
return 'http://localhost:9357/'.mb_ltrim($path, '/');
2931
}
32+
33+
// todo: move this to Pest core
34+
expect()->extend('toBeChecked', function (): Expectation {
35+
36+
expect($this->value)->toBeInstanceOf(Element::class)
37+
->and($this->value->isChecked())->toBeTrue();
38+
39+
return $this;
40+
});

0 commit comments

Comments
 (0)