Skip to content

Conversation

@cyppe
Copy link

@cyppe cyppe commented Aug 31, 2025

Add configurable browser launch arguments

Problem

Currently, there's no way to pass custom launch arguments to the browser when running Pest browser tests. This creates issues in several scenarios:

  • Docker/DDEV environments need stability flags like --no-sandbox and --disable-dev-shm-usage
  • HiDPI displays require scaling arguments for proper rendering in headed mode
  • CI environments could benefit from performance optimizations in headless mode

In our case, we needed to configure HiDPI scaling for browser tests running in DDEV with Omarchy (Arch Linux desktop environment) to match the 2x scaling of our regular desktop environment.

Solution

This PR adds three new methods to the Configuration class:

  • withArgs(array $args) - Sets arguments for all modes (headless and headed)
  • withHeadedArgs(array $args) - Sets arguments only for headed mode
  • withHeadlessArgs(array $args) - Sets arguments only for headless mode

Arguments are merged intelligently: global args are always included, plus mode-specific args based on whether --headed flag is used.

Usage

Configure globally in tests/Pest.php:

use Pest\Browser\Playwright\Playwright;

pest()->extend(Tests\TestCase::class)
    ->beforeEach(function () {
        // Stability args for all modes
        Playwright::setLaunchArgs(['--no-sandbox', '--disable-dev-shm-usage']);
        
        // HiDPI scaling for headed mode only
        Playwright::setHeadedLaunchArgs([
            '--force-device-scale-factor=2',
            '--high-dpi-support=1',
            '--device-scale-factor=2',
            '--force-color-profile=srgb',
        ]);
    })
    ->in('Browser');

Or using the fluent configuration API:

browser()->configure()
    ->withArgs(['--no-sandbox', '--disable-dev-shm-usage'])
    ->withHeadedArgs(['--force-device-scale-factor=2'])
    ->headed();

Implementation

  • Added static properties to Playwright class to store launch arguments
  • Modified Client::connectTo() to include custom arguments in launch options
  • Arguments are passed to Playwright server via WebSocket connection
  • Comprehensive test coverage with proper state management

Backward Compatibility

This change is fully backward compatible. Existing tests continue to work unchanged, and the new functionality is opt-in.

@jasonmccreary
Copy link
Collaborator

@cyppe, I am in the process of merging some PRs. Could you update yours to resolve the conflicts.

@jasonmccreary jasonmccreary marked this pull request as draft September 5, 2025 12:40
@cyppe cyppe force-pushed the feature/configurable-browser-launch-args branch from cfb3fb1 to 3729e86 Compare September 5, 2025 13:38
@cyppe cyppe marked this pull request as ready for review September 5, 2025 13:40
@cyppe
Copy link
Author

cyppe commented Sep 5, 2025

Nice! I see you have been very active. I was a bit AFK, but have resolved conflict now.

@cyppe
Copy link
Author

cyppe commented Sep 5, 2025

Failure not related to this PR as I understand.

@gazzoy
Copy link

gazzoy commented Sep 11, 2025

@cyppe Thanks for your work. Just curious, would this PR support Retries option?

@cyppe
Copy link
Author

cyppe commented Sep 14, 2025

@jasonmccreary something I can do to push forward with this PR?

@dmlogic
Copy link

dmlogic commented Oct 29, 2025

+1 on this.

I need this too. Like, a LOT. Like, will need to fork this repo if I don't get it!

Happy to help in any way I can to get this - or something else that allows custom launch args - merged in.

For context, my use case is testing a webapp with a requirement for media device access. I need to pass in '--disable-audio-input', '--disable-audio-output', '--use-fake-device-for-media-stream', '--auto-accept-camera-and-microphone-capture', to make it work. The technique in this PR works perfectly.

@cyppe
Copy link
Author

cyppe commented Dec 10, 2025

@nunomaduro @jasonmccreary Is there anything I can do to get this PR moving? Or is there some kind of idea when someone will have some time to look into PRs for this project so we get some hint about timeline.

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.

4 participants