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
22 changes: 22 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ public function handleArguments(array $arguments): array
$arguments = array_values($arguments);
}

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

$value = $index === false
? $this->popArgumentValue('--slow-mo', $arguments)
: $arguments[$index + 1] ?? null;

if ($index !== false && is_numeric($value)) {
unset($arguments[$index + 1]);
}

Playwright::setSlowMo(is_numeric($value) ? (int) $value : Playwright::DEFAULT_SLOW_MO);

$arguments = $this->popArgument('--slow-mo', $arguments);
}

$this->validateNonSupportedParallelFeatures();

return $arguments;
Expand Down Expand Up @@ -183,5 +199,11 @@ private function validateNonSupportedParallelFeatures(): void
'Debugging assertions is not supported when running tests in parallel.',
);
}

if (Playwright::slowMo() > 0) {
throw new OptionNotSupportedInParallelException(
'Running tests in slow motion is not supported when running tests in parallel.',
);
}
}
}
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);
});
52 changes: 52 additions & 0 deletions tests/Unit/Plugin/SlowMoPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

use Pest\Browser\Playwright\Playwright;
use Pest\Browser\Plugin;

beforeEach(function (): void {
Playwright::setSlowMo(0);
});

it('leaves slow motion off when the argument is absent', function (): void {
$arguments = new Plugin()->handleArguments(['tests/Unit/ExampleTest.php']);

expect(Playwright::slowMo())->toBe(0)
->and($arguments)->toBe(['tests/Unit/ExampleTest.php']);
});

it('uses the default when passed bare', function (): void {
$arguments = new Plugin()->handleArguments(['tests/Unit/ExampleTest.php', '--slow-mo']);

expect(Playwright::slowMo())->toBe(Playwright::DEFAULT_SLOW_MO)
->and($arguments)->toBe(['tests/Unit/ExampleTest.php']);
});

it('reads the value from the following argument', function (): void {
$arguments = new Plugin()->handleArguments(['tests/Unit/ExampleTest.php', '--slow-mo', '250']);

expect(Playwright::slowMo())->toBe(250)
->and($arguments)->toBe(['tests/Unit/ExampleTest.php']);
});

it('reads the value from the same argument', function (): void {
$arguments = new Plugin()->handleArguments(['tests/Unit/ExampleTest.php', '--slow-mo=250']);

expect(Playwright::slowMo())->toBe(250)
->and($arguments)->toBe(['tests/Unit/ExampleTest.php']);
});

it('keeps a following argument that is not a number', function (): void {
$arguments = new Plugin()->handleArguments(['--slow-mo', 'tests/Unit/ExampleTest.php']);

expect(Playwright::slowMo())->toBe(Playwright::DEFAULT_SLOW_MO)
->and($arguments)->toBe(['tests/Unit/ExampleTest.php']);
});

it('falls back to the default when the same-argument value is not a number', function (): void {
$arguments = new Plugin()->handleArguments(['tests/Unit/ExampleTest.php', '--slow-mo=fast']);

expect(Playwright::slowMo())->toBe(Playwright::DEFAULT_SLOW_MO)
->and($arguments)->toBe(['tests/Unit/ExampleTest.php']);
});