Skip to content

feat: use multiple browsers for individual tests#202

Closed
nexxai wants to merge 4 commits intopestphp:4.xfrom
nexxai:main
Closed

feat: use multiple browsers for individual tests#202
nexxai wants to merge 4 commits intopestphp:4.xfrom
nexxai:main

Conversation

@nexxai
Copy link
Contributor

@nexxai nexxai commented Mar 1, 2026

Problem

This PR was inspired by the fact that Safari's printing functionality works differently than Chrome and Firefox. I needed a way to expose certain individual tests to multiple browsers without having to run the entire test suite for each one. Previously, there was no way to run a single test against multiple browser types within the same test execution; you either have to hard-code the browser in your Pest.php configuration file, or run your entire test suite using the --browser safari syntax.

Proposed Solution

I added a new browser() method to PendingAwaitablePage that accepts an array of BrowserType enums. This allows developers to run the same assertions across multiple browsers sequentially.

Usage Examples:

Fetch /page in both Chrome and Firefox, using dark mode:

visit('/page')
    ->browser([BrowserType::CHROME, BrowserType::FIREFOX])
    ->inDarkMode()
    ->each(function ($page) {
        $page->assertSee('Content');
    });

If you want the results from each browser:

$urls = visit('/page')
    ->browser([BrowserType::CHROME, BrowserType::FIREFOX])
    ->inDarkMode()
    ->withLocale('en-US')
    ->eachResult(function ($page) {
        $page->url();
    });

Technical Details

  • Added MultiBrowserPendingPage class that holds multiple PendingAwaitablePage instances
  • Uses __call magic method to forward method calls (like inDarkMode(), withLocale(), etc.) to all pending pages (Note: Phpstan wasn't understanding the magic here, which is why I added the //ignore-line comment. If you are aware of a better way of handling this, please share and I'll update it)
  • The each() method runs the callback on each browser sequentially, closing and reconnecting between browsers to work within Playwright's single-browser limitation
  • Added closeOthers() method to Playwright class to facilitate switching between browsers
  • Fixed Client::disconnect() to properly close the WebSocket connection

@nexxai nexxai marked this pull request as draft March 1, 2026 09:25
@nexxai nexxai closed this Mar 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant