Skip to content

Commit ce84fe6

Browse files
author
Neubert, Sebastian
committed
add new feature and config param "saveCurrentImageIfFailure"
1 parent 0d7880b commit ce84fe6

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

module/VisualCeption.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class VisualCeption extends \Codeception\Module
1919
private $referenceImageDir;
2020

2121
private $maximumDeviation = 0;
22+
private $saveCurrentImageIfFailure = false;
2223

2324
private $webDriver = null;
2425
private $webDriverModule = null;
@@ -71,6 +72,11 @@ public function seeVisualChanges ($identifier, $elementID = null, $excludeElemen
7172
if ($deviationResult["deviation"] <= $this->maximumDeviation) {
7273
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
7374
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
75+
76+
if ($this->saveCurrentImageIfFailure === true) {
77+
$saveCurrentImagePath = $this->getDeviationScreenshotPath($identifier, 'current');
78+
$deviationResult['currentImage']->writeImage($saveCurrentImagePath);
79+
}
7480
$this->assertTrue(false, "The deviation of the taken screenshot is too low (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.");
7581
}
7682
}
@@ -94,6 +100,11 @@ public function dontSeeVisualChanges ($identifier, $elementID = null, $excludeEl
94100
if ($deviationResult["deviation"] > $this->maximumDeviation) {
95101
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
96102
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
103+
104+
if ($this->saveCurrentImageIfFailure === true) {
105+
$saveCurrentImagePath = $this->getDeviationScreenshotPath($identifier, 'current.');
106+
$deviationResult['currentImage']->writeImage($saveCurrentImagePath);
107+
}
97108
$this->assertTrue(false, "The deviation of the taken screenshot is too high (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.");
98109
}
99110
}
@@ -148,7 +159,12 @@ private function getDeviation ($identifier, $elementID, array $excludeElements =
148159

149160
$deviation = round($compareResult[1] * 100, 2);
150161
$this->debug("The deviation between the images is ". $deviation . " percent");
151-
return array ("deviation" => $deviation, "deviationImage" => $compareResult[0]);
162+
163+
return array (
164+
"deviation" => $deviation,
165+
"deviationImage" => $compareResult[0],
166+
"currentImage" => $compareResult['currentImage'],
167+
);
152168
}
153169

154170
/**
@@ -164,6 +180,10 @@ private function init ()
164180
$this->maximumDeviation = $this->config["maximumDeviation"];
165181
}
166182

183+
if (array_key_exists('saveCurrentImageIfFailure', $this->config)) {
184+
$this->saveCurrentImageIfFailure = (boolean) $this->config["saveCurrentImageIfFailure"];
185+
}
186+
167187
if (array_key_exists('referenceImageDir', $this->config)) {
168188
$this->referenceImageDir = $this->config["referenceImageDir"];
169189
} else {
@@ -313,12 +333,14 @@ private function resetHideElementsForScreenshot(array $excludeElements)
313333
* @param $identifier identifies your test object
314334
* @return string Path of the deviation image
315335
*/
316-
private function getDeviationScreenshotPath ($identifier)
336+
private function getDeviationScreenshotPath ($identifier, $alternativePrefix = '')
317337
{
318338
$debugDir = \Codeception\Configuration::logDir() . 'debug/';
319-
return $debugDir . 'compare.' . $this->getScreenshotName($identifier);
339+
$prefix = ( $alternativePrefix === '') ? 'compare.' : $alternativePrefix;
340+
return $debugDir . $prefix . $this->getScreenshotName($identifier);
320341
}
321342

343+
322344
/**
323345
* Compare two images by its identifiers.
324346
* If the reference image doesn't exists
@@ -329,13 +351,13 @@ private function getDeviationScreenshotPath ($identifier)
329351
*/
330352
private function compare ($identifier)
331353
{
332-
$currentImagePath = $this->getScreenshotPath($identifier);
333354
$expectedImagePath = $this->getExpectedScreenshotPath($identifier);
355+
$currentImagePath = $this->getScreenshotPath($identifier);
334356

335357
if (! file_exists($expectedImagePath)) {
336358
$this->debug("Copying image (from $currentImagePath to $expectedImagePath");
337359
copy($currentImagePath, $expectedImagePath);
338-
return array (null, 0);
360+
return array (null, 0, null);
339361
} else {
340362
return $this->compareImages($expectedImagePath, $currentImagePath);
341363
}
@@ -366,8 +388,11 @@ private function compareImages ($image1, $image2)
366388

367389
try {
368390
$result = $imagick1->compareImages($imagick2, \Imagick::METRIC_MEANSQUAREERROR);
369-
$result[0]->setImageFormat("png");
370-
} catch (\ImagickException $e) {
391+
$result[0]->setImageFormat('png');
392+
$result['currentImage'] = clone $imagick2;
393+
$result['currentImage']->setImageFormat('png');
394+
}
395+
catch (\ImagickException $e) {
371396
$this->debug("IMagickException! could not campare image1 ($image1) and image2 ($image2).\nExceptionMessage: " . $e->getMessage());
372397
$this->fail($e->getMessage() . ", image1 $image1 and image2 $image2.");
373398
}

readme.md

100644100755
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ VisualCeption:
4747
referenceImageDir: /home/codeception/referenceImages/ # Path to the reference folder (optional, standard is
4848
# <datadir>/VisualCeption/)
4949
maximumDeviation: 5 # deviation in percent
50+
saveCurrentImageIfFailure: true # if true, VisualCeption saves the current
51+
# image in debug dir (default: false)
5052
```
5153

5254
* **referenceImageDir** VisualCeption uses an "old" image for calculating the deviation. These images have to be stored in the system. This is the corresponding directory.
53-
* **maximumDeviation** When comparing two images the deviation will be calculated. If this deviation is greater than the maximum deviation the test will fail.
55+
* **maximumDeviation** When comparing two images the deviation will be calculated. If this deviation is greater than the maximum deviation the test will fail.
56+
* **saveCurrentImageIfFailure** When the test fails, the current image will be saved too, so it's easier to change the reference image with this one. The image will appear beside the compare image with the prefix "current."
57+
5458

5559
## Usage
5660

test/integration/tests/acceptance.suite.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ modules:
2323
capabilities:
2424
webStorageEnabled: true
2525
VisualCeption:
26-
maximumDeviation: 0
26+
maximumDeviation: 0
27+
saveCurrentImageIfFailure: true

0 commit comments

Comments
 (0)