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
21 changes: 21 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ public function inSafari(): self
return $this;
}

/**
* Uses a system-installed branded browser via its Playwright channel,
* e.g. "chrome", "chrome-beta", "msedge" or "msedge-dev".
*/
public function usingChannel(string $channel): self
{
Playwright::setChannel($channel);

return $this;
}

/**
* Uses the browser executable at the given path, e.g. "/usr/bin/firefox".
*/
public function usingExecutablePath(string $executablePath): self
{
Playwright::setExecutablePath($executablePath);

return $this;
}

/**
* Sets the theme to light mode.
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Playwright/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ public function connectTo(string $url): void
if (! $this->websocketConnection instanceof WebsocketConnection) {
$browser = Playwright::defaultBrowserType()->toPlaywrightName();

$launchOptions = json_encode([
$launchOptions = json_encode(array_filter([
'headless' => Playwright::isHeadless(),
'ignoreHTTPSErrors' => true,
'bypassCSP' => true,
]);
'channel' => Playwright::channel(),
'executablePath' => Playwright::executablePath(),
], fn (mixed $value): bool => $value !== null));

$this->websocketConnection = connect(
"ws://$url?browser=$browser&launch-options=$launchOptions",
Expand Down
42 changes: 42 additions & 0 deletions src/Playwright/Playwright.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ final class Playwright
*/
private static ?string $host = null;

/**
* The browser channel to use, e.g. "chrome" or "msedge".
*/
private static ?string $channel = null;

/**
* The path to a browser executable to use.
*/
private static ?string $executablePath = null;

/**
* Get a browser factory for the given browser type.
*/
Expand Down Expand Up @@ -155,6 +165,38 @@ public static function host(): ?string
return self::$host;
}

/**
* Set the browser channel to use.
*/
public static function setChannel(?string $channel): void
{
self::$channel = $channel;
}

/**
* Get the browser channel to use.
*/
public static function channel(): ?string
{
return self::$channel;
}

/**
* Set the browser executable path to use.
*/
public static function setExecutablePath(?string $executablePath): void
{
self::$executablePath = $executablePath;
}

/**
* Get the browser executable path to use.
*/
public static function executablePath(): ?string
{
return self::$executablePath;
}

/**
* Get the default color scheme.
*/
Expand Down
8 changes: 7 additions & 1 deletion src/ServerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ public function playwright(): PlaywrightServer
$port = Port::find();
$host = Playwright::host() ?? self::DEFAULT_HOST;

$command = '.'.DIRECTORY_SEPARATOR.'node_modules'.DIRECTORY_SEPARATOR.'.bin'.DIRECTORY_SEPARATOR.'playwright run-server --host %s --port %d --mode launchServer';

if (Playwright::executablePath() !== null) {
$command .= ' --unsafe';
}

$this->playwright ??= PlaywrightNpmServer::create(
PackageJsonDirectory::find(),
'.'.DIRECTORY_SEPARATOR.'node_modules'.DIRECTORY_SEPARATOR.'.bin'.DIRECTORY_SEPARATOR.'playwright run-server --host %s --port %d --mode launchServer',
$command,
$host,
$port,
'Listening on',
Expand Down
63 changes: 63 additions & 0 deletions tests/Unit/Configuration/SystemBrowserConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

use Pest\Browser\Configuration;
use Pest\Browser\Playwright\Playwright;

beforeEach(function (): void {
// Reset Playwright state before each test
Playwright::setChannel(null);
Playwright::setExecutablePath(null);
});

it('can set a system browser channel via configuration', function (): void {
$config = new Configuration();

$result = $config->usingChannel('chrome');

expect($result)->toBeInstanceOf(Configuration::class);
expect(Playwright::channel())->toBe('chrome');
});

it('can set a browser executable path via configuration', function (): void {
$config = new Configuration();

$result = $config->usingExecutablePath('/usr/bin/firefox');

expect($result)->toBeInstanceOf(Configuration::class);
expect(Playwright::executablePath())->toBe('/usr/bin/firefox');
});

it('defaults channel and executable path to null', function (): void {
expect(Playwright::channel())->toBeNull();
expect(Playwright::executablePath())->toBeNull();
});

it('supports fluent chaining with other configuration options', function (): void {
$config = new Configuration();

$result = $config
->usingChannel('msedge')
->usingExecutablePath('/usr/bin/msedge')
->headed()
->timeout(10000);

expect($result)->toBeInstanceOf(Configuration::class);
expect(Playwright::channel())->toBe('msedge');
expect(Playwright::executablePath())->toBe('/usr/bin/msedge');
});

it('can override channel and executable path multiple times', function (): void {
Playwright::setChannel('chrome');
expect(Playwright::channel())->toBe('chrome');

Playwright::setChannel('msedge');
expect(Playwright::channel())->toBe('msedge');

Playwright::setExecutablePath('/usr/bin/firefox');
expect(Playwright::executablePath())->toBe('/usr/bin/firefox');

Playwright::setExecutablePath(null);
expect(Playwright::executablePath())->toBeNull();
});