From e2b3574a48da7ea78986c81420d62428ff479521 Mon Sep 17 00:00:00 2001 From: Zhivko Date: Wed, 5 Apr 2023 15:58:40 +0300 Subject: [PATCH] Fix full-screen screenshots --- module/VisualCeption.php | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/module/VisualCeption.php b/module/VisualCeption.php index 0c7e2f4..21f7744 100755 --- a/module/VisualCeption.php +++ b/module/VisualCeption.php @@ -31,7 +31,7 @@ class VisualCeption extends CodeceptionModule implements MultiSession 'module' => 'WebDriver', 'fullScreenShot' => false ]; - + protected $saveCurrentImageIfFailure; private $referenceImageDir; @@ -161,7 +161,7 @@ protected function getBrowserModule() { return null; } - + /** * Get value of the private property $referenceImageDir * @@ -179,7 +179,7 @@ public function getReferenceImageDir() * @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 + * @param float $deviation */ public function seeVisualChanges($identifier, $elementID = null, $excludeElements = array(), $deviation = null) { @@ -197,7 +197,7 @@ public function seeVisualChanges($identifier, $elementID = null, $excludeElement * @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 + * @param float $deviation */ public function dontSeeVisualChanges($identifier, $elementID = null, $excludeElements = array(), $deviation = null) { @@ -416,17 +416,23 @@ private function createScreenshot($identifier, array $coords, array $excludeElem list($viewportHeight, $devicePixelRatio) = $this->webDriver->executeScript("return [window.innerHeight, window.devicePixelRatio]"); $itr = $height / $viewportHeight; + $isViewPortHeightBiggerThanPageHeight = $height > $viewportHeight; - for ($i = 0; $i < intval($itr); $i++) { - $screenshotBinary = $this->webDriver->takeScreenshot(); - $screenShotImage->readimageblob($screenshotBinary); - $this->webDriver->executeScript("window.scrollBy(0, {$viewportHeight});"); + if ($isViewPortHeightBiggerThanPageHeight) { + 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); + + if ($isViewPortHeightBiggerThanPageHeight) { + $screenShotImage->cropImage(0, 0, 0, $heightOffset * $devicePixelRatio); + } $screenShotImage->resetIterator(); $fullShot = $screenShotImage->appendImages(true); @@ -605,4 +611,4 @@ public function _closeSession($session = null) { // this method will never be needed } -} \ No newline at end of file +}