Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AssertMatchesScreenshot & AssertNotMatchesScreenshot operations #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
37 changes: 37 additions & 0 deletions src/Operations/AssertMatchesScreenshot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Pest\Browser\Operations;

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

/**
* @internal description
*/
final readonly class AssertMatchesScreenshot implements Operation
{
/**
* The path to already present screenshot.
*/
private string $path;

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

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

/**
* Compile the operation.
*/
public function compile(): string
{
return "await expect(page).toHaveScreenshot('$this->path')";
}
}
37 changes: 37 additions & 0 deletions src/Operations/AssertNotMatchesScreenshot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Pest\Browser\Operations;

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

/**
* @internal
*/
final readonly class AssertNotMatchesScreenshot implements Operation
{
/**
* The path to already present screenshot.
*/
private string $path;

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

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

/**
* Compile the operation.
*/
public function compile(): string
{
return "await expect(page).not.toHaveScreenshot('$this->path')";
}
}
20 changes: 20 additions & 0 deletions src/PendingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ public function screenshot(?string $path = null): self
return $this;
}

/**
* Determines if the screenshot matches stored screenshot
*/
public function assertMatchesScreenshot(string $filename): self
{
$this->operations[] = new Operations\AssertMatchesScreenshot($filename);

return $this;
}

/**
* Determines if the screenshot doesn't match stored screenshot
*/
public function assertNotMatchesScreenshot(string $filename): self
{
$this->operations[] = new Operations\AssertNotMatchesScreenshot($filename);

return $this;
}

/**
* Checks if the page has a title.
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/Browser/Operations/AssertMatchesScreenshotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

test('assert page matches a screenshot', function () {
$this->visit('https://example.com')
->screenshot('example.png')
->assertMatchesScreenshot('example.png');
});
12 changes: 12 additions & 0 deletions tests/Browser/Operations/AssertNotMatchesScreenshotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

test('assert page not matches a screenshot', function () {
$this->visit('https://example.com')
->screenshot('example.png')
->assertMatchesScreenshot('example.png');

$this->visit('https://laravel.com')
->assertNotMatchesScreenshot('example.png');
});