Skip to content

Commit 9b2d58a

Browse files
author
Nils Langner
committed
Merge pull request #24 from phmLabs/master
Renamed functions to seeVisualChanged, dontSeeVisualChanges #
2 parents 28cbbb9 + 1c42cfb commit 9b2d58a

File tree

4 files changed

+92
-45
lines changed

4 files changed

+92
-45
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: php
22

33
php:
4+
- 5.3
45
- 5.4
56
- 5.5
67

@@ -10,6 +11,6 @@ before_script:
1011
- java -jar selenium-server-standalone-2.35.0.jar -port 4444 >/dev/null 2>&1 &
1112
- cd test/integration/
1213
- mkdir tests/_log
13-
14+
- php codecept.phar build
1415

1516
script: php codecept.phar run -d

module/VisualCeption.php

Lines changed: 66 additions & 36 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,8 +43,61 @@ public function _before (\Codeception\TestCase $test)
4243
$this->test = $test;
4344
}
4445

46+
private function getDeviation ($identifier, $elementID)
47+
{
48+
$coords = $this->getCoordinates($elementID);
49+
$this->createScreenshot($identifier, $coords);
50+
51+
$compareResult = $this->compare($identifier);
52+
53+
unlink($this->getScreenshotPath($identifier));
54+
55+
$deviation = round($compareResult[1] * 100, 2);
56+
$this->debug("The deviation between the images is ". $deviation . " percent");
57+
return array ("deviation" => $deviation, "deviationImage" => $compareResult[0]);
58+
}
59+
60+
/**
61+
* Compare the reference image with a current screenshot, identified by their indentifier name
62+
* and their element ID.
63+
*
64+
* @param string $identifier identifies your test object
65+
* @param string $elementID DOM ID of the element, which should be screenshotted
66+
*/
67+
public function dontSeeVisualChanges ($identifier, $elementID = null)
68+
{
69+
$deviationResult = $this->getDeviation($identifier, $elementID);
70+
if (! is_null($deviationResult["deviationImage"])) {
71+
if ($deviationResult["deviation"] > $this->maximumDeviation) {
72+
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
73+
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
74+
$this->assertTrue(false, "The deviation of the taken screenshot is too high (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.");
75+
}
76+
}
77+
}
78+
79+
/**
80+
* Compare the reference image with a current screenshot, identified by their indentifier name
81+
* and their element ID.
82+
*
83+
* @param string $identifier identifies your test object
84+
* @param string $elementID DOM ID of the element, which should be screenshotted
85+
*/
86+
public function seeVisualChanges ($identifier, $elementID = null)
87+
{
88+
$deviationResult = $this->getDeviation($identifier, $elementID);
89+
if (! is_null($deviationResult["deviationImage"])) {
90+
if ($deviationResult["deviation"] <= $this->maximumDeviation) {
91+
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
92+
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);
93+
$this->assertTrue(false, "The deviation of the taken screenshot is too low (" . $deviationResult["deviation"] . "%).\nSee $compareScreenshotPath for a deviation screenshot.");
94+
}
95+
}
96+
}
97+
4598
/**
46-
* Initialize the module and read the config. Throws a runtime exception, if the
99+
* Initialize the module and read the config.
100+
* Throws a runtime exception, if the
47101
* reference image dir is not set in the config
48102
*
49103
* @throws \RuntimeException
@@ -67,7 +121,8 @@ private function init ()
67121
}
68122

69123
/**
70-
* Find the position and proportion of a DOM element, specified by it's ID. The method inject the
124+
* Find the position and proportion of a DOM element, specified by it's ID.
125+
* The method inject the
71126
* JQuery Framework and uses the "noConflict"-mode to get the width, height and offset params.
72127
*
73128
* @param $elementId DOM ID of the element, which should be screenshotted
@@ -80,7 +135,7 @@ private function getCoordinates ($elementId)
80135
$elementId = 'body';
81136
}
82137

83-
$jQueryString = file_get_contents(__DIR__."/jquery.js");
138+
$jQueryString = file_get_contents(__DIR__ . "/jquery.js");
84139
$webDriver->executeScript($jQueryString);
85140
$webDriver->executeScript('jQuery.noConflict();');
86141

@@ -118,12 +173,11 @@ private function getScreenshotPath ($identifier)
118173
$debugDir = \Codeception\Configuration::logDir() . 'debug/tmp/';
119174
if (! is_dir($debugDir)) {
120175
$created = mkdir($debugDir, 0777, true);
121-
if( $created ) {
122-
$this->debug("Creating directory: $debugDir");
123-
}else{
176+
if ($created) {
177+
$this->debug("Creating directory: $debugDir");
178+
} else {
124179
throw new \RuntimeException("Unable to create temporary screenshot dir ($debugDir)");
125180
}
126-
127181
}
128182
return $debugDir . $this->getScreenshotName($identifier);
129183
}
@@ -157,40 +211,15 @@ private function createScreenshot ($identifier, array $coords)
157211
$webDriver->takeScreenshot($screenshotPath);
158212

159213
$screenShotImage = new \Imagick();
160-
$screenShotImage->readImage( $screenshotPath );
161-
$screenShotImage->cropImage( $coords['width'], $coords['height'], $coords['offset_x'], $coords['offset_y'] );
162-
$screenShotImage->writeImage( $elementPath );
214+
$screenShotImage->readImage($screenshotPath);
215+
$screenShotImage->cropImage($coords['width'], $coords['height'], $coords['offset_x'], $coords['offset_y']);
216+
$screenShotImage->writeImage($elementPath);
163217

164218
unlink($screenshotPath);
165219

166220
return $elementPath;
167221
}
168222

169-
/**
170-
* Compare the reference image with a current screenshot, identified by their indentifier name
171-
* and their element ID
172-
*
173-
* @param string $identifier identifies your test object
174-
* @param string $elementID DOM ID of the element, which should be screenshotted
175-
*/
176-
public function compareScreenshot ($identifier, $elementID = null)
177-
{
178-
$coords = $this->getCoordinates($elementID);
179-
$this->createScreenshot($identifier, $coords);
180-
181-
$compareResult = $this->compare($identifier);
182-
183-
unlink($this->getScreenshotPath($identifier));
184-
185-
$deviation = round($compareResult[1] * 100, 2);
186-
187-
if ($deviation > $this->maximumDeviation) {
188-
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
189-
$compareResult[0]->writeImage($compareScreenshotPath);
190-
$this->assertTrue(false, "The deviation of the taken screenshot is too high (".$deviation."%).\nSee $compareScreenshotPath for a deviation screenshot.");
191-
}
192-
}
193-
194223
/**
195224
* Returns the image path including the filename of a deviation image
196225
*
@@ -204,7 +233,8 @@ private function getDeviationScreenshotPath ($identifier)
204233
}
205234

206235
/**
207-
* Compare two images by its identifiers. If the reference image doesn't exists
236+
* Compare two images by its identifiers.
237+
* If the reference image doesn't exists
208238
* the image is copied to the reference path.
209239
*
210240
* @param $identifier identifies your test object

readme.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# VisualCeption
22
Visual regression tests integrated in [Codeception](http://codeception.com/).
33

4+
[![Build Status](https://travis-ci.org/DigitalProducts/codeception-module-visualception.svg?branch=master)](https://travis-ci.org/DigitalProducts/codeception-module-visualception)
5+
46
This module can be used to compare the current representation of a website element with an expeted. It was written on the shoulders of codeception and integrates in a very easy way.
57

68
**Example**
@@ -52,20 +54,23 @@ VisualCeption:
5254
5355
## Usage
5456
55-
VisualCeption is really easy to use. There is only one method that will be added to your WebGuy <code>compareScreenshot</code>. This will be used to name the screenshot and identify the elements that has to be screenshot.
57+
VisualCeption is really easy to use. There are only two method that will be added to your WebGuy <code>seeVisualChanges</code> and <code>dontSeeVisualChanges</code>.
5658
5759
```php
58-
$I->compareScreenshot( "uniqueIdentifier", "elementId" );
60+
$I->seeVisualChanges( "uniqueIdentifier1", "elementId1" );
61+
$I->dontSeeVisualChanges( "uniqueIdentifier2", "elementId2" );
5962
```
6063

6164
* **uniqueIdentifier** For comparing the images it is important to have a stable name. This is the corresponding name.
6265
* **elementId** It is possible to only compare a special div container. The element id can be passed. *You can use all locators that can be used in jQuery*.
6366

6467
**Example Usage**
6568
```php
66-
$I->compareScreenshot( "subNavigation", "#subNav" );
69+
$I->seeVisualChanges( "subNavigation", "#subNav" );
6770
```
6871

72+
If you need more information about the test run please use the command line debug option (-d).
73+
6974
## Restriction
7075

7176
VisualCeption uses the WebDriver module for making the screenshots. As a consequence we are not able to take screenshots via google chrome as the chromedriver does not allow full page screenshots.

test/integration/tests/acceptance/TimeComparisonCest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ class TimeComparisonCest
66
/**
77
* Coparing a div that renders the current time
88
*/
9-
public function compareTimeString (WebGuy $I, $scenario)
9+
public function seeVisualChanges (WebGuy $I, $scenario)
1010
{
11-
$I->amOnPage("/VisualCeption/time.php");
12-
$I->compareScreenshot("the-time", "#thetime");
11+
$I->amOnPage("/VisualCeption/seeVisualChanges.php");
12+
$I->seeVisualChanges("block", "#theblock");
1313

1414
// the test has to be called twice for comparison on the travis server
15-
$I->amOnPage("/VisualCeption/time.php");
16-
$I->compareScreenshot("the-time", "#thetime");
15+
$I->amOnPage("/VisualCeption/seeVisualChanges.php");
16+
$I->seeVisualChanges("block", "#theblock");
1717
}
18+
19+
public function dontSeeVisualChanges (WebGuy $I, $scenario)
20+
{
21+
$I->amOnPage("/VisualCeption/dontSeeVisualChanges.php");
22+
$I->dontSeeVisualChanges("block", "#theblock");
23+
24+
// the test has to be called twice for comparison on the travis server
25+
$I->amOnPage("/VisualCeption/dontSeeVisualChanges.php");
26+
$I->dontSeeVisualChanges("block", "#theblock");
27+
}
28+
1829
}

0 commit comments

Comments
 (0)