diff --git a/src/Playwright/Playwright.php b/src/Playwright/Playwright.php index a464e97d..0e559684 100644 --- a/src/Playwright/Playwright.php +++ b/src/Playwright/Playwright.php @@ -44,6 +44,11 @@ final class Playwright */ private static ColorScheme $defaultColorScheme = ColorScheme::LIGHT; + /** + * The path to the Playwright executable. + */ + private static string $executablePath = DIRECTORY_SEPARATOR.'node_modules'.DIRECTORY_SEPARATOR.'.bin'.DIRECTORY_SEPARATOR; + /** * The timeout in milliseconds. */ @@ -179,6 +184,24 @@ public static function defaultBrowserType(): BrowserType return self::$defaultBrowserType; } + /** + * Sets the path to the Playwright executable. + */ + public static function setExecutablePath(string $path): void + { + $separator = preg_quote(DIRECTORY_SEPARATOR, '/'); + + self::$executablePath = preg_replace('/['.$separator.'\/]+$/', '', $path).DIRECTORY_SEPARATOR; + } + + /** + * Get the path to the Playwright executable. + */ + public static function executeablePath(): string + { + return self::$executablePath; + } + /** * Executes a callback with a temporary timeout. * diff --git a/src/Playwright/Servers/PlaywrightNpmServer.php b/src/Playwright/Servers/PlaywrightNpmServer.php index 31c9febe..64721a3e 100644 --- a/src/Playwright/Servers/PlaywrightNpmServer.php +++ b/src/Playwright/Servers/PlaywrightNpmServer.php @@ -47,7 +47,11 @@ private function __construct( public static function create(string $baseDirectory, string $command, string $host, int $port, string $until): self { return new self( - $baseDirectory, $command, $host, $port, $until + $baseDirectory, + $command, + $host, + $port, + $until ); } @@ -122,14 +126,17 @@ public function url(): string { if (! $this->isRunning()) { throw new RuntimeException( - sprintf('The process with arguments [%s] is not running or has stopped unexpectedly.', json_encode([ - 'baseDirectory' => $this->baseDirectory, - 'command' => $this->command, - 'host' => $this->host, - 'port' => $this->port, - 'until' => $this->until, - ]), - )); + sprintf( + 'The process with arguments [%s] is not running or has stopped unexpectedly.', + json_encode([ + 'baseDirectory' => $this->baseDirectory, + 'command' => $this->command, + 'host' => $this->host, + 'port' => $this->port, + 'until' => $this->until, + ]), + ) + ); } return sprintf('%s:%d', $this->host, $this->port); @@ -143,7 +150,7 @@ public function url(): string private function ensurePlaywrightIsInstalledAndVersionIsSupported(): void { $process = SystemProcess::fromShellCommandline( - '.'.DIRECTORY_SEPARATOR.'node_modules'.DIRECTORY_SEPARATOR.'.bin'.DIRECTORY_SEPARATOR.'playwright run-server --version', + '.'.Playwright::executeablePath().'playwright run-server --version', $this->baseDirectory, ); diff --git a/src/Plugin.php b/src/Plugin.php index 90ff64ca..c6ef6446 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -18,6 +18,7 @@ use Pest\Plugins\Parallel; use Pest\TestSuite; use PHPUnit\Framework\TestStatus\TestStatus; +use RuntimeException; /** * @internal @@ -107,7 +108,7 @@ public function handleArguments(array $arguments): array if (($browser = BrowserType::tryFrom($browser)) === null) { throw new BrowserNotSupportedException( 'The specified browser type is not supported. Supported types are: '. - implode(', ', array_map(fn (BrowserType $type): string => mb_strtolower($type->name), BrowserType::cases())) + implode(', ', array_map(fn (BrowserType $type): string => mb_strtolower($type->name), BrowserType::cases())) ); } @@ -118,6 +119,24 @@ public function handleArguments(array $arguments): array $arguments = array_values($arguments); } + if ($this->hasArgument('--playwright-path', $arguments)) { + $index = array_search('--playwright-path', $arguments, true); + + if ($index === false || ! isset($arguments[$index + 1])) { + throw new RuntimeException( + 'The "--playwright-path" argument requires a value. Usage: --playwright-path .' + ); + } + + $path = $arguments[$index + 1]; + + Playwright::setExecutablePath($path); + + unset($arguments[$index], $arguments[$index + 1]); + + $arguments = array_values($arguments); + } + $this->validateNonSupportedParallelFeatures(); return $arguments; diff --git a/src/ServerManager.php b/src/ServerManager.php index dc5c7d55..2cda7914 100644 --- a/src/ServerManager.php +++ b/src/ServerManager.php @@ -8,6 +8,7 @@ use Pest\Browser\Contracts\PlaywrightServer; use Pest\Browser\Drivers\LaravelHttpServer; use Pest\Browser\Drivers\NullableHttpServer; +use Pest\Browser\Playwright\Playwright; use Pest\Browser\Playwright\Servers\AlreadyStartedPlaywrightServer; use Pest\Browser\Playwright\Servers\PlaywrightNpmServer; use Pest\Browser\Support\PackageJsonDirectory; @@ -62,7 +63,7 @@ public function playwright(): PlaywrightServer $this->playwright ??= PlaywrightNpmServer::create( PackageJsonDirectory::find(), - '.'.DIRECTORY_SEPARATOR.'node_modules'.DIRECTORY_SEPARATOR.'.bin'.DIRECTORY_SEPARATOR.'playwright run-server --host %s --port %d --mode launchServer', + '.'.Playwright::executeablePath().'playwright run-server --host %s --port %d --mode launchServer', self::DEFAULT_HOST, $port, 'Listening on',