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
19 changes: 19 additions & 0 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Pest\Browser;

use Pest\Browser\Playwright\Client;
use Pest\Browser\Playwright\Page;
use Pest\Browser\Playwright\Playwright;
use Pest\Browser\Playwright\Server;
use Pest\Plugin;
use Pest\Plugins\Parallel;
Expand All @@ -24,6 +26,23 @@ function visit(string $url): PendingTest
}
}

if (! function_exists('\Pest\Browser\page')) {
/**
* Visits the given URL, and starts a new browser test.
*/
function page(string $url = '/'): Page
{
Server::instance()->start();
Client::instance()->connectTo(Server::instance()->url('?browser=chromium'));

$browser = Playwright::chromium()->launch();
$page = $browser->newPage();
$page->goto($url);

return $page;
}
}

register_shutdown_function(function (): void {
if (Parallel::isEnabled() || ! Parallel::isWorker()) {
Server::instance()->stop();
Expand Down
10 changes: 10 additions & 0 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Pest\Browser;

use Pest\Browser\Playwright\Page;

/**
* @internal
*/
Expand All @@ -16,4 +18,12 @@ public function visit(string $url): PendingTest
{
return visit($url);
}

/**
* gets the page instance for given URL.
*/
public function page(string $url): Page
{
return page($url);
}
}
19 changes: 19 additions & 0 deletions tests/Browser/Operations/AssertCheckedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\ExpectationFailedException;

describe('assertChecked', function () {
it('passes when checkbox is checked', function () {
$page = $this->page(playgroundUrl('/test/form-inputs'));

expect($page->querySelector('input[name="checked-checkbox"]'))->toBeChecked();
});

it('fails when checkbox is not checked', function () {
$page = $this->page(playgroundUrl('/test/form-inputs'));

expect($page->querySelector('input[name="unchecked-checkbox"]'))->toBeChecked();
})->throws(ExpectationFailedException::class);
});
11 changes: 11 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use Pest\Browser\Playwright\Element;
use Pest\Expectation;
use Pest\TestSuite;

pest()
Expand All @@ -27,3 +29,12 @@ function playgroundUrl(string $path = '/'): string
{
return 'http://localhost:9357/'.mb_ltrim($path, '/');
}

// todo: move this to Pest core
expect()->extend('toBeChecked', function (): Expectation {

expect($this->value)->toBeInstanceOf(Element::class)
->and($this->value->isChecked())->toBeTrue();

return $this;
});
Loading