Skip to content

Commit 057a3ac

Browse files
author
Neubert, Sebastian
committed
pull request #27: add improvements, worked on comments for pull request. fixed tests
1 parent 01d81a2 commit 057a3ac

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

module/VisualCeption.php

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ public function _before (\Codeception\TestCase $test)
5959
*/
6060
public function seeVisualChanges ($identifier, $elementID = null, $excludeElements = array())
6161
{
62-
if (!is_array($excludeElements) && (string) $excludeElements !== '') {
63-
$excludeElements = (array) $excludeElements;
64-
}
62+
$excludeElements = (array) $excludeElements;
6563

6664
$deviationResult = $this->getDeviation($identifier, $elementID, $excludeElements);
6765

@@ -84,9 +82,7 @@ public function seeVisualChanges ($identifier, $elementID = null, $excludeElemen
8482
*/
8583
public function dontSeeVisualChanges ($identifier, $elementID = null, $excludeElements = array())
8684
{
87-
if (!is_array($excludeElements) && (string) $excludeElements !== '') {
88-
$excludeElements = (array) (string)$excludeElements;
89-
}
85+
$excludeElements = (array) $excludeElements;
9086

9187
$deviationResult = $this->getDeviation($identifier, $elementID, $excludeElements);
9288

@@ -99,6 +95,36 @@ public function dontSeeVisualChanges ($identifier, $elementID = null, $excludeEl
9995
}
10096
}
10197

98+
/**
99+
* Hide an element to set the visibility to hidden
100+
*
101+
* @param $elementSelector String of jQuery Element selector, set visibility to hidden
102+
*/
103+
public function hideElement($elementSelector)
104+
{
105+
$this->webDriver->executeScript('
106+
if( jQuery("'.$elementSelector.'").length > 0 ) {
107+
jQuery( "'.$elementSelector.'" ).css("visibility","hidden");
108+
}
109+
');
110+
$this->debug("set visibility of element '$elementSelector' to 'hidden'");
111+
}
112+
113+
/**
114+
* Show an element to set the visibility to visible
115+
*
116+
* @param $elementSelector String of jQuery Element selector, set visibility to visible
117+
*/
118+
public function showElement($elementSelector)
119+
{
120+
$this->webDriver->executeScript('
121+
if( jQuery("'.$elementSelector.'").length > 0 ) {
122+
jQuery( "'.$elementSelector.'" ).css("visibility","visible");
123+
}
124+
');
125+
$this->debug("set visibility of element '$elementSelector' to 'visible'");
126+
}
127+
102128
/**
103129
* Compares the two images and calculate the deviation between expected and actual image
104130
*
@@ -165,10 +191,10 @@ private function getCoordinates ($elementId)
165191
$this->webDriver->executeScript('jQuery.noConflict();');
166192

167193
$imageCoords = array ();
168-
$imageCoords['offset_x'] = (string) $this->webDriver->executeScript('var element = jQuery( "' . $elementId . '" );var offset = element.offset();return offset.left;');
169-
$imageCoords['offset_y'] = (string) $this->webDriver->executeScript('var element = jQuery( "' . $elementId . '" );var offset = element.offset();return offset.top;');
170-
$imageCoords['width'] = (string) $this->webDriver->executeScript('var element = jQuery( "' . $elementId . '" );return element.width();');
171-
$imageCoords['height'] = (string) $this->webDriver->executeScript('var element = jQuery( "' . $elementId . '" );return element.height();');
194+
$imageCoords['offset_x'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).offset().left;');
195+
$imageCoords['offset_y'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).offset().top;');
196+
$imageCoords['width'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).width();');
197+
$imageCoords['height'] = (string) $this->webDriver->executeScript('return jQuery( "' . $elementId . '" ).height();');
172198

173199
return $imageCoords;
174200
}
@@ -237,16 +263,9 @@ private function createScreenshot ($identifier, array $coords, array $excludeEle
237263
$screenshotPath = \Codeception\Configuration::logDir() . 'debug/' . "fullscreenshot.tmp.png";
238264
$elementPath = $this->getScreenshotPath($identifier);
239265

240-
// exclude Elements here
241-
if (false !== reset($excludeElements)) {
242-
$this->hideElementsForScreenshot($excludeElements);
243-
}
244-
266+
$this->hideElementsForScreenshot($excludeElements);
245267
$this->webDriver->takeScreenshot($screenshotPath);
246-
247-
if (false !== reset($excludeElements)) {
248-
$this->resetHideElementsForScreenshot($excludeElements);
249-
}
268+
$this->resetHideElementsForScreenshot($excludeElements);
250269

251270
$screenShotImage = new \Imagick();
252271
$screenShotImage->readImage($screenshotPath);
@@ -266,8 +285,7 @@ private function createScreenshot ($identifier, array $coords, array $excludeEle
266285
private function hideElementsForScreenshot(array $excludeElements)
267286
{
268287
foreach ($excludeElements as $element) {
269-
$this->webDriver->executeScript('var element = jQuery( "'.$element.'" ).css("visibility","hidden");');
270-
$this->debug("set visibility of element '$element' to 'hidden'");
288+
$this->hideElement($element);
271289
}
272290
$this->webDriverModule->wait(1);
273291
}
@@ -280,8 +298,7 @@ private function hideElementsForScreenshot(array $excludeElements)
280298
private function resetHideElementsForScreenshot(array $excludeElements)
281299
{
282300
foreach ($excludeElements as $element) {
283-
$this->webDriver->executeScript('var element = jQuery( "'.$element.'" ).css("visibility","visible");');
284-
$this->debug("set visibility of element '$element' to 'visible'");
301+
$this->showElement($element);
285302
}
286303
$this->webDriverModule->wait(1);
287304
}

test/integration/tests/acceptance/TimeComparisonCest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ public function seeVisualChangesAndHideElement (WebGuy $I, $scenario)
3535
$I->amOnPage("/VisualCeption/seeVisualChanges.php");
3636
$I->seeVisualChanges("body", "body", "#intro");
3737

38+
$I->wait(1);
39+
3840
// the test has to be called twice for comparison on the travis server
3941
$I->amOnPage("/VisualCeption/seeVisualChanges.php");
40-
$I->dontSeeVisualChanges("body", "body", array("#intro"));
42+
$I->seeVisualChanges("body", "body", array("#intro"));
4143
}
4244

4345
public function dontSeeVisualChangesAndHideElement (WebGuy $I, $scenario)
4446
{
4547
$I->amOnPage("/VisualCeption/seeVisualChanges.php");
4648
$I->dontSeeVisualChanges("body", "body", "#theblock");
4749

50+
$I->wait(1);
51+
4852
// the test has to be called twice for comparison on the travis server
4953
$I->amOnPage("/VisualCeption/seeVisualChanges.php");
5054
$I->dontSeeVisualChanges("body", "body", array("#theblock"));

0 commit comments

Comments
 (0)