Skip to content

Commit 74f433e

Browse files
Nils LangnerNils Langner
authored andcommitted
Added ImageDeviationException as we need this to create great reports
1 parent afab048 commit 74f433e

File tree

2 files changed

+78
-39
lines changed

2 files changed

+78
-39
lines changed

module/ImageDeviationException.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: langn
5+
* Date: 14.10.14
6+
* Time: 13:25
7+
*/
8+
9+
namespace Codeception\Module;
10+
11+
class ImageDeviationException extends \PHPUnit_Framework_ExpectationFailedException
12+
{
13+
private $expectedImage;
14+
private $currentImage;
15+
private $deviationImage;
16+
17+
public function __construct($message, $expectedImage, $currentImage, $deviationImage)
18+
{
19+
$this->deviationImage = $deviationImage;
20+
$this->currentImage = $currentImage;
21+
$this->expectedImage = $expectedImage;
22+
23+
parent::__construct($message);
24+
}
25+
26+
public function getDeviationImage( )
27+
{
28+
return $this->deviationImage;
29+
}
30+
31+
public function getCurrentImage()
32+
{
33+
return $this->currentImage;
34+
}
35+
36+
public function getExpectedImage()
37+
{
38+
return $this->expectedImage;
39+
}
40+
}

module/VisualCeption.php

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class VisualCeption extends \Codeception\Module
2929
* @param array $config
3030
* @return result
3131
*/
32-
public function __construct ($config)
32+
public function __construct($config)
3333
{
3434
$result = parent::__construct($config);
3535
$this->init();
@@ -42,9 +42,9 @@ public function __construct ($config)
4242
* @param \Codeception\TestCase $test
4343
* @throws \Exception
4444
*/
45-
public function _before (\Codeception\TestCase $test)
45+
public function _before(\Codeception\TestCase $test)
4646
{
47-
if ( !$this->hasModule("WebDriver")) {
47+
if (!$this->hasModule("WebDriver")) {
4848
throw new \Exception("VisualCeption uses the WebDriver. Please be sure that this module is activated.");
4949
}
5050

@@ -66,17 +66,17 @@ public function _before (\Codeception\TestCase $test)
6666
* @param string $elementID DOM ID of the element, which should be screenshotted
6767
* @param string|array $excludeElements Element name or array of Element names, which should not appear in the screenshot
6868
*/
69-
public function seeVisualChanges ($identifier, $elementID = null, $excludeElements = array())
69+
public function seeVisualChanges($identifier, $elementID = null, $excludeElements = array())
7070
{
71-
$excludeElements = (array) $excludeElements;
71+
$excludeElements = (array)$excludeElements;
7272

7373
$deviationResult = $this->getDeviation($identifier, $elementID, $excludeElements);
7474

75-
if (! is_null($deviationResult["deviationImage"])) {
75+
if (!is_null($deviationResult["deviationImage"])) {
7676
if ($deviationResult["deviation"] <= $this->maximumDeviation) {
7777
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
7878
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
79-
$this->assertTrue(false, "The deviation of the taken screenshot is too low (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.");
79+
throw new ImageDeviationException("The deviation of the taken screenshot is too low (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.", "pic1", "pic2", $compareScreenshotPath);
8080
}
8181
}
8282
}
@@ -89,13 +89,13 @@ public function seeVisualChanges ($identifier, $elementID = null, $excludeElemen
8989
* @param string $elementID DOM ID of the element, which should be screenshotted
9090
* @param string|array $excludeElements string of Element name or array of Element names, which should not appear in the screenshot
9191
*/
92-
public function dontSeeVisualChanges ($identifier, $elementID = null, $excludeElements = array())
92+
public function dontSeeVisualChanges($identifier, $elementID = null, $excludeElements = array())
9393
{
94-
$excludeElements = (array) $excludeElements;
94+
$excludeElements = (array)$excludeElements;
9595

9696
$deviationResult = $this->getDeviation($identifier, $elementID, $excludeElements);
9797

98-
if (! is_null($deviationResult["deviationImage"])) {
98+
if (!is_null($deviationResult["deviationImage"])) {
9999
if ($deviationResult["deviation"] > $this->maximumDeviation) {
100100
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
101101
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
@@ -122,8 +122,8 @@ public function wantToUseJQuery()
122122
public function hideElement($elementSelector)
123123
{
124124
$this->webDriver->executeScript('
125-
if( jQuery("'.$elementSelector.'").length > 0 ) {
126-
jQuery( "'.$elementSelector.'" ).css("visibility","hidden");
125+
if( jQuery("' . $elementSelector . '").length > 0 ) {
126+
jQuery( "' . $elementSelector . '" ).css("visibility","hidden");
127127
}
128128
');
129129
$this->debug("set visibility of element '$elementSelector' to 'hidden'");
@@ -137,8 +137,8 @@ public function hideElement($elementSelector)
137137
public function showElement($elementSelector)
138138
{
139139
$this->webDriver->executeScript('
140-
if( jQuery("'.$elementSelector.'").length > 0 ) {
141-
jQuery( "'.$elementSelector.'" ).css("visibility","visible");
140+
if( jQuery("' . $elementSelector . '").length > 0 ) {
141+
jQuery( "' . $elementSelector . '" ).css("visibility","visible");
142142
}
143143
');
144144
$this->debug("set visibility of element '$elementSelector' to 'visible'");
@@ -152,7 +152,7 @@ public function showElement($elementSelector)
152152
* @param array $excludeElements Element names, which should not appear in the screenshot
153153
* @return array Includes the calculation of deviation in percent and the diff-image
154154
*/
155-
private function getDeviation ($identifier, $elementID, array $excludeElements = array())
155+
private function getDeviation($identifier, $elementID, array $excludeElements = array())
156156
{
157157
$coords = $this->getCoordinates($elementID);
158158
$this->createScreenshot($identifier, $coords, $excludeElements);
@@ -162,8 +162,8 @@ private function getDeviation ($identifier, $elementID, array $excludeElements =
162162
unlink($this->getScreenshotPath($identifier));
163163

164164
$deviation = round($compareResult[1] * 100, 2);
165-
$this->debug("The deviation between the images is ". $deviation . " percent");
166-
return array ("deviation" => $deviation, "deviationImage" => $compareResult[0]);
165+
$this->debug("The deviation between the images is " . $deviation . " percent");
166+
return array("deviation" => $deviation, "deviationImage" => $compareResult[0]);
167167
}
168168

169169
/**
@@ -173,7 +173,7 @@ private function getDeviation ($identifier, $elementID, array $excludeElements =
173173
*
174174
* @throws \RuntimeException
175175
*/
176-
private function init ()
176+
private function init()
177177
{
178178
if (array_key_exists('maximumDeviation', $this->config)) {
179179
$this->maximumDeviation = $this->config["maximumDeviation"];
@@ -185,7 +185,7 @@ private function init ()
185185
$this->referenceImageDir = \Codeception\Configuration::dataDir() . 'VisualCeption/';
186186
}
187187

188-
if (! is_dir($this->referenceImageDir)) {
188+
if (!is_dir($this->referenceImageDir)) {
189189
$this->debug("Creating directory: $this->referenceImageDir");
190190
mkdir($this->referenceImageDir, 0777, true);
191191
}
@@ -199,7 +199,7 @@ private function init ()
199199
* @param $elementId DOM ID of the element, which should be screenshotted
200200
* @return array coordinates of the element
201201
*/
202-
private function getCoordinates ($elementId)
202+
private function getCoordinates($elementId)
203203
{
204204
if (is_null($elementId)) {
205205
$elementId = 'body';
@@ -209,18 +209,18 @@ private function getCoordinates ($elementId)
209209
$this->webDriver->executeScript($jQueryString);
210210
$this->webDriver->executeScript('jQuery.noConflict();');
211211

212-
$imageCoords = array ();
212+
$imageCoords = array();
213213

214-
$elementExists = (bool) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).length > 0;');
214+
$elementExists = (bool)$this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).length > 0;');
215215

216-
if( !$elementExists) {
217-
throw new \Exception("The element you want to examine ('".$elementId."') was not found.");
216+
if (!$elementExists) {
217+
throw new \Exception("The element you want to examine ('" . $elementId . "') was not found.");
218218
}
219219

220-
$imageCoords['offset_x'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).offset().left;');
221-
$imageCoords['offset_y'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).offset().top;');
222-
$imageCoords['width'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).width();');
223-
$imageCoords['height'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).height();');
220+
$imageCoords['offset_x'] = (string)$this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).offset().left;');
221+
$imageCoords['offset_y'] = (string)$this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).offset().top;');
222+
$imageCoords['width'] = (string)$this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).width();');
223+
$imageCoords['height'] = (string)$this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).height();');
224224

225225
return $imageCoords;
226226
}
@@ -232,7 +232,7 @@ private function getCoordinates ($elementId)
232232
* @param string $identifier identifies your test object
233233
* @return string Name of the image file
234234
*/
235-
private function getScreenshotName ($identifier)
235+
private function getScreenshotName($identifier)
236236
{
237237
$caseName = str_replace('Cept.php', '', $this->test->getFileName());
238238

@@ -251,10 +251,10 @@ private function getScreenshotName ($identifier)
251251
* @return string Path an name of the image file
252252
* @throws \RuntimeException if debug dir could not create
253253
*/
254-
private function getScreenshotPath ($identifier)
254+
private function getScreenshotPath($identifier)
255255
{
256256
$debugDir = \Codeception\Configuration::logDir() . 'debug/tmp/';
257-
if (! is_dir($debugDir)) {
257+
if (!is_dir($debugDir)) {
258258
$created = mkdir($debugDir, 0777, true);
259259
if ($created) {
260260
$this->debug("Creating directory: $debugDir");
@@ -271,7 +271,7 @@ private function getScreenshotPath ($identifier)
271271
* @param string $identifier identifies your test object
272272
* @return string Name of the reference image file
273273
*/
274-
private function getExpectedScreenshotPath ($identifier)
274+
private function getExpectedScreenshotPath($identifier)
275275
{
276276
return $this->referenceImageDir . $this->getScreenshotName($identifier);
277277
}
@@ -284,7 +284,7 @@ private function getExpectedScreenshotPath ($identifier)
284284
* @param array $excludeElements List of elements, which should not appear in the screenshot
285285
* @return string Path of the current screenshot image
286286
*/
287-
private function createScreenshot ($identifier, array $coords, array $excludeElements = array())
287+
private function createScreenshot($identifier, array $coords, array $excludeElements = array())
288288
{
289289
$screenshotPath = \Codeception\Configuration::logDir() . 'debug/' . "fullscreenshot.tmp.png";
290290
$elementPath = $this->getScreenshotPath($identifier);
@@ -335,7 +335,7 @@ private function resetHideElementsForScreenshot(array $excludeElements)
335335
* @param $identifier identifies your test object
336336
* @return string Path of the deviation image
337337
*/
338-
private function getDeviationScreenshotPath ($identifier)
338+
private function getDeviationScreenshotPath($identifier)
339339
{
340340
$debugDir = \Codeception\Configuration::logDir() . 'debug/';
341341
return $debugDir . 'compare.' . $this->getScreenshotName($identifier);
@@ -349,15 +349,15 @@ private function getDeviationScreenshotPath ($identifier)
349349
* @param $identifier identifies your test object
350350
* @return array Test result of image comparison
351351
*/
352-
private function compare ($identifier)
352+
private function compare($identifier)
353353
{
354354
$currentImagePath = $this->getScreenshotPath($identifier);
355355
$expectedImagePath = $this->getExpectedScreenshotPath($identifier);
356356

357-
if (! file_exists($expectedImagePath)) {
357+
if (!file_exists($expectedImagePath)) {
358358
$this->debug("Copying image (from $currentImagePath to $expectedImagePath");
359359
copy($currentImagePath, $expectedImagePath);
360-
return array (null, 0);
360+
return array(null, 0);
361361
} else {
362362
return $this->compareImages($expectedImagePath, $currentImagePath);
363363
}
@@ -370,7 +370,7 @@ private function compare ($identifier)
370370
* @param $image2 Path to the current image in the screenshot
371371
* @return array Result of the comparison
372372
*/
373-
private function compareImages ($image1, $image2)
373+
private function compareImages($image1, $image2)
374374
{
375375
$this->debug("Trying to compare $image1 with $image2");
376376

@@ -393,7 +393,6 @@ private function compareImages ($image1, $image2)
393393
$this->debug("IMagickException! could not campare image1 ($image1) and image2 ($image2).\nExceptionMessage: " . $e->getMessage());
394394
$this->fail($e->getMessage() . ", image1 $image1 and image2 $image2.");
395395
}
396-
\PHPUnit_Framework_Assert::assertTrue(true);
397396
return $result;
398397
}
399398
}

0 commit comments

Comments
 (0)