You are here

public function TestSubContext::takeScreenshot in Panopoly 8.2

Same name and namespace in other branches
  1. 7 modules/panopoly/panopoly_test/behat/steps/panopoly_test.behat.inc \TestSubContext::takeScreenshot()

Explicitly take a screenshot.

@Given I take a screenshot @Given I take a screenshot with the title :title

1 call to TestSubContext::takeScreenshot()
TestSubContext::afterStepTakeScreenshot in modules/panopoly/panopoly_test/behat/steps/panopoly_test.behat.inc
After a failed step, upload a screenshot.

File

modules/panopoly/panopoly_test/behat/steps/panopoly_test.behat.inc, line 327
Provide Behat step-definitions for generic Panopoly tests.

Class

TestSubContext
Behat sub-context for Panopoly.

Code

public function takeScreenshot($title = 'screenshot') {
  static $screenshot_count = 0;
  $driver = $this
    ->getSession()
    ->getDriver();

  // Get the screenshot if the driver supports it.
  try {
    $image = $driver
      ->getScreenshot();
  } catch (UnsupportedDriverActionException $e) {
    return;
  }

  // Set default title.
  $title = sprintf('%s_%s_%s', date("Ymd-Hi"), preg_replace('/[^a-zA-Z0-9\\._-]/', '_', $title), ++$screenshot_count);

  // Save the file locally, if a path is available. Variable can be set in
  // .travis.yml or in local working environment.
  $local_screenshot_path = getenv('PANOPOLY_BEHAT_SCREENSHOT_PATH');
  if (empty($local_screenshot_path)) {
    print "Environment variable PANOPOLY_BEHAT_SCREENSHOT_PATH is not set, unable to save screenshot\n";
  }
  elseif (!is_dir($local_screenshot_path)) {
    print "Directory {$local_screenshot_path} does not exist, unable to save screenshot\n";
  }
  else {
    $file_location = "{$local_screenshot_path}/{$title}.png";
    if (@file_put_contents($file_location, $image) !== FALSE) {
      print "Screenshot saved to {$file_location}\n";
    }
    else {
      print "Unable to save screenshot\n";
    }
  }

  // Upload the image to Imgur if a client ID is available.
  $imgur_client_id = getenv('IMGUR_CLIENT_ID');
  if ($imgur_client_id) {
    $url = $this
      ->uploadScreenshot($image, $title, $imgur_client_id);
    print "Screenshot uploaded to {$url}\n";
  }
  else {
    print "Environment variable IMGUR_CLIENT_ID not set, unable to upload screenshot\n";
  }
}