diff --git a/src/Api/On.php b/src/Api/On.php index 9cec714d..4600d1fb 100644 --- a/src/Api/On.php +++ b/src/Api/On.php @@ -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 $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 $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); } /** diff --git a/tests/Browser/Visit/OnTest.php b/tests/Browser/Visit/OnTest.php new file mode 100644 index 00000000..8726cdbf --- /dev/null +++ b/tests/Browser/Visit/OnTest.php @@ -0,0 +1,31 @@ + ' + + Waiting + '); + + $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 => ' + + Waiting + '); + + $page = visit('/')->on(); + + $page->click('#toggle'); + + $page->assertSeeIn('#result', 'Clicked'); +});