Skip to content

Commit fd0ea5d

Browse files
author
Neubert, Sebastian
committed
merge conflicts and add incomplete test for writing current image to debug folder.
2 parents cda7076 + 0c9c64c commit fd0ea5d

File tree

3 files changed

+121
-40
lines changed

3 files changed

+121
-40
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: 42 additions & 40 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
@@ -30,7 +31,7 @@ class VisualCeption extends \Codeception\Module
3031
* @param array $config
3132
* @return result
3233
*/
33-
public function __construct ($config)
34+
public function __construct($config)
3435
{
3536
$result = parent::__construct($config);
3637
$this->init();
@@ -43,9 +44,9 @@ public function __construct ($config)
4344
* @param \Codeception\TestCase $test
4445
* @throws \Exception
4546
*/
46-
public function _before (\Codeception\TestCase $test)
47+
public function _before(\Codeception\TestCase $test)
4748
{
48-
if ( !$this->hasModule("WebDriver")) {
49+
if (!$this->hasModule("WebDriver")) {
4950
throw new \Exception("VisualCeption uses the WebDriver. Please be sure that this module is activated.");
5051
}
5152

@@ -67,13 +68,13 @@ public function _before (\Codeception\TestCase $test)
6768
* @param string $elementID DOM ID of the element, which should be screenshotted
6869
* @param string|array $excludeElements Element name or array of Element names, which should not appear in the screenshot
6970
*/
70-
public function seeVisualChanges ($identifier, $elementID = null, $excludeElements = array())
71+
public function seeVisualChanges($identifier, $elementID = null, $excludeElements = array())
7172
{
72-
$excludeElements = (array) $excludeElements;
73+
$excludeElements = (array)$excludeElements;
7374

7475
$deviationResult = $this->getDeviation($identifier, $elementID, $excludeElements);
7576

76-
if (! is_null($deviationResult["deviationImage"])) {
77+
if (!is_null($deviationResult["deviationImage"])) {
7778
if ($deviationResult["deviation"] <= $this->maximumDeviation) {
7879
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
7980
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
@@ -82,7 +83,8 @@ public function seeVisualChanges ($identifier, $elementID = null, $excludeElemen
8283
$saveCurrentImagePath = $this->getDeviationScreenshotPath($identifier, 'current');
8384
$deviationResult['currentImage']->writeImage($saveCurrentImagePath);
8485
}
85-
$this->assertTrue(false, "The deviation of the taken screenshot is too low (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.");
86+
87+
throw new ImageDeviationException("The deviation of the taken screenshot is too low (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.", "pic1", "pic2", $compareScreenshotPath);
8688
}
8789
}
8890
}
@@ -95,13 +97,13 @@ public function seeVisualChanges ($identifier, $elementID = null, $excludeElemen
9597
* @param string $elementID DOM ID of the element, which should be screenshotted
9698
* @param string|array $excludeElements string of Element name or array of Element names, which should not appear in the screenshot
9799
*/
98-
public function dontSeeVisualChanges ($identifier, $elementID = null, $excludeElements = array())
100+
public function dontSeeVisualChanges($identifier, $elementID = null, $excludeElements = array())
99101
{
100-
$excludeElements = (array) $excludeElements;
102+
$excludeElements = (array)$excludeElements;
101103

102104
$deviationResult = $this->getDeviation($identifier, $elementID, $excludeElements);
103105

104-
if (! is_null($deviationResult["deviationImage"])) {
106+
if (!is_null($deviationResult["deviationImage"])) {
105107
if ($deviationResult["deviation"] > $this->maximumDeviation) {
106108
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
107109
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
@@ -110,7 +112,7 @@ public function dontSeeVisualChanges ($identifier, $elementID = null, $excludeEl
110112
$saveCurrentImagePath = $this->getDeviationScreenshotPath($identifier, 'current.');
111113
$deviationResult['currentImage']->writeImage($saveCurrentImagePath);
112114
}
113-
$this->assertTrue(false, "The deviation of the taken screenshot is too high (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.");
115+
throw new ImageDeviationException("The deviation of the taken screenshot is too hight (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.", "pic1", "pic2", $compareScreenshotPath);
114116
}
115117
}
116118
}
@@ -123,8 +125,8 @@ public function dontSeeVisualChanges ($identifier, $elementID = null, $excludeEl
123125
private function hideElement($elementSelector)
124126
{
125127
$this->webDriver->executeScript('
126-
if( jQuery("'.$elementSelector.'").length > 0 ) {
127-
jQuery( "'.$elementSelector.'" ).css("visibility","hidden");
128+
if( jQuery("' . $elementSelector . '").length > 0 ) {
129+
jQuery( "' . $elementSelector . '" ).css("visibility","hidden");
128130
}
129131
');
130132
$this->debug("set visibility of element '$elementSelector' to 'hidden'");
@@ -138,8 +140,8 @@ private function hideElement($elementSelector)
138140
private function showElement($elementSelector)
139141
{
140142
$this->webDriver->executeScript('
141-
if( jQuery("'.$elementSelector.'").length > 0 ) {
142-
jQuery( "'.$elementSelector.'" ).css("visibility","visible");
143+
if( jQuery("' . $elementSelector . '").length > 0 ) {
144+
jQuery( "' . $elementSelector . '" ).css("visibility","visible");
143145
}
144146
');
145147
$this->debug("set visibility of element '$elementSelector' to 'visible'");
@@ -148,12 +150,12 @@ private function showElement($elementSelector)
148150
/**
149151
* Compares the two images and calculate the deviation between expected and actual image
150152
*
151-
* @param $identifier Identifies your test object
152-
* @param $elementID DOM ID of the element, which should be screenshotted
153+
* @param string $identifier Identifies your test object
154+
* @param string $elementID DOM ID of the element, which should be screenshotted
153155
* @param array $excludeElements Element names, which should not appear in the screenshot
154156
* @return array Includes the calculation of deviation in percent and the diff-image
155157
*/
156-
private function getDeviation ($identifier, $elementID, array $excludeElements = array())
158+
private function getDeviation($identifier, $elementID, array $excludeElements = array())
157159
{
158160
$coords = $this->getCoordinates($elementID);
159161
$this->createScreenshot($identifier, $coords, $excludeElements);
@@ -163,6 +165,7 @@ private function getDeviation ($identifier, $elementID, array $excludeElements =
163165
unlink($this->getScreenshotPath($identifier));
164166

165167
$deviation = round($compareResult[1] * 100, 2);
168+
166169
$this->debug("The deviation between the images is ". $deviation . " percent");
167170

168171
return array (
@@ -179,7 +182,7 @@ private function getDeviation ($identifier, $elementID, array $excludeElements =
179182
*
180183
* @throws \RuntimeException
181184
*/
182-
private function init ()
185+
private function init()
183186
{
184187
if (array_key_exists('maximumDeviation', $this->config)) {
185188
$this->maximumDeviation = $this->config["maximumDeviation"];
@@ -195,7 +198,7 @@ private function init ()
195198
$this->referenceImageDir = \Codeception\Configuration::dataDir() . 'VisualCeption/';
196199
}
197200

198-
if (! is_dir($this->referenceImageDir)) {
201+
if (!is_dir($this->referenceImageDir)) {
199202
$this->debug("Creating directory: $this->referenceImageDir");
200203
mkdir($this->referenceImageDir, 0777, true);
201204
}
@@ -206,10 +209,10 @@ private function init ()
206209
* The method inject the
207210
* JQuery Framework and uses the "noConflict"-mode to get the width, height and offset params.
208211
*
209-
* @param $elementId DOM ID of the element, which should be screenshotted
212+
* @param string $elementId DOM ID of the element, which should be screenshotted
210213
* @return array coordinates of the element
211214
*/
212-
private function getCoordinates ($elementId)
215+
private function getCoordinates($elementId)
213216
{
214217
if (is_null($elementId)) {
215218
$elementId = 'body';
@@ -219,18 +222,18 @@ private function getCoordinates ($elementId)
219222
$this->webDriver->executeScript($jQueryString);
220223
$this->webDriver->executeScript('jQuery.noConflict();');
221224

222-
$imageCoords = array ();
225+
$imageCoords = array();
223226

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

226-
if( !$elementExists) {
227-
throw new \Exception("The element you want to examine ('".$elementId."') was not found.");
229+
if (!$elementExists) {
230+
throw new \Exception("The element you want to examine ('" . $elementId . "') was not found.");
228231
}
229232

230-
$imageCoords['offset_x'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).offset().left;');
231-
$imageCoords['offset_y'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).offset().top;');
232-
$imageCoords['width'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).width();');
233-
$imageCoords['height'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).height();');
233+
$imageCoords['offset_x'] = (string)$this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).offset().left;');
234+
$imageCoords['offset_y'] = (string)$this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).offset().top;');
235+
$imageCoords['width'] = (string)$this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).width();');
236+
$imageCoords['height'] = (string)$this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).height();');
234237

235238
return $imageCoords;
236239
}
@@ -242,7 +245,7 @@ private function getCoordinates ($elementId)
242245
* @param string $identifier identifies your test object
243246
* @return string Name of the image file
244247
*/
245-
private function getScreenshotName ($identifier)
248+
private function getScreenshotName($identifier)
246249
{
247250
$caseName = str_replace('Cept.php', '', $this->test->getFileName());
248251

@@ -261,10 +264,10 @@ private function getScreenshotName ($identifier)
261264
* @return string Path an name of the image file
262265
* @throws \RuntimeException if debug dir could not create
263266
*/
264-
private function getScreenshotPath ($identifier)
267+
private function getScreenshotPath($identifier)
265268
{
266269
$debugDir = \Codeception\Configuration::logDir() . 'debug/tmp/';
267-
if (! is_dir($debugDir)) {
270+
if (!is_dir($debugDir)) {
268271
$created = mkdir($debugDir, 0777, true);
269272
if ($created) {
270273
$this->debug("Creating directory: $debugDir");
@@ -281,7 +284,7 @@ private function getScreenshotPath ($identifier)
281284
* @param string $identifier identifies your test object
282285
* @return string Name of the reference image file
283286
*/
284-
private function getExpectedScreenshotPath ($identifier)
287+
private function getExpectedScreenshotPath($identifier)
285288
{
286289
return $this->referenceImageDir . $this->getScreenshotName($identifier);
287290
}
@@ -294,7 +297,7 @@ private function getExpectedScreenshotPath ($identifier)
294297
* @param array $excludeElements List of elements, which should not appear in the screenshot
295298
* @return string Path of the current screenshot image
296299
*/
297-
private function createScreenshot ($identifier, array $coords, array $excludeElements = array())
300+
private function createScreenshot($identifier, array $coords, array $excludeElements = array())
298301
{
299302
$screenshotPath = \Codeception\Configuration::logDir() . 'debug/' . "fullscreenshot.tmp.png";
300303
$elementPath = $this->getScreenshotPath($identifier);
@@ -361,15 +364,15 @@ private function getDeviationScreenshotPath ($identifier, $alternativePrefix = '
361364
* @param $identifier identifies your test object
362365
* @return array Test result of image comparison
363366
*/
364-
private function compare ($identifier)
367+
private function compare($identifier)
365368
{
366369
$expectedImagePath = $this->getExpectedScreenshotPath($identifier);
367370
$currentImagePath = $this->getScreenshotPath($identifier);
368371

369-
if (! file_exists($expectedImagePath)) {
372+
if (!file_exists($expectedImagePath)) {
370373
$this->debug("Copying image (from $currentImagePath to $expectedImagePath");
371374
copy($currentImagePath, $expectedImagePath);
372-
return array (null, 0, null);
375+
return array (null, 0, 'currentImage' => null);
373376
} else {
374377
return $this->compareImages($expectedImagePath, $currentImagePath);
375378
}
@@ -382,7 +385,7 @@ private function compare ($identifier)
382385
* @param $image2 Path to the current image in the screenshot
383386
* @return array Result of the comparison
384387
*/
385-
private function compareImages ($image1, $image2)
388+
private function compareImages($image1, $image2)
386389
{
387390
$this->debug("Trying to compare $image1 with $image2");
388391

@@ -408,7 +411,6 @@ private function compareImages ($image1, $image2)
408411
$this->debug("IMagickException! could not campare image1 ($image1) and image2 ($image2).\nExceptionMessage: " . $e->getMessage());
409412
$this->fail($e->getMessage() . ", image1 $image1 and image2 $image2.");
410413
}
411-
\PHPUnit_Framework_Assert::assertTrue(true);
412414
return $result;
413415
}
414416
}
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)