diff --git a/src/Configuration.php b/src/Configuration.php index bf3c458f..5c4a13c7 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -105,6 +105,17 @@ public function withHost(?string $host): self return $this; } + /** + * Sets the URL of an already-running Playwright server (e.g. http://192.168.1.100:9999). + * When set, the plugin connects to this server instead of starting a local one. + */ + public function withPlaywrightServer(?string $serverUrl): self + { + Playwright::setPlaywrightServerUrl($serverUrl); + + return $this; + } + /** * Enables debug mode for assertions. */ diff --git a/src/Playwright/Playwright.php b/src/Playwright/Playwright.php index c0cffae1..cd878492 100644 --- a/src/Playwright/Playwright.php +++ b/src/Playwright/Playwright.php @@ -59,6 +59,12 @@ final class Playwright */ private static ?string $host = null; + /** + * The URL of an already-running Playwright server (e.g. http://192.168.1.100:9999). + * When set, the plugin connects to this server instead of starting a local one. + */ + private static ?string $playwrightServerUrl = null; + /** * Get a browser factory for the given browser type. */ @@ -155,6 +161,22 @@ public static function host(): ?string return self::$host; } + /** + * Set the URL of an external already-running Playwright server. + */ + public static function setPlaywrightServerUrl(?string $serverUrl): void + { + self::$playwrightServerUrl = $serverUrl; + } + + /** + * Get the URL of the external Playwright server, if set. + */ + public static function playwrightServerUrl(): ?string + { + return self::$playwrightServerUrl ?? getenv('PEST_BROWSER_PLAYWRIGHT_URL') ?: null; + } + /** * Get the default color scheme. */ diff --git a/src/ServerManager.php b/src/ServerManager.php index 4e406fd9..5a9dcedb 100644 --- a/src/ServerManager.php +++ b/src/ServerManager.php @@ -55,6 +55,15 @@ public static function instance(): self */ public function playwright(): PlaywrightServer { + $serverUrl = Playwright::playwrightServerUrl(); + if ($serverUrl) { + $parts = parse_url($serverUrl); + $host = $parts['host']; + $port = $parts['port'] ?? 9999; + AlreadyStartedPlaywrightServer::persist($host, $port); + return $this->playwright ??= new AlreadyStartedPlaywrightServer($host, $port); + } + if (Parallel::isWorker()) { return AlreadyStartedPlaywrightServer::fromPersisted(); } @@ -83,7 +92,7 @@ public function playwright(): PlaywrightServer */ public function http(): HttpServer { - return $this->http ??= match (function_exists('app_path')) { + return $this->http ??= match (class_exists(\Illuminate\Foundation\Application::class)) { true => new LaravelHttpServer( self::DEFAULT_HOST, // Always bind to 127.0.0.1 for server Port::find(),