Skip to content

Commit a581d71

Browse files
Nils LangnerNils Langner
authored andcommitted
Added Exception to test bootstrap
2 parents d8145ef + fd0ea5d commit a581d71

File tree

7 files changed

+90
-27
lines changed

7 files changed

+90
-27
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ matrix:
1616

1717
before_script:
1818
- printf "\n" | pecl install imagick
19-
- wget http://selenium.googlecode.com/files/selenium-server-standalone-2.35.0.jar
20-
- java -jar selenium-server-standalone-2.35.0.jar -port 4444 >/dev/null 2>&1 &
19+
- wget http://selenium-release.storage.googleapis.com/2.41/selenium-server-standalone-2.41.0.jar
20+
- java -jar selenium-server-standalone-2.41.0.jar -port 4444 >/dev/null 2>&1 &
2121
- cd test/integration/
2222
- mkdir tests/_log
2323
- php codecept${CODECEPT_VERSION}.phar build

module/VisualCeption.php

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace Codeception\Module;
4+
use Codeception\Module\ImageDeviationException;
45

56
/**
67
* Class VisualCeption
@@ -19,6 +20,7 @@ class VisualCeption extends \Codeception\Module
1920
private $referenceImageDir;
2021

2122
private $maximumDeviation = 0;
23+
private $saveCurrentImageIfFailure = false;
2224

2325
private $webDriver = null;
2426
private $webDriverModule = null;
@@ -76,6 +78,12 @@ public function seeVisualChanges($identifier, $elementID = null, $excludeElement
7678
if ($deviationResult["deviation"] <= $this->maximumDeviation) {
7779
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
7880
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
81+
82+
if ($this->saveCurrentImageIfFailure === true) {
83+
$saveCurrentImagePath = $this->getDeviationScreenshotPath($identifier, 'current');
84+
$deviationResult['currentImage']->writeImage($saveCurrentImagePath);
85+
}
86+
7987
throw new ImageDeviationException("The deviation of the taken screenshot is too low (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.",
8088
$this->getExpectedScreenshotPath($identifier),
8189
$this->getScreenshotPath($identifier),
@@ -102,6 +110,12 @@ public function dontSeeVisualChanges($identifier, $elementID = null, $excludeEle
102110
if ($deviationResult["deviation"] > $this->maximumDeviation) {
103111
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
104112
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
113+
114+
if ($this->saveCurrentImageIfFailure === true) {
115+
$saveCurrentImagePath = $this->getDeviationScreenshotPath($identifier, 'current.');
116+
$deviationResult['currentImage']->writeImage($saveCurrentImagePath);
117+
}
118+
105119
throw new ImageDeviationException("The deviation of the taken screenshot is too hight (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.",
106120
$this->getExpectedScreenshotPath($identifier),
107121
$this->getScreenshotPath($identifier),
@@ -110,22 +124,12 @@ public function dontSeeVisualChanges($identifier, $elementID = null, $excludeEle
110124
}
111125
}
112126

113-
/**
114-
* Inject jQuery.js to the actual site
115-
*/
116-
public function wantToUseJQuery()
117-
{
118-
$jQueryString = file_get_contents(__DIR__ . "/jquery.js");
119-
$this->webDriver->executeScript($jQueryString);
120-
$this->webDriver->executeScript('jQuery.noConflict();');
121-
}
122-
123127
/**
124128
* Hide an element to set the visibility to hidden
125129
*
126130
* @param $elementSelector String of jQuery Element selector, set visibility to hidden
127131
*/
128-
public function hideElement($elementSelector)
132+
private function hideElement($elementSelector)
129133
{
130134
$this->webDriver->executeScript('
131135
if( jQuery("' . $elementSelector . '").length > 0 ) {
@@ -140,7 +144,7 @@ public function hideElement($elementSelector)
140144
*
141145
* @param $elementSelector String of jQuery Element selector, set visibility to visible
142146
*/
143-
public function showElement($elementSelector)
147+
private function showElement($elementSelector)
144148
{
145149
$this->webDriver->executeScript('
146150
if( jQuery("' . $elementSelector . '").length > 0 ) {
@@ -153,8 +157,8 @@ public function showElement($elementSelector)
153157
/**
154158
* Compares the two images and calculate the deviation between expected and actual image
155159
*
156-
* @param $identifier Identifies your test object
157-
* @param $elementID DOM ID of the element, which should be screenshotted
160+
* @param string $identifier Identifies your test object
161+
* @param string $elementID DOM ID of the element, which should be screenshotted
158162
* @param array $excludeElements Element names, which should not appear in the screenshot
159163
* @return array Includes the calculation of deviation in percent and the diff-image
160164
*/
@@ -168,8 +172,14 @@ private function getDeviation($identifier, $elementID, array $excludeElements =
168172
unlink($this->getScreenshotPath($identifier));
169173

170174
$deviation = round($compareResult[1] * 100, 2);
171-
$this->debug("The deviation between the images is " . $deviation . " percent");
172-
return array("deviation" => $deviation, "deviationImage" => $compareResult[0]);
175+
176+
$this->debug("The deviation between the images is ". $deviation . " percent");
177+
178+
return array (
179+
"deviation" => $deviation,
180+
"deviationImage" => $compareResult[0],
181+
"currentImage" => $compareResult['currentImage'],
182+
);
173183
}
174184

175185
/**
@@ -185,6 +195,10 @@ private function init()
185195
$this->maximumDeviation = $this->config["maximumDeviation"];
186196
}
187197

198+
if (array_key_exists('saveCurrentImageIfFailure', $this->config)) {
199+
$this->saveCurrentImageIfFailure = (boolean) $this->config["saveCurrentImageIfFailure"];
200+
}
201+
188202
if (array_key_exists('referenceImageDir', $this->config)) {
189203
$this->referenceImageDir = $this->config["referenceImageDir"];
190204
} else {
@@ -202,7 +216,7 @@ private function init()
202216
* The method inject the
203217
* JQuery Framework and uses the "noConflict"-mode to get the width, height and offset params.
204218
*
205-
* @param $elementId DOM ID of the element, which should be screenshotted
219+
* @param string $elementId DOM ID of the element, which should be screenshotted
206220
* @return array coordinates of the element
207221
*/
208222
private function getCoordinates($elementId)
@@ -341,12 +355,14 @@ private function resetHideElementsForScreenshot(array $excludeElements)
341355
* @param $identifier identifies your test object
342356
* @return string Path of the deviation image
343357
*/
344-
private function getDeviationScreenshotPath($identifier)
358+
private function getDeviationScreenshotPath ($identifier, $alternativePrefix = '')
345359
{
346360
$debugDir = \Codeception\Configuration::logDir() . 'debug/';
347-
return $debugDir . 'compare.' . $this->getScreenshotName($identifier);
361+
$prefix = ( $alternativePrefix === '') ? 'compare.' : $alternativePrefix;
362+
return $debugDir . $prefix . $this->getScreenshotName($identifier);
348363
}
349364

365+
350366
/**
351367
* Compare two images by its identifiers.
352368
* If the reference image doesn't exists
@@ -357,13 +373,13 @@ private function getDeviationScreenshotPath($identifier)
357373
*/
358374
private function compare($identifier)
359375
{
360-
$currentImagePath = $this->getScreenshotPath($identifier);
361376
$expectedImagePath = $this->getExpectedScreenshotPath($identifier);
377+
$currentImagePath = $this->getScreenshotPath($identifier);
362378

363379
if (!file_exists($expectedImagePath)) {
364380
$this->debug("Copying image (from $currentImagePath to $expectedImagePath");
365381
copy($currentImagePath, $expectedImagePath);
366-
return array(null, 0);
382+
return array (null, 0, 'currentImage' => null);
367383
} else {
368384
return $this->compareImages($expectedImagePath, $currentImagePath);
369385
}
@@ -394,8 +410,11 @@ private function compareImages($image1, $image2)
394410

395411
try {
396412
$result = $imagick1->compareImages($imagick2, \Imagick::METRIC_MEANSQUAREERROR);
397-
$result[0]->setImageFormat("png");
398-
} catch (\ImagickException $e) {
413+
$result[0]->setImageFormat('png');
414+
$result['currentImage'] = clone $imagick2;
415+
$result['currentImage']->setImageFormat('png');
416+
}
417+
catch (\ImagickException $e) {
399418
$this->debug("IMagickException! could not campare image1 ($image1) and image2 ($image2).\nExceptionMessage: " . $e->getMessage());
400419
$this->fail($e->getMessage() . ", image1 $image1 and image2 $image2.");
401420
}

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/codecept1.phar

100644100755
File mode changed.

test/integration/codecept2.phar

100644100755
475 KB
Binary file not shown.

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
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Codeception\Module\ImageDeviationException;
4+
5+
class WriteCurrentImageCest
6+
{
7+
8+
/**
9+
* fail the test. lookup is current image is written
10+
*/
11+
public function writeCurrentImageFile(WebGuy $I, $scenario)
12+
{
13+
$I->amOnPage("/VisualCeption/seeVisualChanges.php");
14+
$I->dontSeeVisualChanges("currentImageIdentifier", "#theblock");
15+
16+
$I->wait(2);
17+
18+
// the test has to be called twice for comparison on the travis server
19+
// expect failing the test
20+
21+
$I->amOnPage("/VisualCeption/seeVisualChanges.php");
22+
try
23+
{
24+
$I->dontSeeVisualChanges("currentImageIdentifier", "#theblock");
25+
}
26+
catch (ImageDeviationException $exception)
27+
{
28+
$currentImagePath = $exception->getCurrentImage();
29+
30+
if (is_file( $currentImagePath )) {
31+
return true;
32+
}
33+
34+
// @TODO: complete the test, if current.* image is written
35+
$scenario->incomplete();
36+
throw $exception;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)