Skip to content

Commit b9dd4e2

Browse files
committed
Store diff element screenshot even if dimensions changed
1 parent ee1d027 commit b9dd4e2

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

src/ServiceProvider.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,13 @@ public function boot(): void
8787
$filePath = sprintf('%s/references/%s.png', rtrim(Browser::$storeScreenshotsAt, '/'), $name);
8888
$diffName = sprintf('%s-diff', $name);
8989
$diffFilePath = sprintf('%s/diffs/%s.png', rtrim(Browser::$storeScreenshotsAt, '/'), $diffName);
90-
$tempFilePath = sprintf('%s/temp/%s.png', rtrim(Browser::$storeScreenshotsAt, '/'), $name);
9190

9291
// Create directories if they don't exist
9392
$directoryPath = dirname($filePath);
9493
if (!is_dir($directoryPath)) {
9594
mkdir($directoryPath, 0777, true);
9695
}
9796

98-
$tempDirectoryPath = dirname($tempFilePath);
99-
if (!is_dir($tempDirectoryPath)) {
100-
mkdir($tempDirectoryPath, 0777, true);
101-
}
102-
10397
// If no reference exists, create it
10498
if (!file_exists($filePath)) {
10599
// Use Dusk's built-in screenshotElement method
@@ -115,29 +109,31 @@ public function boot(): void
115109
return $this;
116110
}
117111

112+
$diffName = sprintf('diffs/%s', $name);
113+
118114
// Take current element screenshot for comparison
119-
$this->screenshotElement($selector, 'temp/' . $name);
115+
$this->screenshotElement($selector, $diffName);
120116

117+
$diffFilePath = sprintf('%s/%s.png', rtrim(Browser::$storeScreenshotsAt, '/'), $diffName);
118+
121119
// Compare images
122120
$originalImage = new Imagick($filePath);
123-
$currentImage = new Imagick($tempFilePath);
121+
$currentImage = new Imagick($diffFilePath);
124122

125123
if (
126124
$originalImage->getImageWidth() !== $currentImage->getImageWidth()
127125
|| $originalImage->getImageHeight() !== $currentImage->getImageHeight()
128126
) {
129-
// Clean up temp file
130-
unlink($tempFilePath);
131-
132127
if (config('visual-assert.skip_if_different_element_size', false)) {
133128
return $this;
134129
}
135130

136-
Assert::assertTrue(false, sprintf("Element screenshots are not the same size (original: %dx%d, current: %dx%d). The element may have changed dimensions.",
131+
Assert::assertTrue(false, sprintf("Element screenshots are not the same size (original: %dx%d, current: %dx%d). The element may have changed dimensions. Difference can be viewed at: %s",
137132
$originalImage->getImageWidth(),
138133
$originalImage->getImageHeight(),
139134
$currentImage->getImageWidth(),
140-
$currentImage->getImageHeight()
135+
$currentImage->getImageHeight(),
136+
$diffFilePath
141137
));
142138
return $this;
143139
}
@@ -146,9 +142,6 @@ public function boot(): void
146142

147143
// If there's a difference above threshold, save the diff
148144
if ($result[1] > $threshold) {
149-
// Move temp file to diff folder
150-
rename($tempFilePath, $diffFilePath);
151-
152145
// Also create a visual diff image
153146
$visualDiffPath = sprintf('%s/diffs/%s-visual.png', rtrim(Browser::$storeScreenshotsAt, '/'), $name);
154147
$result[0]->setImageFormat("png");
@@ -157,7 +150,6 @@ public function boot(): void
157150
Assert::assertLessThanOrEqual($threshold, $result[1], sprintf('Element screenshots are not the same. Difference can be viewed at: %s and visual diff at: %s', $diffFilePath, $visualDiffPath));
158151
} else {
159152
// Clean up temp file
160-
unlink($tempFilePath);
161153
Assert::assertLessThanOrEqual($threshold, $result[1], 'Element screenshots match within threshold.');
162154
}
163155

0 commit comments

Comments
 (0)