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
23 changes: 23 additions & 0 deletions src/Playwright/Playwright.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*
Expand Down
27 changes: 17 additions & 10 deletions src/Playwright/Servers/PlaywrightNpmServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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,
);

Expand Down
21 changes: 20 additions & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Pest\Plugins\Parallel;
use Pest\TestSuite;
use PHPUnit\Framework\TestStatus\TestStatus;
use RuntimeException;

/**
* @internal
Expand Down Expand Up @@ -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()))
);
}

Expand All @@ -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-to-playwright>.'
);
}

$path = $arguments[$index + 1];

Playwright::setExecutablePath($path);

unset($arguments[$index], $arguments[$index + 1]);

$arguments = array_values($arguments);
}

$this->validateNonSupportedParallelFeatures();

return $arguments;
Expand Down
3 changes: 2 additions & 1 deletion src/ServerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down