forked from nixdrey/VisualCeption
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualCeption.php
More file actions
executable file
·616 lines (522 loc) · 21.1 KB
/
VisualCeption.php
File metadata and controls
executable file
·616 lines (522 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<?php
namespace Codeception\Module;
use Codeception\Lib\Interfaces\MultiSession;
use Codeception\Module as CodeceptionModule;
use Codeception\Test\Descriptor;
use Facebook\WebDriver\Exception\UnknownServerException;
use RemoteWebDriver;
use Codeception\Module\VisualCeption\Utils;
use Codeception\TestInterface;
/**
* Class VisualCeption
*
* @copyright Copyright (c) 2014 G+J Digital Products GmbH
* @license MIT license, http://www.opensource.org/licenses/mit-license.php
* @package Codeception\Module
*
* @author Nils Langner <langner.nils@guj.de>
* @author Torsten Franz
* @author Sebastian Neubert
* @author Ray Romanov
*/
class VisualCeption extends CodeceptionModule implements MultiSession
{
protected $config = [
'maximumDeviation' => 0,
'saveCurrentImageIfFailure' => true,
'pixelRatio' => 1.0,
'compressionLevel' => 0,
'referenceImageDir' => 'VisualCeption/',
'currentImageDir' => 'debug/visual/',
'report' => false,
'module' => 'WebDriver',
'fullScreenShot' => false
];
protected $saveCurrentImageIfFailure;
protected $pixelRatio;
protected $compressionLevel;
private $referenceImageDir;
/**
* This var represents the directory where the taken images are stored
* @var string
*/
private $currentImageDir;
private $maximumDeviation = 0;
/**
* @var RemoteWebDriver
*/
private $webDriver = null;
/**
* @var WebDriver
*/
private $webDriverModule = null;
/**
* @var Utils
*/
private $utils;
/**
* @var TestInterface
*/
private $test;
private $failed = array();
private $logFile;
private $templateVars = array();
private $templateFile;
public function _initialize()
{
$this->utils = new Utils();
$this->maximumDeviation = $this->config["maximumDeviation"];
$this->saveCurrentImageIfFailure = (boolean)$this->config["saveCurrentImageIfFailure"];
$this->pixelRatio = (float)$this->config["pixelRatio"];
$this->compressionLevel = (int)$this->config["compressionLevel"];
$this->referenceImageDir = (file_exists($this->config["referenceImageDir"]) ? "" : codecept_data_dir()) . $this->config["referenceImageDir"];
if (!is_dir($this->referenceImageDir)) {
$this->debug("Creating directory: $this->referenceImageDir");
@mkdir($this->referenceImageDir, 0777, true);
}
$this->currentImageDir = codecept_output_dir() . $this->config["currentImageDir"];
}
public function _beforeSuite($settings = [])
{
$this->_initVisualReport();
}
public function _afterSuite()
{
if (!$this->config['report']) {
return;
}
$failedTests = $this->failed;
$vars = $this->templateVars;
$referenceImageDir = $this->referenceImageDir;
$i = 0;
ob_start();
include $this->templateFile;
$reportContent = ob_get_contents();
ob_end_clean();
$this->debug("Trying to store file (".$this->logFile.")");
file_put_contents($this->logFile, $reportContent);
}
public function _failed(TestInterface $test, $fail)
{
if ($fail instanceof ImageDeviationException) {
$this->failed[Descriptor::getTestSignatureUnique($test) . '.' . $fail->getIdentifier()] = $fail;
}
}
/**
* Event hook before a test starts
*
* @param TestInterface $test
* @throws \Exception
*/
public function _before(TestInterface $test)
{
$browserModule = $this->getBrowserModule();
if ($browserModule === null) {
throw new \Codeception\Exception\ConfigurationException("VisualCeption uses the WebDriver. Please ensure that this module is activated.");
}
if (!class_exists('Imagick')) {
throw new \Codeception\Exception\ConfigurationException("VisualCeption requires ImageMagick PHP Extension but it was not installed");
}
$this->webDriverModule = $browserModule;
$this->webDriver = $this->webDriverModule->webDriver;
$this->test = $test;
}
protected function getBrowserModule() {
if ($this->hasModule($this->config['module'])) {
return $this->getModule($this->config['module']);
}
foreach($this->getModules() as $module) {
if($module === $this) {
continue;
}
if($module instanceof WebDriver) {
return $module;
}
}
return null;
}
/**
* Get value of the private property $referenceImageDir
*
* @return string Path to reference image dir
*/
public function getReferenceImageDir()
{
return $this->referenceImageDir;
}
/**
* Compare the reference image with a current screenshot, identified by their indentifier name
* and their element ID.
*
* @param string $identifier Identifies your test object
* @param string $elementID DOM ID of the element, which should be screenshotted
* @param string|array $excludeElements Element name or array of Element names, which should not appear in the screenshot
* @param float $deviation
*/
public function seeVisualChanges($identifier, $elementID = null, $excludeElements = array(), $deviation = null)
{
$this->compareVisualChanges($identifier, $elementID, $excludeElements, $deviation, true);
// used for assertion counter in codeception / phpunit
$this->assertTrue(true);
}
/**
* Compare the reference image with a current screenshot, identified by their indentifier name
* and their element ID.
*
* @param string $identifier identifies your test object
* @param string $elementID DOM ID of the element, which should be screenshotted
* @param string|array $excludeElements string of Element name or array of Element names, which should not appear in the screenshot
* @param float $deviation
*/
public function dontSeeVisualChanges($identifier, $elementID = null, $excludeElements = array(), $deviation = null)
{
$this->compareVisualChanges($identifier, $elementID, $excludeElements, $deviation, false);
// used for assertion counter in codeception / phpunit
$this->assertTrue(true);
}
private function compareVisualChanges($identifier, $elementID, $excludeElements, $deviation, $seeChanges)
{
$excludeElements = (array) $excludeElements;
$maximumDeviation = (!$deviation && !is_numeric($deviation)) ? $this->maximumDeviation : (float) $deviation;
$deviationResult = $this->getDeviation($identifier, $elementID, $excludeElements);
if (is_null($deviationResult["deviationImage"])) {
return;
}
if ($seeChanges && $deviationResult["deviation"] <= $maximumDeviation ||
!$seeChanges && $deviationResult["deviation"] > $maximumDeviation) {
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
throw $this->createImageDeviationException($identifier, $compareScreenshotPath, $deviationResult["deviation"], $seeChanges);
}
}
private function createImageDeviationException($identifier, $compareScreenshotPath, $deviation, $seeChanges)
{
if ($seeChanges) {
$message = "The deviation of the taken screenshot is too low";
} else {
$message = "The deviation of the taken screenshot is too high";
}
$message .= " (" . $deviation . "%).\nSee $compareScreenshotPath for a deviation screenshot.";
return new ImageDeviationException(
$message,
$identifier,
$this->getExpectedScreenshotPath($identifier),
$this->getScreenshotPath($identifier),
$compareScreenshotPath
);
}
/**
* Hide an element to set the visibility to hidden
*
* @param $elementSelector String of CSS Element selector, set visibility to hidden
*/
private function hideElement($elementSelector)
{
$this->setVisibility($elementSelector, false);
}
/**
* Show an element to set the visibility to visible
*
* @param $elementSelector String of CSS Element selector, set visibility to visible
*/
private function showElement($elementSelector)
{
$this->setVisibility($elementSelector, true);
}
private function setVisibility($elementSelector, $isVisible)
{
$styleVisibility = $isVisible ? 'visible' : 'hidden';
$this->webDriver->executeScript('
var elements = [];
elements = document.querySelectorAll("' . $elementSelector . '");
if( elements.length > 0 ) {
for (var i = 0; i < elements.length; i++) {
elements[i].style.visibility = "' . $styleVisibility . '";
}
}
');
$this->debug("set visibility of element '$elementSelector' to '$styleVisibility'");
}
/**
* Compares the two images and calculate the deviation between expected and actual image
*
* @param string $identifier Identifies your test object
* @param string $elementID DOM ID of the element, which should be screenshotted
* @param array $excludeElements Element names, which should not appear in the screenshot
* @return array Includes the calculation of deviation in percent and the diff-image
*/
private function getDeviation($identifier, $elementID, array $excludeElements = array())
{
$coords = $this->getCoordinates($elementID);
$this->createScreenshot($identifier, $coords, $excludeElements);
$compareResult = $this->compare($identifier);
$deviation = $compareResult[1] * 100;
$this->debug("The deviation between the images is ". $deviation . " percent");
return array (
"deviation" => $deviation,
"deviationImage" => $compareResult[0],
"currentImage" => $compareResult['currentImage'],
);
}
/**
* Initialize the module and read the config.
* Throws a runtime exception, if the
* reference image dir is not set in the config
*
* @throws \RuntimeException
*/
/**
* Find the position and proportion of a DOM element, specified by it's ID.
* Used native JavaScript.
* @param string $elementId DOM ID of the element, which should be screenshotted
* @return array coordinates of the element
*/
private function getCoordinates($elementId)
{
if (is_null($elementId)) {
$elementId = 'body';
} else {
// escape double quotes to not break JavaScript commands
$elementId = str_replace('"', '\\"', $elementId);
}
$elementExists = (bool)$this->webDriver->executeScript('return document.querySelectorAll( "' . $elementId . '" ).length > 0;');
if (!$elementExists) {
throw new \Exception("The element you want to examine ('" . $elementId . "') was not found.");
}
$imageCoords = $this->webDriver->executeScript('
var rect = document.querySelector( "' . $elementId . '" ).getBoundingClientRect();
return {"offset_x": rect.left, "offset_y": rect.top, "width": rect.width, "height": rect.height};
');
return $imageCoords;
}
/**
* Generates a screenshot image filename
* it uses the testcase name and the given indentifier to generate a png image name
*
* @param string $identifier identifies your test object
* @return string Name of the image file
*/
private function getScreenshotName($identifier)
{
return $this->utils->getTestFileName($this->test, $identifier);
}
/**
* Returns the temporary path including the filename where a the screenshot should be saved
* If the path doesn't exist, the method generate it itself
*
* @param string $identifier identifies your test object
* @return string Path an name of the image file
* @throws \RuntimeException if debug dir could not create
*/
private function getScreenshotPath($identifier)
{
$debugDir = $this->currentImageDir;
if (!is_dir($debugDir)) {
$created = @mkdir($debugDir, 0777, true);
if ($created) {
$this->debug("Creating directory: $debugDir");
} else {
throw new \RuntimeException("Unable to create temporary screenshot dir ($debugDir)");
}
}
return $debugDir . $this->getScreenshotName($identifier);
}
/**
* Returns the reference image path including the filename
*
* @param string $identifier identifies your test object
* @return string Name of the reference image file
*/
private function getExpectedScreenshotPath($identifier)
{
return $this->referenceImageDir . $this->getScreenshotName($identifier);
}
/**
* Generate the screenshot of the dom element
*
* @param string $identifier identifies your test object
* @param array $coords Coordinates where the DOM element is located
* @param array $excludeElements List of elements, which should not appear in the screenshot
* @return string Path of the current screenshot image
*/
private function createScreenshot($identifier, array $coords, array $excludeElements = array())
{
$screenShotDir = \Codeception\Configuration::logDir() . 'debug/';
if (!is_dir($screenShotDir)) {
mkdir($screenShotDir, 0777, true);
}
$elementPath = $this->getScreenshotPath($identifier);
$screenShotImage = new \Imagick();
$screenShotImage->setOption('png:compression-level', $this->config['compressionLevel']);
$this->hideElementsForScreenshot($excludeElements);
if ($this->config["fullScreenShot"] == true) {
$height = $this->webDriver->executeScript("var ele=document.querySelector('html'); return ele.scrollHeight;");
list($viewportHeight, $devicePixelRatio) = $this->webDriver->executeScript("return [window.innerHeight, window.devicePixelRatio]");
$itr = $height / $viewportHeight;
for ($i = 0; $i < intval($itr); $i++) {
$screenshotBinary = $this->webDriver->takeScreenshot();
$screenShotImage->readimageblob($screenshotBinary);
$this->webDriver->executeScript("window.scrollBy(0, {$viewportHeight});");
}
$screenshotBinary = $this->webDriver->takeScreenshot();
$screenShotImage->readimageblob($screenshotBinary);
$heightOffset = $viewportHeight - ($height - (intval($itr) * $viewportHeight));
$screenShotImage->cropImage(0, 0, 0, $heightOffset * $devicePixelRatio);
$screenShotImage->resetIterator();
$fullShot = $screenShotImage->appendImages(true);
$fullShot->writeImage($elementPath);
} else {
$screenshotBinary = $this->webDriver->takeScreenshot();
$screenShotImage->readimageblob($screenshotBinary);
$screenShotImage->cropImage(
$coords['width'] * $this->pixelRatio,
$coords['height'] * $this->pixelRatio,
$coords['offset_x'] * $this->pixelRatio,
$coords['offset_y'] * $this->pixelRatio
);
$screenShotImage->writeImage($elementPath);
}
$this->resetHideElementsForScreenshot($excludeElements);
return $elementPath;
}
/**
* Hide the given elements with CSS visibility = hidden. Wait a second after hiding
*
* @param array $excludeElements Array of strings, which should be not visible
*/
private function hideElementsForScreenshot(array $excludeElements)
{
foreach ($excludeElements as $element) {
$this->hideElement($element);
}
if (!empty($excludeElements)) {
$this->webDriverModule->waitForElementNotVisible(array_pop($excludeElements));
}
}
/**
* Reset hiding the given elements with CSS visibility = visible. Wait a second after reset hiding
*
* @param array $excludeElements array of strings, which should be visible again
*/
private function resetHideElementsForScreenshot(array $excludeElements)
{
foreach ($excludeElements as $element) {
$this->showElement($element);
}
if (!empty($excludeElements)) {
$this->webDriverModule->waitForElementVisible(array_pop($excludeElements));
}
}
/**
* Returns the image path including the filename of a deviation image
*
* @param $identifier identifies your test object
* @return string Path of the deviation image
*/
private function getDeviationScreenshotPath ($identifier, $alternativePrefix = '')
{
$debugDir = \Codeception\Configuration::logDir() . 'debug/';
$prefix = ( $alternativePrefix === '') ? 'compare' : $alternativePrefix;
return $debugDir . $prefix . $this->getScreenshotName($identifier);
}
/**
* Compare two images by its identifiers.
* If the reference image doesn't exists
* the image is copied to the reference path.
*
* @param $identifier identifies your test object
* @return array Test result of image comparison
*/
private function compare($identifier)
{
$expectedImagePath = $this->getExpectedScreenshotPath($identifier);
$currentImagePath = $this->getScreenshotPath($identifier);
if (!file_exists($expectedImagePath)) {
$this->debug("Copying image (from $currentImagePath to $expectedImagePath");
copy($currentImagePath, $expectedImagePath);
throw (new \Exception("Reference image $expectedImagePath has just been saved. Start test again. If it's CI run - please add reference."));
} else {
return $this->compareImages($expectedImagePath, $currentImagePath);
}
}
/**
* Compares to images by given file path
*
* @param $image1 Path to the exprected reference image
* @param $image2 Path to the current image in the screenshot
* @return array Result of the comparison
*/
private function compareImages($image1, $image2)
{
$this->debug("Trying to compare $image1 with $image2");
$imagick1 = new \Imagick($image1);
$imagick2 = new \Imagick($image2);
$imagick1->setOption('png:compression-level', $this->config['compressionLevel']);
$imagick2->setOption('png:compression-level', $this->config['compressionLevel']);
$imagick1Size = $imagick1->getImageGeometry();
$imagick2Size = $imagick2->getImageGeometry();
$maxWidth = max($imagick1Size['width'], $imagick2Size['width']);
$maxHeight = max($imagick1Size['height'], $imagick2Size['height']);
$imagick1->extentImage($maxWidth, $maxHeight, 0, 0);
$imagick2->extentImage($maxWidth, $maxHeight, 0, 0);
try {
$result = $imagick1->compareImages($imagick2, \Imagick::METRIC_MEANSQUAREERROR);
$result[0]->setImageFormat('png');
$result['currentImage'] = clone $imagick2;
$result['currentImage']->setImageFormat('png');
}
catch (\ImagickException $e) {
$this->debug("IMagickException! could not campare image1 ($image1) and image2 ($image2).\nExceptionMessage: " . $e->getMessage());
$this->fail($e->getMessage() . ", image1 $image1 and image2 $image2.");
}
return $result;
}
protected function _initVisualReport()
{
if (!$this->config['report']) {
return;
}
$filename = 'vcresult';
$this->logFile = \Codeception\Configuration::logDir() . $filename . '.html';
if (array_key_exists('templateVars', $this->config)) {
$this->templateVars = $this->config["templateVars"];
}
if (array_key_exists('templateFile', $this->config)) {
$this->templateFile = (file_exists($this->config["templateFile"]) ? "" : __DIR__ ) . $this->config["templateFile"];
} else {
$this->templateFile = __DIR__ . "/report/template.php";
}
$this->debug( "VisualCeptionReporter: templateFile = " . $this->templateFile );
}
/**
* Get a new loaded module
*/
public function _initializeSession()
{
$browserModule = $this->getBrowserModule();
$this->webDriverModule = $browserModule;
$this->webDriver = $this->webDriverModule->webDriver;
}
/**
* Loads current RemoteWebDriver instance as a session
*
* @param $session
*/
public function _loadSession($session)
{
$this->webDriver = $session;
}
/**
* Returns current WebDriver session for saving
*
* @return RemoteWebDriver
*/
public function _backupSession()
{
return $this->webDriver;
}
public function _closeSession($session = null)
{
// this method will never be needed
}
}