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

/**
* Slows down each browser action by the given number of milliseconds.
*/
public function slowMo(int $milliseconds = Playwright::DEFAULT_SLOW_MO): self
{
Playwright::setSlowMo($milliseconds);

return $this;
}

/**
* Sets the browsers userAgent.
*/
Expand Down
1 change: 1 addition & 0 deletions src/Playwright/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function connectTo(string $url): void

$launchOptions = json_encode([
'headless' => Playwright::isHeadless(),
'slowMo' => Playwright::slowMo(),
'ignoreHTTPSErrors' => true,
'bypassCSP' => true,
]);
Expand Down
27 changes: 27 additions & 0 deletions src/Playwright/Playwright.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
*/
final class Playwright
{
/**
* The default slow-motion delay, in milliseconds, used when `--slow-mo` is
* passed without a value.
*/
public const int DEFAULT_SLOW_MO = 500;

/**
* Browser types
*
Expand Down Expand Up @@ -49,6 +55,11 @@ final class Playwright
*/
private static int $timeout = 5_000;

/**
* The delay, in milliseconds, applied before each browser action.
*/
private static int $slowMo = 0;

/**
* The default userAgent.
*/
Expand Down Expand Up @@ -131,6 +142,22 @@ public static function timeout(): int
return self::$timeout;
}

/**
* Set the delay, in milliseconds, applied before each browser action.
*/
public static function setSlowMo(int $milliseconds): void
{
self::$slowMo = $milliseconds;
}

/**
* Get the delay, in milliseconds, applied before each browser action.
*/
public static function slowMo(): int
{
return self::$slowMo;
}

/**
* Set the default userAgent.
*/
Expand Down
18 changes: 18 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ public function handleArguments(array $arguments): array
$arguments = array_values($arguments);
}

if ($this->hasArgument('--slow-mo', $arguments)) {
$index = array_search('--slow-mo', $arguments, true);

// `--slow-mo` may be passed bare (uses a sensible default) or with an
// explicit millisecond value: `--slow-mo 250`.
if ($index !== false && isset($arguments[$index + 1]) && is_numeric($arguments[$index + 1])) {
Playwright::setSlowMo((int) $arguments[$index + 1]);

unset($arguments[$index], $arguments[$index + 1]);
} else {
Playwright::setSlowMo(Playwright::DEFAULT_SLOW_MO);

unset($arguments[$index]);
}

$arguments = array_values($arguments);
}

$this->validateNonSupportedParallelFeatures();

return $arguments;
Expand Down
51 changes: 51 additions & 0 deletions tests/Unit/Configuration/SlowMoConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

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

beforeEach(function (): void {
// Reset Playwright state before each test
Playwright::setSlowMo(0);
});

it('defaults slow motion to zero', function (): void {
expect(Playwright::slowMo())->toBe(0);
});

it('can set slow motion via configuration', function (): void {
$config = new Configuration();

$result = $config->slowMo(250);

expect($result)->toBeInstanceOf(Configuration::class);
expect(Playwright::slowMo())->toBe(250);
});

it('uses a sensible default when no value is given', function (): void {
$config = new Configuration();

$config->slowMo();

expect(Playwright::slowMo())->toBe(Playwright::DEFAULT_SLOW_MO);
});

it('follows fluent interface pattern', function (): void {
$config = new Configuration();

$result = $config
->slowMo(500)
->timeout(10000);

expect($result)->toBeInstanceOf(Configuration::class);
expect(Playwright::slowMo())->toBe(500);
});

it('stores slow motion in Playwright global state', function (): void {
expect(Playwright::slowMo())->toBe(0);

Playwright::setSlowMo(1000);

expect(Playwright::slowMo())->toBe(1000);
});