Skip to content

Commit

Permalink
Merge pull request #7 from 1FF/change-retries-times-with-timeout
Browse files Browse the repository at this point in the history
Change retries times with timeout
  • Loading branch information
GeorgiDrumev authored Sep 27, 2024
2 parents 1850f19 + 391963b commit 63d4292
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/Codeception/Module/VisualCeption.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public function dontSeeVisualChangesForViewPort(string $identifier, string $elem
* @param $deviation
* @param $seeChanges
* @return void
* @throws \Exception
*/
private function compareVisualChanges($identifier, $elementID, $excludeElements, $deviation, $seeChanges, $fullScreenshot = true): void
{
Expand All @@ -274,13 +275,20 @@ private function compareVisualChanges($identifier, $elementID, $excludeElements,
$outOfMaxDeviation = !$seeChanges && $deviationResult["deviation"] > $maximumDeviation;

if ($outOfMaxDeviation) {
$retries = 0;
$startTime = time();
$timeout = 15;

while ($outOfMaxDeviation) {
$currentTime = time() - $startTime;

if($currentTime > $timeout){
break;
}

while ($retries < 5 && $outOfMaxDeviation){
$deviationResult = $this->getDeviation($identifier, $elementID, $fullScreenshot, $excludeElements);
$outOfMaxDeviation = $deviationResult["deviation"] > $maximumDeviation;
$retries++;
sleep(1);
$this->webDriver->executeScript("window.scrollTo(0, 0);");
sleep(2);
}
}

Expand All @@ -289,7 +297,7 @@ private function compareVisualChanges($identifier, $elementID, $excludeElements,
$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);

throw $this->createImageDeviationException($identifier, $compareScreenshotPath, $deviationResult["deviation"], $seeChanges);
throw $this->createImageDeviationException($identifier, $compareScreenshotPath, $deviationResult["deviation"], $seeChanges, $currentTime);
}
}

Expand All @@ -300,14 +308,15 @@ private function compareVisualChanges($identifier, $elementID, $excludeElements,
* @param $seeChanges
* @return \Codeception\Exception\ImageDeviationException
*/
private function createImageDeviationException($identifier, $compareScreenshotPath, $deviation, $seeChanges): ImageDeviationException
private function createImageDeviationException($identifier, $compareScreenshotPath, $deviation, $seeChanges, $currentTime = null): ImageDeviationException
{
$message = "The deviation of the taken screenshot is too high";
if ($seeChanges) {
$message = "The deviation of the taken screenshot is too low";
$message = "The deviation of the taken screenshot is too low ({$deviation}%) \nSee {$compareScreenshotPath} for a deviation screenshot.";
}

$message .= " ({$deviation}%) \nSee {$compareScreenshotPath} for a deviation screenshot.";
if(!$seeChanges){
$message = " ({$deviation}%) \nSee {$compareScreenshotPath} for a deviation screenshot with timeout $currentTime seconds.";
}

return new ImageDeviationException(
$message,
Expand Down

0 comments on commit 63d4292

Please sign in to comment.