/**
  * @param StepEvent $stepEvent
  */
 public function stepAfter(StepEvent $stepEvent)
 {
     if ($stepEvent->getStep()->hasFailed() && $stepEvent->getStep()->getAction('seeNoDifferenceToReferenceImage')) {
         /** @var WebDriver $stepWebDriver */
         $stepWebDriver = $stepEvent->getTest()->getScenario()->current('modules')['WebDriver'];
         $identifier = $stepEvent->getStep()->getArguments()[0];
         $windowSize = $this->fileSystemUtil->getCurrentWindowSizeString($stepWebDriver);
         $this->failedIdentifiers[] = array('identifier' => $identifier, 'windowSize' => $windowSize, 'failImage' => base64_encode(file_get_contents($this->fileSystemUtil->getFailImagePath($identifier, $windowSize, 'fail'))), 'diffImage' => base64_encode(file_get_contents($this->fileSystemUtil->getFailImagePath($identifier, $windowSize, 'diff'))), 'referenceImage' => base64_encode(file_get_contents($this->fileSystemUtil->getReferenceImagePath($identifier, $windowSize))));
     }
 }
 /**
  * Checks item in Memcached exists and the same as expected.
  *
  * @param string $referenceImageIdentifier
  * @param null|string $selector
  * @throws ModuleException
  */
 public function seeNoDifferenceToReferenceImage($referenceImageIdentifier, $selector = null)
 {
     if ($selector === null) {
         $selector = 'body';
     }
     $elements = $this->webDriver->_findElements($selector);
     if (count($elements) == 0) {
         throw new ElementNotFound($selector);
     } elseif (count($elements) > 1) {
         throw new ModuleException(__CLASS__, 'Multiple elements found for given selector "' . $selector . '" but need exactly one element!');
     }
     /** @var RemoteWebElement $element */
     $image = $this->_createScreenshot($referenceImageIdentifier, reset($elements));
     $windowSizeString = $this->moduleFileSystemUtil->getCurrentWindowSizeString($this->webDriver);
     $referenceImagePath = $this->moduleFileSystemUtil->getReferenceImagePath($referenceImageIdentifier, $windowSizeString);
     if (!file_exists($referenceImagePath)) {
         // Ensure that the target directory exists
         $this->moduleFileSystemUtil->createDirectoryRecursive(dirname($referenceImagePath));
         copy($image->getImageFilename(), $referenceImagePath);
         $this->currentTestCase->markTestIncomplete('Reference Image does not exist.
             Test is skipeed but will now copy reference image to target directory...');
     } else {
         $referenceImage = new \Imagick($referenceImagePath);
         /** @var \Imagick $comparedImage */
         list($comparedImage, $difference) = $referenceImage->compareImages($image, \Imagick::METRIC_MEANSQUAREERROR);
         $calculatedDifferenceValue = round((double) substr($difference, 0, 6) * 100, 2);
         $this->currentTestCase->getScenario()->comment('Difference between reference and current image is around ' . $calculatedDifferenceValue . '%');
         if ($calculatedDifferenceValue > $this->config['maxDifference']) {
             $failImagePath = $this->moduleFileSystemUtil->getFailImagePath($referenceImageIdentifier, $windowSizeString, 'diff');
             $this->moduleFileSystemUtil->createDirectoryRecursive(dirname($failImagePath));
             $image->writeImage($this->moduleFileSystemUtil->getFailImagePath($referenceImageIdentifier, $windowSizeString, 'fail'));
             $comparedImage->setImageFormat('png');
             $comparedImage->writeImage($failImagePath);
             $this->fail('Image does not match to the reference image.');
         }
     }
 }