You are here

class FailureContext in Commerce Kickstart 7.2

Defines application features from the specific context.

Hierarchy

Expanded class hierarchy of FailureContext

1 string reference to 'FailureContext'
behat.common.yml in tests/behat/behat.common.yml
tests/behat/behat.common.yml

File

tests/behat/features/bootstrap/FailureContext.php, line 11

View source
class FailureContext implements Context {
  protected $contexts = array();
  protected $failurePath;

  /**
   * @BeforeScenario
   */
  public function prepare(BeforeScenarioScope $scope) {
    $this->contexts['mink'] = $scope
      ->getEnvironment()
      ->getContext('Drupal\\DrupalExtension\\Context\\MinkContext');
    $this->failurePath = $scope
      ->getEnvironment()
      ->getSuite()
      ->getSetting('failure_path');
  }

  /**
   * @AfterStep
   */
  public function handleFailure(AfterStepScope $scope) {
    if (99 !== $scope
      ->getTestResult()
      ->getResultCode()) {
      return;
    }
    $fileName = $this
      ->fileName($scope);
    $this
      ->dumpMarkup($fileName);
    $this
      ->screenShot($fileName);
  }

  /**
   * @Then /^I take an awesome screenshot$/
   */
  public function takeScreenShot() {
    $fileName = $this
      ->fileName();
    $this
      ->dumpMarkup($fileName);
    $this
      ->screenShot($fileName);
  }

  /**
   * @Then /^I take a markup dump$/
   */
  public function takeMarkupDump() {
    $this
      ->dumpMarkup($this
      ->fileName());
  }

  /**
   * Compute a file name for the output.
   */
  protected function fileName($scope = NULL) {
    if ($scope) {
      $baseName = pathinfo($scope
        ->getFeature()
        ->getFile());
      $baseName = substr($baseName['basename'], 0, strlen($baseName['basename']) - strlen($baseName['extension']) - 1);
      $baseName .= '-' . $scope
        ->getStep()
        ->getLine();
    }
    else {
      $baseName = 'failure';
    }
    $baseName .= '-' . date('YmdHis');
    $baseName = $this->failurePath . '/' . $baseName;
    return $baseName;
  }

  /**
   * Save the markup from the failed step.
   */
  protected function dumpMarkup($fileName) {
    $fileName .= '.html';
    $html = $this->contexts['mink']
      ->getSession()
      ->getPage()
      ->getContent();
    file_put_contents($fileName, $html);
    sprintf("HTML available at: %s\n", $fileName);
  }

  /**
   * Save a screen shot from the failed step.
   */
  protected function screenShot($fileName) {
    $fileName .= '.png';
    $driver = $this->contexts['mink']
      ->getSession()
      ->getDriver();
    if ($driver instanceof Selenium2Driver) {
      file_put_contents($fileName, $this->contexts['mink']
        ->getSession()
        ->getDriver()
        ->getScreenshot());
      sprintf("Screen shot available at: %s\n", $fileName);
      return;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FailureContext::$contexts protected property
FailureContext::$failurePath protected property
FailureContext::dumpMarkup protected function Save the markup from the failed step.
FailureContext::fileName protected function Compute a file name for the output.
FailureContext::handleFailure public function @AfterStep
FailureContext::prepare public function @BeforeScenario
FailureContext::screenShot protected function Save a screen shot from the failed step.
FailureContext::takeMarkupDump public function @Then /^I take a markup dump$/
FailureContext::takeScreenShot public function @Then /^I take an awesome screenshot$/