22
33namespace Codeception \Module ;
44
5+ /**
6+ * Class VisualCeption
7+ * @copyright Copyright (c) 2014 G+J Digital Products GmbH
8+ * @license MIT license, http://www.opensource.org/licenses/mit-license.php
9+ * @package Codeception\Module
10+ */
511class VisualCeption extends \Codeception \Module
612{
713
814 private $ referenceImageDir ;
915
1016 private $ maximumDeviation = 0 ;
1117
18+ /**
19+ * Create an object from VisualCeption Class
20+ *
21+ * @param array $config
22+ * @return result
23+ */
1224 public function __construct ($ config )
1325 {
1426 $ result = parent ::__construct ($ config );
1527 $ this ->init ();
1628 return $ result ;
1729 }
1830
31+ /**
32+ * Event hook before a test starts
33+ *
34+ * @param \Codeception\TestCase $test
35+ */
1936 public function _before (\Codeception \TestCase $ test )
2037 {
2138 $ this ->test = $ test ;
2239 }
2340
41+ /**
42+ * Initialize the module and read the config. Throws a runtime exception, if the
43+ * reference image dir is not set in the config
44+ *
45+ * @throws \RuntimeException
46+ */
2447 private function init ()
2548 {
2649 if (array_key_exists ('maximumDeviation ' , $ this ->config )) {
@@ -38,6 +61,13 @@ private function init ()
3861 }
3962 }
4063
64+ /**
65+ * Find the position and proportion of a DOM element, specified by it's ID. The method inject the
66+ * JQuery Framework and uses the "noConflict"-mode to get the width, height and offset params.
67+ *
68+ * @param $elementId DOM ID of the element, which should be screenshotted
69+ * @return array coordinates of the element
70+ */
4171 private function getCoordinates ($ elementId )
4272 {
4373 $ webDriver = $ this ->getModule ("WebDriver " )->webDriver ;
@@ -58,12 +88,26 @@ private function getCoordinates ($elementId)
5888 return $ imageCoords ;
5989 }
6090
91+ /**
92+ * Generates a screenshot image filename
93+ * it uses the testcase name and the given indentifier to generate a png image name
94+ *
95+ * @param string $identifier identifies your test object
96+ * @return string Name of the image file
97+ */
6198 private function getScreenshotName ($ identifier )
6299 {
63100 $ caseName = str_replace ('Cept.php ' , '' , $ this ->test ->getFileName ());
64101 return $ caseName . '. ' . $ identifier . '.png ' ;
65102 }
66103
104+ /**
105+ * Returns the temporary path including the filename where a the screenshot should be saved
106+ * If the path doesn't exist, the method generate it itself
107+ *
108+ * @param string $identifier identifies your test object
109+ * @return string Path an name of the image file
110+ */
67111 private function getScreenshotPath ($ identifier )
68112 {
69113 $ debugDir = \Codeception \Configuration::logDir () . 'debug/tmp/ ' ;
@@ -73,11 +117,24 @@ private function getScreenshotPath ($identifier)
73117 return $ debugDir . $ this ->getScreenshotName ($ identifier );
74118 }
75119
120+ /**
121+ * Returns the reference image path including the filename
122+ *
123+ * @param string $identifier identifies your test object
124+ * @return string Name of the reference image file
125+ */
76126 private function getExpectedScreenshotPath ($ identifier )
77127 {
78128 return $ this ->referenceImageDir . $ this ->getScreenshotName ($ identifier );
79129 }
80130
131+ /**
132+ * Generate the screenshot of the dom element
133+ *
134+ * @param string $identifier identifies your test object
135+ * @param array $coords Coordinates where the DOM element is located
136+ * @return string Path of the current screenshot image
137+ */
81138 private function createScreenshot ($ identifier , array $ coords )
82139 {
83140 $ webDriverModule = $ this ->getModule ("WebDriver " );
@@ -98,10 +155,17 @@ private function createScreenshot ($identifier, array $coords)
98155 return $ elementPath ;
99156 }
100157
158+ /**
159+ * Compare the reference image with a current screenshot, identified by their indentifier name
160+ * and their element ID
161+ *
162+ * @param string $identifier identifies your test object
163+ * @param null $elementID DOM ID of the element, which should be screenshotted
164+ */
101165 public function compareScreenshot ($ identifier , $ elementID = null )
102166 {
103167 $ coords = $ this ->getCoordinates ($ elementID );
104- $ currentImagePath = $ this ->createScreenshot ($ identifier , $ coords );
168+ $ this ->createScreenshot ($ identifier , $ coords );
105169
106170 $ compareResult = $ this ->compare ($ identifier );
107171
@@ -118,12 +182,25 @@ public function compareScreenshot ($identifier, $elementID = null)
118182 }
119183 }
120184
185+ /**
186+ * Returns the image path including the filename of a deviation image
187+ *
188+ * @param $identifier identifies your test object
189+ * @return string Path of the deviation image
190+ */
121191 private function getDeviationScreenshotPath ($ identifier )
122192 {
123193 $ debugDir = \Codeception \Configuration::logDir () . 'debug/ ' ;
124194 return $ debugDir . 'compare. ' . $ this ->getScreenshotName ($ identifier );
125195 }
126196
197+ /**
198+ * Compare two images by its identifiers. If the reference image doesn't exists
199+ * the image is copied to the reference path.
200+ *
201+ * @param $identifier identifies your test object
202+ * @return array Test result of image comparison
203+ */
127204 private function compare ($ identifier )
128205 {
129206 $ currentImagePath = $ this ->getScreenshotPath ($ identifier );
@@ -137,6 +214,13 @@ private function compare ($identifier)
137214 }
138215 }
139216
217+ /**
218+ * Compares to images by given file path
219+ *
220+ * @param $image1 Path to the exprected reference image
221+ * @param $image2 Path to the current image in the screenshot
222+ * @return array Result of the comparison
223+ */
140224 private function compareImages ($ image1 , $ image2 )
141225 {
142226 $ imagick1 = new \Imagick ($ image1 );
0 commit comments