forked from DigitalProducts/codeception-module-visualception
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathVisualCeption.php
More file actions
executable file
·136 lines (120 loc) · 4.79 KB
/
Copy pathVisualCeption.php
File metadata and controls
executable file
·136 lines (120 loc) · 4.79 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
<?php
namespace Codeception\Module;
use Codeception\Module\ImageDeviationException;
/**
* 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
*/
class VisualCeption extends \Codeception\Module
{
private $maximumDeviation = 0;
/**
* @var \RemoteWebDriver
*/
private $webDriver = null;
/**
* @var \Storage
*/
private $storageStrategy;
/**
* Create an object from VisualCeption Class
*
* @param array $config
* @return result
*/
public function __construct($config)
{
$result = parent::__construct($config);
$this->init();
return $result;
}
/**
* Initialize the module and read the config.
*
* @throws \RuntimeException
*/
private function init()
{
if (array_key_exists('maximumDeviation', $this->config)) {
$this->maximumDeviation = $this->config["maximumDeviation"];
}
$this->storageStrategy = \Factory::getStorage($this->config);
}
/**
* Event hook before a test starts
*
* @param \Codeception\TestCase $test
* @throws \Exception
*/
public function _before(\Codeception\TestCase $test)
{
if (!$this->hasModule("WebDriver")) {
throw new \Exception("VisualCeption uses the WebDriver. Please be sure that this module is activated.");
}
$this->webDriver = $this->getModule("WebDriver")->webDriver;
}
/**
* 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
*/
public function seeVisualChanges($identifier, $elementId = null, $excludedElements = array())
{
$comparisonResult = $this->getVisualChanges($identifier, $elementId, (array)$excludedElements);
if($comparisonResult->getDeviation() <= $this->maximumDeviation ) {
$this->assertTrue(true);
throw new ImageDeviationException("The deviation of the taken screenshot is too low (" . $comparisonResult->getDeviation() . "%)",
$comparisonResult, $this->storageStrategy, $identifier);
}
}
/**
* 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
*/
public function dontSeeVisualChanges($identifier, $elementId = null, $excludedElements = array())
{
$comparisonResult = $this->getVisualChanges($identifier, $elementId, (array)$excludedElements);
if($comparisonResult->getDeviation() > $this->maximumDeviation ) {
$this->assertTrue(true);
throw new ImageDeviationException("The deviation of the taken screenshot is too high (" . $comparisonResult->getDeviation() . "%)",
$comparisonResult, $this->storageStrategy, $identifier);
}
}
private function getVisualChanges($identifier, $elementId, array $excludedElements)
{
$expectedImage = $this->storageStrategy->getImage($identifier);
$currentImage = $this->getCurrentImage($excludedElements, $elementId);
return $this->getComparisonResult($expectedImage, $currentImage);
}
private function getComparisonResult(\Imagick $expectedImage, \Imagick $currentImage)
{
try {
$imageCompare = new \Comparison();
return $imageCompare->compare($expectedImage, $currentImage);
} catch (\ImagickException $e) {
$this->debug("IMagickException! Could not compare images.\nExceptionMessage: " . $e->getMessage());
$this->fail($e->getMessage());
}
}
private function getCurrentImage(array $excludedElements, $elementId)
{
$htmlManipulator = new \Manipulation($this->webDriver);
$htmlManipulator->hideElements($excludedElements);
$htmlScreenshot = new \Screenshot($this->webDriver);
return $htmlScreenshot->takeScreenshot($elementId);
}
}