Advertisement
claudiucristea

Untitled

Nov 3rd, 2016
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.72 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\joinup\Traits;
  4.  
  5. use Behat\Gherkin\Node\TableNode;
  6. use Behat\Mink\Element\NodeElement;
  7. use Behat\Mink\Element\DocumentElement;
  8. use Behat\Mink\Exception\ExpectationException;
  9. use Behat\Behat\Context\Environment\InitializedContextEnvironment;
  10. use Behat\Behat\Hook\Scope\AfterStepScope;
  11. use Behat\Mink\Exception\UnsupportedDriverActionException;
  12. use Behat\Mink\Exception\DriverException;
  13.  
  14. /**
  15.  * Trait ScreenShot.
  16.  */
  17. trait ScreenShotTrait {
  18.  
  19.   /**
  20.    * @Then (I )take a screenshot :name
  21.    */
  22.   public function takeAScreenshot($i = NULL, $name = NULL) {
  23.     $file_name = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $name;
  24.     $message = "Screenshot created in @file_name";
  25.     $this->createScreenshot($file_name, $message, FALSE);
  26.   }
  27.   /**
  28.    * @Then (I )take a screenshot
  29.    */
  30.   public function takeAScreenshotUnnamed() {
  31.     $file_name = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'behat-screenshot';
  32.     $message = "Screenshot created in @file_name";
  33.     $this->createScreenshot($file_name, $message);
  34.   }
  35.  
  36.   /**
  37.    * @AfterStep
  38.    *
  39.    * Make sure there is no PHP notice on the screen during tests.
  40.    *
  41.    * @param $event
  42.    */
  43.   public function screenshotForPhpNotices(AfterStepScope $event) {
  44.     $environment = $event->getEnvironment();
  45.     // Make sure the environment has the MessageContext.
  46.     $class = 'Drupal\DrupalExtension\Context\MessageContext';
  47.     if ($environment instanceof InitializedContextEnvironment && $environment->hasContextClass($class)) {
  48.       /** @var \Drupal\DrupalExtension\Context\MessageContext $context */
  49.       $context = $environment->getContext($class);
  50.       // Only check if the session is started.
  51.       try {
  52.         if ($context->getMink()->isSessionStarted()) {
  53.           try {
  54.             $context->assertNotWarningMessage('Notice:');
  55.           }
  56.           catch (\Exception $e) {
  57.             // Use the step test in the filename.
  58.             $step = $event->getStep();
  59.             if (function_exists('transliteration_clean_filename')) {
  60.               $file_name = transliteration_clean_filename($step->getKeyword() . '_' . $step->getText());
  61.             }
  62.             else {
  63.               $file_name = str_replace(' ', '_', $step->getKeyword() . '_' . $step->getText());
  64.               $file_name = preg_replace('![^0-9A-Za-z_.-]!', '', $file_name);
  65.             }
  66.             $file_name = substr($file_name, 0, 30);
  67.             $file_name = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'behat-notice__' . $file_name;
  68.             $message = "Screenshot for behat notice in step created in @file_name";
  69.             $this->createScreenshot($file_name, $message);
  70.             // We don't throw $e any more because we don't fail on the notice.
  71.           }
  72.         }
  73.       } catch (DriverException $driver_exception) { }
  74.     }
  75.   }
  76.  
  77.   /**
  78.    * @AfterStep
  79.    *
  80.    * Take a screen shot after failed steps for Selenium drivers or save the html
  81.    * for non js drivers.
  82.    */
  83.   public function takeScreenshotAfterFailedStep(AfterStepScope $event) {
  84.     if ($event->getTestResult()->isPassed()) {
  85.       // Not a failed step.
  86.       return;
  87.     }
  88.     $step = $event->getStep();
  89.     if (function_exists('transliteration_clean_filename')) {
  90.       $file_name = transliteration_clean_filename($step->getKeyword() . '_' . $step->getText());
  91.     }
  92.     else {
  93.       $file_name = str_replace(' ', '_', $step->getKeyword() . '_' . $step->getText());
  94.       $file_name = preg_replace('![^0-9A-Za-z_.-]!', '', $file_name);
  95.     }
  96.     $file_name = substr($file_name, 0, 30);
  97.     $file_name = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'behat-failed__' . $file_name;
  98.     $message = "Screenshot for failed step created in @file_name";
  99.     $this->createScreenshot($file_name, $message);
  100.   }
  101.  
  102.   /**
  103.    * Create a screenshot or save the html
  104.    *
  105.    * @param string $file_name
  106.    *   The filename of the screenshot (complete).
  107.    * @param string $message
  108.    *   The message to be printed. @file_name will be replaced with $file name.
  109.    * @param bool|TRUE $ext
  110.    *   Whether to add .png or .html to the name of the screenshot.
  111.    */
  112.   public function createScreenshot($file_name, $message, $ext = TRUE) {
  113.     if ($this->getSession()->getDriver() instanceof \Behat\Mink\Driver\Selenium2Driver) {
  114.       if ($ext) {
  115.         $file_name .= '.png';
  116.       }
  117.       $screenshot = $this->getSession()->getDriver()->getScreenshot();
  118.       file_put_contents($file_name, $screenshot);
  119.     }
  120.     else {
  121.       if ($ext) {
  122.         $file_name .= '.html';
  123.       }
  124.       $html_data = $this->getSession()->getDriver()->getContent();
  125.       file_put_contents($file_name, $html_data);
  126.     }
  127.     if ($message) {
  128.       print strtr($message, ['@file_name' => $file_name]);
  129.     }
  130.   }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement