[Bug]: On::__call() creates a new browser context on every method call - breaks stateful page interactions
What Happened
When I use:
$page = visit('/')->on()->mobile();
$page->click('@search-button');
$page->...It always failed. BUT! When I use like that:
$page = visit('/');
$page->click('@search-button');
$page->...Test passed! All works! And not, problem not inside my website on mobile phones, because that, ALSO works:
$page = visit('/')->on()->mobile()->inLightMode();
$page->click('@search-button');
$page->...So, the problem inside On class, inside __call magic method:
/**
* Creates the actual visit page instance, 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->browserType,
$this->device,
$this->url,
$this->options,
)->{$name}(...$arguments);
}That always returns NEW browser on every any method call. Assertions happen to pass because each fresh page is in the expected initial state. Actions (click, type, etc.) mutate the page state, but the next call opens a new browser context - so the mutation is lost and subsequent assertions fail.
inLightMode - fix's that, because that returns PendingAwaitablePage, not On class:
/**
* Sets the color scheme to light mode.
*/
public function inLightMode(): self
{
return new self($this->browserType, $this->device, $this->url, [
'colorScheme' => ColorScheme::LIGHT->value,
...$this->options,
]);
}How to Reproduce
Install fresh pest, pest-plugin-browser and try to use ->on()-><device>() with separate method calls (not chained):
// ❌ BROKEN - each call on $page opens a new browser context
$page = visit('/')->on()->mobile();
$page->click('@search-button');
$page->assertVisible('@search-modal');The documentation shows both patterns. The chained pattern works because every method on PendingAwaitablePage returns self, so calls stay on the same cached instance:
// ✅ WORKS - chained calls stay on the same PendingAwaitablePage instance
$page = visit('/')->on()->mobile();
$page->click('Sign In')
->assertSee('Dashboard');But the documentation also shows the separate-call pattern for other methods (e.g. from()->losAngeles()), which implies it should work for on() too:
// From docs - separate calls pattern:
$page = visit('/')->from()->losAngeles();
$page->assertSee('Los Angeles');
$page->assertSee('America/Los_Angeles');
// So user naturally expects this to work the same way:
$page = visit('/')->on()->mobile();
$page->assertSee('...'); // opens browser #1
$page->click('...'); // opens browser #2 - mutation lost
$page->assertVisible('...'); // opens browser #3 - failsThis makes the bug especially non-obvious: assertions may accidentally pass (each fresh page is in the same initial state), but actions (click, type, etc.) mutate page state that is lost when the next call opens a new context.
Sample Repository
No response
Pest Version
5.2.1
PHP Version
8.5.10
Operation System
Linux
Notes
pestphp/pest-plugin-browser - 5.0.1 version.
Write issue there, because pestphp/pest-plugin-browser repository does't has issues tab.
The fix I think is add caching like inside PendingAwaitablePage:
/**
* Creates the actual visit page instance, and calls the given method on it.
*
* @param array<int, mixed> $arguments
*/
public function __call(string $name, array $arguments): mixed
{
$this->waitablePage ??= $this->createAwaitablePage();
// @phpstan-ignore-next-line
return $this->waitablePage->{$name}(...$arguments);
}Or add some method like ->build() to get PendingAwaitablePage instance.
Because now this example in documentation is broken...
Using Other Devices By default, the visit() method uses a desktop viewport. However, you may specify a mobile viewport by chaining the mobile() method onto the on() method:
$page = visit('/')->on()->mobile();If you wish to use a specific device, you may use the on() method and chain it with a device method such as macbook14() or iPhone14Pro():
$page = visit('/')->on()->iPhone14Pro();From https://pestphp.com/docs/browser-testing#content-using-other-devices
Source: pestphp/pest