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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Added
- Targeted browser installation through `playwright-install [browser...]`

### Fixed
- Failure screenshots are named after the failing test again on PHPUnit 10 and newer

## [1.4.0] - 2026-08-10

### Added
Expand Down
28 changes: 26 additions & 2 deletions src/Testing/PlaywrightTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ protected function tearDownPlaywright(): void
$status = $this->status();

if ($status->isFailure() || $status->isError()) {
$testName = method_exists($this, 'getName') && is_string($this->getName()) ? $this->getName() : 'test';
$this->captureFailureArtifacts($testName);
$this->captureFailureArtifacts($this->resolveTestName());
}

$this->safeClose($this->context);
Expand Down Expand Up @@ -180,6 +179,31 @@ private function shouldTrace(): bool
return '' !== $value && '0' !== $value;
}

private function resolveTestName(): string
{
foreach (['name', 'getName'] as $method) {
if (!method_exists($this, $method)) {
continue;
}

$name = $this->{$method}();

if (is_string($name) && '' !== $name) {
return self::sanitizeFileName($name);
}
}

return 'test';
}

private static function sanitizeFileName(string $name): string
{
$sanitized = preg_replace('/[^A-Za-z0-9._-]+/', '_', $name) ?? '';
$sanitized = trim($sanitized, '_.');

return '' === $sanitized ? 'test' : $sanitized;
}

private function captureFailureArtifacts(string $testName): void
{
$dir = getcwd().'/test-failures';
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/Testing/PlaywrightTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,43 @@ public function testSetUpCallsSetUpPlaywright(): void
$this->assertTrue($setUpMethod->hasReturnType());
$this->assertEquals('void', $setUpMethod->getReturnType()->getName());
}

public function testResolveTestNameUsesTheRunningTestName(): void
{
$case = new class('testExample') extends PlaywrightTestCase {
public function testExample(): void
{
}
};

$method = new \ReflectionMethod($case, 'resolveTestName');

$this->assertSame('testExample', $method->invoke($case));
}

public function testResolveTestNameStaysUsableAsAFileName(): void
{
$case = new class('testExample with data set "a/b"') extends PlaywrightTestCase {
public function testExample(): void
{
}
};

$method = new \ReflectionMethod($case, 'resolveTestName');

$this->assertSame('testExample_with_data_set_a_b', $method->invoke($case));
}

public function testResolveTestNameFallsBackWhenTheNameIsEmpty(): void
{
$case = new class('') extends PlaywrightTestCase {
public function testExample(): void
{
}
};

$method = new \ReflectionMethod($case, 'resolveTestName');

$this->assertSame('test', $method->invoke($case));
}
}
Loading