Skip to content

Commit

Permalink
feat: adds screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Feb 14, 2025
1 parent fd23bd2 commit 6084b08
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Operations/Screenshot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Pest\Browser\Operations;

use Pest\Browser\Contracts\Operation;
use Pest\TestSuite;

/**
* @internal
*/
final readonly class Screenshot implements Operation
{
/**
* The path to save the screenshot.
*/
private string $path;

/**
* Creates an operation instance.
*/
public function __construct(
?string $path = null,
) {
$basePath = TestSuite::getInstance()->testPath.'/Browser/screenshots';

$path ??= $this->generateFilename();

$this->path = $basePath.'/'.$path;
}

public function compile(): string
{
return sprintf("await page.screenshot({ path: '%s' });", $this->path);
}

private function generateFilename(): string
{
$name = test()->name(); // @phpstan-ignore-line
assert(is_string($name));

return mb_ltrim($name, '__pest_evaluable_')
.'_'
.date('Y_m_d_H_i_s').'.png';
}
}
14 changes: 14 additions & 0 deletions src/PendingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Pest\Browser\Contracts\Operation;
use Pest\Browser\ValueObjects\TestResult;

// Modern PHP Tooling

/**
* @internal
*/
Expand Down Expand Up @@ -37,6 +39,16 @@ public function visit(string $url): self
return $this;
}

/**
* Takes a screenshot.
*/
public function screenshot(?string $path = null): self
{
$this->operations[] = new Operations\Screenshot($path);

return $this;
}

/**
* Checks if the page has a title.
*/
Expand All @@ -53,6 +65,8 @@ public function toHaveTitle(string $title): self
public function toNotHaveTitle(string $title): self
{
$this->operations[] = new Operations\ToNotHaveTitle($title);

return $this;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/Browser/Operations/ScreenshotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

use Pest\TestSuite;

use function Pest\Browser\visit;

afterEach(function () {
$basePath = TestSuite::getInstance()->testPath.'/Browser/screenshots';

foreach (glob($basePath.'/*') as $file) {
if (is_file($file)) {
unlink($file);
}
}

rmdir($basePath);
});

it('takes a screenshot', function (): void {
$basePath = TestSuite::getInstance()->testPath.'/Browser/screenshots';

visit('https://laravel.com')
->screenshot('laravel.png');

expect(file_exists($basePath.'/laravel.png'))->toBeTrue();
});

it('takes a screenshot and generates a path', function (): void {
$basePath = TestSuite::getInstance()->testPath.'/Browser/screenshots';

visit('https://laravel.com')
->screenshot();

$files = glob($basePath.'/*');

expect(count($files))->toBe(1);
});

0 comments on commit 6084b08

Please sign in to comment.