You are here

trait ThunderImageCompareTestTrait in Thunder 8.2

Trait for creating and comparing of screenshots.

@package Drupal\Tests\thunder\FunctionalJavascript

Hierarchy

File

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

Namespace

Drupal\Tests\thunder\FunctionalJavascript
View source
trait ThunderImageCompareTestTrait {

  /**
   * Flag to switch between creation of screenshots and comparing of them.
   *
   * @var bool
   */
  protected $generateMode = FALSE;

  /**
   * File system service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * Prefix for temporally created screenshots.
   *
   * @var string
   */
  protected $screenShotPrefix = 'test_ss_';

  /**
   * Set generate mode.
   *
   * @param bool $generateMode
   *   Generate mode.
   */
  public function setGenerateMode($generateMode) {
    $this->generateMode = $generateMode;
  }

  /**
   * Get file system service.
   *
   * @return \Drupal\Core\File\FileSystemInterface
   *   Returns file system service.
   */
  protected function getFileSystem() {
    if (!isset($this->fileSystem)) {
      $this->fileSystem = \Drupal::service('file_system');
    }
    return $this->fileSystem;
  }

  /**
   * Set size of browser window.
   *
   * @param array $windowSize
   *   New size for window. Associative array with width and height keys.
   */
  protected function setWindowSize(array $windowSize) {
    $this
      ->getSession()
      ->getDriver()
      ->resizeWindow($windowSize['width'], $windowSize['height']);
    $this
      ->assertSession()
      ->assertWaitOnAjaxRequest();
  }

  /**
   * Compare screen part to image of previous screenshot.
   *
   * @param string $expectedImageFile
   *   Expected image.
   * @param array $imageSize
   *   Crop information for comparible part of screenshot.
   * @param array $windowSize
   *   Window size, to adjust for browser before screenshot.
   * @param float $threshold
   *   Threshold of allowed error.
   *
   * @return bool
   *   Returns TRUE if difference between images are below allowed threshold.
   *
   * @throws \Exception
   */
  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);
  }

  /**
   * Compare two images with allowed difference threshold.
   *
   * @param string $expectedImageFile
   *   Expected image.
   * @param string $actualImageFile
   *   Actual image.
   * @param float $threshold
   *   Threshold of allowed error.
   *
   * @return bool
   *   Returns TRUE if difference between images are below allowed threshold.
   *
   * @throws \Exception
   */
  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;
  }

  /**
   * Store diff image to screenshot folder for possible debugging of test.
   *
   * @param string $expectedImageFile
   *   Expected image.
   * @param array $compareResult
   *   Result of Imagick::compareImages() execution.
   */
  protected function storeDiffImage($expectedImageFile, array $compareResult) {
    $fileName = $this
      ->getScreenshotFolder() . '/' . basename($expectedImageFile) . '_' . date('Ymd_His') . '.png';
    file_put_contents($fileName, $compareResult[0]);
  }

  /**
   * Get screenshot file path.
   *
   * @param string $screenshotName
   *   Screenshot name.
   *
   * @return string
   *   Return file path to screenshot image file.
   */
  public function getScreenshotFile($screenshotName) {
    return dirname(__FILE__) . sprintf('/../../fixtures/screenshots/%s.png', $screenshotName);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ThunderImageCompareTestTrait::$fileSystem protected property File system service.
ThunderImageCompareTestTrait::$generateMode protected property Flag to switch between creation of screenshots and comparing of them.
ThunderImageCompareTestTrait::$screenShotPrefix protected property Prefix for temporally created screenshots.
ThunderImageCompareTestTrait::compareImages public function Compare two images with allowed difference threshold.
ThunderImageCompareTestTrait::compareScreenToImage public function Compare screen part to image of previous screenshot.
ThunderImageCompareTestTrait::getFileSystem protected function Get file system service.
ThunderImageCompareTestTrait::getScreenshotFile public function Get screenshot file path.
ThunderImageCompareTestTrait::setGenerateMode public function Set generate mode.
ThunderImageCompareTestTrait::setWindowSize protected function Set size of browser window.
ThunderImageCompareTestTrait::storeDiffImage protected function Store diff image to screenshot folder for possible debugging of test.