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
11 changes: 11 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Pest\Browser\Enums\BrowserType;
use Pest\Browser\Enums\ColorScheme;
use Pest\Browser\Playwright\Playwright;
use Pest\Browser\Support\Screenshot;

/**
* @internal
Expand Down Expand Up @@ -95,6 +96,16 @@ public function userAgent(string $userAgent): self
return $this;
}

/**
* Sets the screenshots directory.
*/
public function screenshots(string $dir): self
{
Screenshot::useDirectory($dir);

return $this;
}

/**
* Sets the host for the server.
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Support/Screenshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,28 @@
*/
final class Screenshot
{
/**
* The path to the screenshots' directory.
*/
private static ?string $dir = null;

/**
* Set the path to the screenshots' directory.
*/
public static function useDirectory(string $dir): void
{
self::$dir = $dir;
}

/**
* Return the path to the screenshots' directory.
*/
public static function dir(): string
{
if (self::$dir !== null) {
return self::$dir;
}

return TestSuite::getInstance()->rootPath
.'/tests/Browser/Screenshots';
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/Configuration/HostConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@

use Pest\Browser\Configuration;
use Pest\Browser\Playwright\Playwright;
use Pest\Browser\Support\Screenshot;

function resetConfigurationScreenshotDir(): void
{
$property = new ReflectionProperty(Screenshot::class, 'dir');
$property->setValue(null, null);
}

beforeEach(function (): void {
// Reset Playwright state before each test
Playwright::setHost(null);
resetConfigurationScreenshotDir();
});

afterEach(function (): void {
resetConfigurationScreenshotDir();
});

it('can set host via configuration', function (): void {
Expand All @@ -31,6 +43,16 @@
expect(Playwright::host())->toBe('app.localhost');
});

it('can set screenshots directory via configuration', function (): void {
$config = new Configuration();
$dir = sys_get_temp_dir().'/pest-browser-configuration-'.uniqid('', true);

$result = $config->screenshots($dir);

expect($result)->toBeInstanceOf(Configuration::class)
->and(Screenshot::dir())->toBe($dir);
});

it('stores host in Playwright global state', function (): void {
expect(Playwright::host())->toBeNull();

Expand Down
64 changes: 64 additions & 0 deletions tests/Unit/Support/ScreenshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,70 @@
declare(strict_types=1);

use Pest\Browser\Support\Screenshot;
use Pest\TestSuite;

function resetScreenshotDir(): void
{
$property = new ReflectionProperty(Screenshot::class, 'dir');
$property->setValue(null, null);
}

function screenshotTestDir(string $name): string
{
return sys_get_temp_dir().'/pest-browser-'.$name.'-'.uniqid('', true);
}

beforeEach(function (): void {
resetScreenshotDir();
});

afterEach(function (): void {
Screenshot::cleanup();
resetScreenshotDir();
});

it('uses the default screenshots directory', function (): void {
expect(Screenshot::dir())->toBe(TestSuite::getInstance()->rootPath.'/tests/Browser/Screenshots');
});

it('uses a configured screenshots directory', function (): void {
$dir = screenshotTestDir('configured');

Screenshot::useDirectory($dir);

expect(Screenshot::dir())->toBe($dir);
});

it('builds screenshot paths from the configured directory', function (): void {
$dir = screenshotTestDir('path');

Screenshot::useDirectory($dir);

expect(Screenshot::path('foo'))->toBe($dir.'/foo.png');
});

it('cleans up only the configured screenshots directory', function (): void {
$parent = screenshotTestDir('cleanup');
$dir = $parent.'/Screenshots';
$sharedFile = $parent.'/shared.txt';

mkdir($dir.'/Sliders', 0755, true);
mkdir($dir.'/ImageDiffView', 0755, true);
file_put_contents($dir.'/screenshot.png', 'screenshot');
file_put_contents($dir.'/Sliders/slider.png', 'slider');
file_put_contents($dir.'/ImageDiffView/diff.html', 'diff');
file_put_contents($sharedFile, 'shared');

Screenshot::useDirectory($dir);
Screenshot::cleanup();

expect(is_dir($dir))->toBeFalse()
->and(is_dir($parent))->toBeTrue()
->and(file_exists($sharedFile))->toBeTrue();

@unlink($sharedFile);
@rmdir($parent);
});

it('places screenshots under tests/Browser/screenshots', function (): void {
Screenshot::save('asdf', 'test-screenshot.png');
Expand Down