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
25 changes: 16 additions & 9 deletions src/Api/On.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,43 @@
/**
* @mixin PendingAwaitablePage
*/
final readonly class On
final class On
{
/**
* The pending page instance that receives every forwarded call.
*/
private ?PendingAwaitablePage $pendingAwaitablePage = null;

/**
* Creates a new pending awaitable page instance.
*
* @param array<string, mixed> $options
*/
public function __construct(
private BrowserType $browserType,
private Device $device,
private string $url,
private array $options,
private readonly BrowserType $browserType,
private readonly Device $device,
private readonly string $url,
private readonly array $options,
) {
//
}

/**
* Creates the actual visit page instance, and calls the given method on it.
* Creates the actual visit page instance once, and calls the given method on it.
*
* @param array<int, mixed> $arguments
*/
public function __call(string $name, array $arguments): mixed
{
// @phpstan-ignore-next-line
return new PendingAwaitablePage(
$this->pendingAwaitablePage ??= new PendingAwaitablePage(
$this->browserType,
$this->device,
$this->url,
$this->options,
)->{$name}(...$arguments);
);

// @phpstan-ignore-next-line
return $this->pendingAwaitablePage->{$name}(...$arguments);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/Browser/Visit/OnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;

it('keeps the same page across separate calls after choosing a device', function (string $device): void {
Route::get('/', fn (): string => '
<button id="toggle" type="button" onclick="document.getElementById(\'result\').textContent = \'Clicked\'">Toggle</button>
<span id="result">Waiting</span>
');

$page = visit('/')->on()->{$device}();

$page->click('#toggle');

$page->assertSeeIn('#result', 'Clicked');
})->with(['desktop', 'mobile', 'iPhone14Pro']);

it('keeps the same page across separate calls without choosing a device', function (): void {
Route::get('/', fn (): string => '
<button id="toggle" type="button" onclick="document.getElementById(\'result\').textContent = \'Clicked\'">Toggle</button>
<span id="result">Waiting</span>
');

$page = visit('/')->on();

$page->click('#toggle');

$page->assertSeeIn('#result', 'Clicked');
});