From 0b9c9c3b5e12f85b9cc94814e2dc344db3439a71 Mon Sep 17 00:00:00 2001 From: Yogesh Vaishnav Date: Sun, 16 Feb 2025 14:42:14 +0530 Subject: [PATCH] feat: add operations to check matching and non matching of page screenshot with stored screenshot --- src/Operations/AssertMatchesScreenshot.php | 37 +++++++++++++++++++ src/Operations/AssertNotMatchesScreenshot.php | 37 +++++++++++++++++++ src/PendingTest.php | 20 ++++++++++ .../AssertMatchesScreenshotTest.php | 9 +++++ .../AssertNotMatchesScreenshotTest.php | 12 ++++++ 5 files changed, 115 insertions(+) create mode 100644 src/Operations/AssertMatchesScreenshot.php create mode 100644 src/Operations/AssertNotMatchesScreenshot.php create mode 100644 tests/Browser/Operations/AssertMatchesScreenshotTest.php create mode 100644 tests/Browser/Operations/AssertNotMatchesScreenshotTest.php diff --git a/src/Operations/AssertMatchesScreenshot.php b/src/Operations/AssertMatchesScreenshot.php new file mode 100644 index 0000000..f124d05 --- /dev/null +++ b/src/Operations/AssertMatchesScreenshot.php @@ -0,0 +1,37 @@ +testPath.'/Browser/screenshots'; + + $this->path = $basePath.'/'.$filename; + } + + /** + * Compile the operation. + */ + public function compile(): string + { + return "await expect(page).toHaveScreenshot('$this->path')"; + } +} diff --git a/src/Operations/AssertNotMatchesScreenshot.php b/src/Operations/AssertNotMatchesScreenshot.php new file mode 100644 index 0000000..a4ceed0 --- /dev/null +++ b/src/Operations/AssertNotMatchesScreenshot.php @@ -0,0 +1,37 @@ +testPath.'/Browser/screenshots'; + + $this->path = $basePath.'/'.$filename; + } + + /** + * Compile the operation. + */ + public function compile(): string + { + return "await expect(page).not.toHaveScreenshot('$this->path')"; + } +} diff --git a/src/PendingTest.php b/src/PendingTest.php index 5970e4a..ac24a0e 100644 --- a/src/PendingTest.php +++ b/src/PendingTest.php @@ -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. */ diff --git a/tests/Browser/Operations/AssertMatchesScreenshotTest.php b/tests/Browser/Operations/AssertMatchesScreenshotTest.php new file mode 100644 index 0000000..73a7060 --- /dev/null +++ b/tests/Browser/Operations/AssertMatchesScreenshotTest.php @@ -0,0 +1,9 @@ +visit('https://example.com') + ->screenshot('example.png') + ->assertMatchesScreenshot('example.png'); +}); diff --git a/tests/Browser/Operations/AssertNotMatchesScreenshotTest.php b/tests/Browser/Operations/AssertNotMatchesScreenshotTest.php new file mode 100644 index 0000000..e2c9862 --- /dev/null +++ b/tests/Browser/Operations/AssertNotMatchesScreenshotTest.php @@ -0,0 +1,12 @@ +visit('https://example.com') + ->screenshot('example.png') + ->assertMatchesScreenshot('example.png'); + + $this->visit('https://laravel.com') + ->assertNotMatchesScreenshot('example.png'); +});