You are here

public function ThunderImageCompareTestTrait::compareImages in Thunder 8.2

Compare two images with allowed difference threshold.

Parameters

string $expectedImageFile: Expected image.

string $actualImageFile: Actual image.

float $threshold: Threshold of allowed error.

Return value

bool Returns TRUE if difference between images are below allowed threshold.

Throws

\Exception

1 call to ThunderImageCompareTestTrait::compareImages()
ThunderImageCompareTestTrait::compareScreenToImage in tests/src/FunctionalJavascript/ThunderImageCompareTestTrait.php
Compare screen part to image of previous screenshot.

File

tests/src/FunctionalJavascript/ThunderImageCompareTestTrait.php, line 138

Class

ThunderImageCompareTestTrait
Trait for creating and comparing of screenshots.

Namespace

Drupal\Tests\thunder\FunctionalJavascript

Code

public function compareImages($expectedImageFile, $actualImageFile, $threshold = 0.01) {

  // Store created screenshot file for next test execution.
  if ($this->generateMode) {
    $newFileName = file_unmanaged_move($actualImageFile, $expectedImageFile, FILE_EXISTS_REPLACE);
    if (!$newFileName) {
      throw new \Exception(sprintf('Unable to create file in %s.', $expectedImageFile));
    }
    return TRUE;
  }
  $expectedImage = new Imagick(realpath($expectedImageFile));
  $actualImage = new Imagick(realpath($actualImageFile));
  $result = $actualImage
    ->compareImages($expectedImage, Imagick::METRIC_MEANSQUAREERROR);
  $differentImages = $result[1] < $threshold;
  if (!$differentImages) {
    $this
      ->storeDiffImage($expectedImageFile, $result);
  }
  return $differentImages;
}