Skip to content

Commit

Permalink
Add retries for visual screenshots (#5)
Browse files Browse the repository at this point in the history
* Add retry logic for screenshots

* fix infinite loop

* fix docker compose command in ci.yml

---------

Co-authored-by: Georgi Drumev <[email protected]>
  • Loading branch information
GeorgiDrumev and Georgi Drumev authored Sep 20, 2024
1 parent 5a8da27 commit 61b8eee
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
uses: actions/checkout@v1

- name: Run tests on docker compose
run: docker-compose up --abort-on-container-exit
run: docker compose up --abort-on-container-exit
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ VisualCeption uses the WebDriver module for making the screenshots. As a consequ

## Run tests with Docker
```
docker-compose up --abort-on-container-exit
docker compose up --abort-on-container-exit
```
20 changes: 18 additions & 2 deletions src/Codeception/Module/VisualCeption.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,21 @@ private function compareVisualChanges($identifier, $elementID, $excludeElements,
return;
}

if (($seeChanges && $deviationResult["deviation"] <= $maximumDeviation) || (!$seeChanges && $deviationResult["deviation"] > $maximumDeviation)) {
$outOfMaxDeviation = !$seeChanges && $deviationResult["deviation"] > $maximumDeviation;

if ($outOfMaxDeviation) {
$retries = 0;

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

if (($seeChanges && $deviationResult["deviation"] <= $maximumDeviation) || $outOfMaxDeviation) {

$compareScreenshotPath = $this->getDeviationScreenshotPath($identifier);
$deviationResult["deviationImage"]->writeImage($compareScreenshotPath);

Expand Down Expand Up @@ -455,6 +469,7 @@ private function getExpectedScreenshotPath($identifier)
* @param array $excludeElements List of elements, which should not appear in the screenshot
* @return string Path of the current screenshot image
* @throws \ImagickException
* @throws \Exception
*/
private function createScreenshot($identifier, array $coords, $fullScreenshot, array $excludeElements = [])
{
Expand Down Expand Up @@ -498,6 +513,7 @@ private function createScreenshot($identifier, array $coords, $fullScreenshot, a
if (!$timeout) {
throw new \Exception('Error on scroll and make screenshot');
}

if ($pageTop == $height - $viewportHeight) {
break;
}
Expand All @@ -510,7 +526,7 @@ private function createScreenshot($identifier, array $coords, $fullScreenshot, a
$heightOffset = $viewportHeight - ($height - (intval($itr) * $viewportHeight));

if ($isViewPortHeightBiggerThanPageHeight) {
$screenShotImage->cropImage(0, 0, 0, $heightOffset * $devicePixelRatio);
$screenShotImage->cropImage(0, 0, 0, $heightOffset * $devicePixelRatio);
}

$screenShotImage->resetIterator();
Expand Down

0 comments on commit 61b8eee

Please sign in to comment.