You are here

public function ThunderImageCompareTestTrait::compareScreenToImage in Thunder 8.2

Compare screen part to image of previous screenshot.

Parameters

string $expectedImageFile: Expected image.

array $imageSize: Crop information for comparible part of screenshot.

array $windowSize: Window size, to adjust for browser before screenshot.

float $threshold: Threshold of allowed error.

Return value

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

Throws

\Exception

2 calls to ThunderImageCompareTestTrait::compareScreenToImage()
RiddleTest::testRiddle in tests/src/FunctionalJavascript/Integration/RiddleTest.php
Testing integration of "thunder_riddle" module.
ThunderMediaTest::test8104 in tests/src/FunctionalJavascript/Update/ThunderMediaTest.php
Test that Video and Image entity browser uses 24 images per page.

File

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

Class

ThunderImageCompareTestTrait
Trait for creating and comparing of screenshots.

Namespace

Drupal\Tests\thunder\FunctionalJavascript

Code

public function compareScreenToImage($expectedImageFile, array $imageSize = [], array $windowSize = [], $threshold = 0.01) {
  $tempScreenShotFile = $this
    ->getFileSystem()
    ->tempnam(FileSystem::getOsTemporaryDirectory(), $this->screenShotPrefix);
  if (!$tempScreenShotFile) {
    throw new \Exception('Unable to get temporally file name.');
  }

  // Resizing of window before making screenshot.
  $adjustWindowForScreenshot = !empty($windowSize);
  if ($adjustWindowForScreenshot) {
    $this
      ->setWindowSize($windowSize);
  }

  // Create screenshot and fetch full path to it.
  $this
    ->createScreenshot($tempScreenShotFile);
  $tempScreenShotFile = realpath($tempScreenShotFile);

  // Set windows size back to previous size, before continuing.
  if ($adjustWindowForScreenshot) {
    $this
      ->setWindowSize($this
      ->getWindowSize());
  }

  // Crop screenshot.
  if (!empty($imageSize)) {
    $image = new Imagick($tempScreenShotFile);
    $image
      ->cropImage($imageSize['width'], $imageSize['height'], $imageSize['x'], $imageSize['y']);
    file_put_contents($tempScreenShotFile, $image);
  }
  return $this
    ->compareImages($expectedImageFile, $tempScreenShotFile, $threshold);
}