Skip to content

Commit 4375a36

Browse files
author
Nils Langner
committed
Added handling for first shots
1 parent be8285b commit 4375a36

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

module/VisualCeption.php

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
/**
66
* Class VisualCeption
7+
*
78
* @copyright Copyright (c) 2014 G+J Digital Products GmbH
89
* @license MIT license, http://www.opensource.org/licenses/mit-license.php
910
* @package Codeception\Module
@@ -42,7 +43,7 @@ public function _before (\Codeception\TestCase $test)
4243
$this->test = $test;
4344
}
4445

45-
private function getDeviation($identifier, $elementID)
46+
private function getDeviation ($identifier, $elementID)
4647
{
4748
$coords = $this->getCoordinates($elementID);
4849
$this->createScreenshot($identifier, $coords);
@@ -52,7 +53,7 @@ private function getDeviation($identifier, $elementID)
5253
unlink($this->getScreenshotPath($identifier));
5354

5455
$deviation = round($compareResult[1] * 100, 2);
55-
return array("deviation" => $deviation, "deviationImage" => $compareResult[0]);
56+
return array ("deviation" => $deviation, "deviationImage" => $compareResult[0]);
5657
}
5758

5859
/**
@@ -62,13 +63,15 @@ private function getDeviation($identifier, $elementID)
6263
* @param string $identifier identifies your test object
6364
* @param string $elementID DOM ID of the element, which should be screenshotted
6465
*/
65-
public function dontseeVisualChanges($identifier, $elementID = null)
66+
public function dontseeVisualChanges ($identifier, $elementID = null)
6667
{
6768
$deviationResult = $this->getDeviation($identifier, $elementID);
68-
if ($deviationResult["deviation"] > $this->maximumDeviation) {
69-
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
70-
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
71-
$this->assertTrue(false, "The deviation of the taken screenshot is too high (".$deviationResult["deviation"]."%).\nSee $compareScreenshotPath for a deviation screenshot.");
69+
if (! is_null($deviationResult["deviationImage"])) {
70+
if ($deviationResult["deviation"] > $this->maximumDeviation) {
71+
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
72+
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
73+
$this->assertTrue(false, "The deviation of the taken screenshot is too high (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.");
74+
}
7275
}
7376
}
7477

@@ -79,18 +82,21 @@ public function dontseeVisualChanges($identifier, $elementID = null)
7982
* @param string $identifier identifies your test object
8083
* @param string $elementID DOM ID of the element, which should be screenshotted
8184
*/
82-
public function seeVisualChanges($identifier, $elementID = null)
85+
public function seeVisualChanges ($identifier, $elementID = null)
8386
{
8487
$deviationResult = $this->getDeviation($identifier, $elementID);
85-
if ($deviationResult["deviation"] <= $this->maximumDeviation) {
86-
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
87-
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
88-
$this->assertTrue(false, "The deviation of the taken screenshot is too low (".$deviationResult["deviation"]."%).\nSee $compareScreenshotPath for a deviation screenshot.");
88+
if (! is_null($deviationResult["deviationImage"])) {
89+
if ($deviationResult["deviation"] <= $this->maximumDeviation) {
90+
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
91+
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
92+
$this->assertTrue(false, "The deviation of the taken screenshot is too low (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.");
93+
}
8994
}
9095
}
9196

9297
/**
93-
* Initialize the module and read the config. Throws a runtime exception, if the
98+
* Initialize the module and read the config.
99+
* Throws a runtime exception, if the
94100
* reference image dir is not set in the config
95101
*
96102
* @throws \RuntimeException
@@ -114,7 +120,8 @@ private function init ()
114120
}
115121

116122
/**
117-
* Find the position and proportion of a DOM element, specified by it's ID. The method inject the
123+
* Find the position and proportion of a DOM element, specified by it's ID.
124+
* The method inject the
118125
* JQuery Framework and uses the "noConflict"-mode to get the width, height and offset params.
119126
*
120127
* @param $elementId DOM ID of the element, which should be screenshotted
@@ -127,7 +134,7 @@ private function getCoordinates ($elementId)
127134
$elementId = 'body';
128135
}
129136

130-
$jQueryString = file_get_contents(__DIR__."/jquery.js");
137+
$jQueryString = file_get_contents(__DIR__ . "/jquery.js");
131138
$webDriver->executeScript($jQueryString);
132139
$webDriver->executeScript('jQuery.noConflict();');
133140

@@ -165,12 +172,11 @@ private function getScreenshotPath ($identifier)
165172
$debugDir = \Codeception\Configuration::logDir() . 'debug/tmp/';
166173
if (! is_dir($debugDir)) {
167174
$created = mkdir($debugDir, 0777, true);
168-
if( $created ) {
169-
$this->debug("Creating directory: $debugDir");
170-
}else{
175+
if ($created) {
176+
$this->debug("Creating directory: $debugDir");
177+
} else {
171178
throw new \RuntimeException("Unable to create temporary screenshot dir ($debugDir)");
172179
}
173-
174180
}
175181
return $debugDir . $this->getScreenshotName($identifier);
176182
}
@@ -204,9 +210,9 @@ private function createScreenshot ($identifier, array $coords)
204210
$webDriver->takeScreenshot($screenshotPath);
205211

206212
$screenShotImage = new \Imagick();
207-
$screenShotImage->readImage( $screenshotPath );
208-
$screenShotImage->cropImage( $coords['width'], $coords['height'], $coords['offset_x'], $coords['offset_y'] );
209-
$screenShotImage->writeImage( $elementPath );
213+
$screenShotImage->readImage($screenshotPath);
214+
$screenShotImage->cropImage($coords['width'], $coords['height'], $coords['offset_x'], $coords['offset_y']);
215+
$screenShotImage->writeImage($elementPath);
210216

211217
unlink($screenshotPath);
212218

@@ -226,7 +232,8 @@ private function getDeviationScreenshotPath ($identifier)
226232
}
227233

228234
/**
229-
* Compare two images by its identifiers. If the reference image doesn't exists
235+
* Compare two images by its identifiers.
236+
* If the reference image doesn't exists
230237
* the image is copied to the reference path.
231238
*
232239
* @param $identifier identifies your test object

0 commit comments

Comments
 (0)