You are here

class SocialMinkContext in Open Social 8.9

Same name and namespace in other branches
  1. 8.3 tests/behat/features/bootstrap/SocialMinkContext.php \Drupal\social\Behat\SocialMinkContext
  2. 8.4 tests/behat/features/bootstrap/SocialMinkContext.php \Drupal\social\Behat\SocialMinkContext
  3. 8.5 tests/behat/features/bootstrap/SocialMinkContext.php \Drupal\social\Behat\SocialMinkContext
  4. 8.6 tests/behat/features/bootstrap/SocialMinkContext.php \Drupal\social\Behat\SocialMinkContext
  5. 8.7 tests/behat/features/bootstrap/SocialMinkContext.php \Drupal\social\Behat\SocialMinkContext
  6. 8.8 tests/behat/features/bootstrap/SocialMinkContext.php \Drupal\social\Behat\SocialMinkContext
  7. 10.3.x tests/behat/features/bootstrap/SocialMinkContext.php \Drupal\social\Behat\SocialMinkContext
  8. 10.0.x tests/behat/features/bootstrap/SocialMinkContext.php \Drupal\social\Behat\SocialMinkContext
  9. 10.1.x tests/behat/features/bootstrap/SocialMinkContext.php \Drupal\social\Behat\SocialMinkContext
  10. 10.2.x tests/behat/features/bootstrap/SocialMinkContext.php \Drupal\social\Behat\SocialMinkContext

Defines application features from the specific context.

Hierarchy

  • class \Drupal\social\Behat\SocialMinkContext extends \Drupal\DrupalExtension\Context\MinkContext

Expanded class hierarchy of SocialMinkContext

1 string reference to 'SocialMinkContext'
behat.yml in tests/behat/config/behat.yml
tests/behat/config/behat.yml

File

tests/behat/features/bootstrap/SocialMinkContext.php, line 21

Namespace

Drupal\social\Behat
View source
class SocialMinkContext extends MinkContext {

  /**
   * @override MinkContext::assertRegionHeading()
   *
   * Makes the step case insensitive.
   */
  public function assertRegionHeading($heading, $region) {
    $regionObj = $this
      ->getRegion($region);
    foreach (array(
      'h1',
      'h2',
      'h3',
      'h4',
      'h5',
      'h6',
    ) as $tag) {
      $elements = $regionObj
        ->findAll('css', $tag);
      if (!empty($elements)) {
        foreach ($elements as $element) {
          if (trim(strtolower($element
            ->getText())) === strtolower($heading)) {
            return;
          }
        }
      }
    }
    throw new \Exception(sprintf('The heading "%s" was not found in the "%s" region on the page %s', $heading, $region, $this
      ->getSession()
      ->getCurrentUrl()));
  }

  /**
   * @override MinkContext::assertCheckBox()
   */
  public function assertCheckBox($checkbox) {
    $this
      ->getSession()
      ->executeScript("\n      var inputs = document.getElementsByTagName('input');\n      for (var i = 0; i < inputs.length; i++) {\n        inputs[i].style.opacity = 1;\n        inputs[i].style.left = 0;\n        inputs[i].style.position = 'relative';\n      }\n    ");
    parent::assertCheckBox($checkbox);
  }

  /**
   * @Given /^I make a screenshot$/
   */
  public function iMakeAScreenshot() {
    $this
      ->iMakeAScreenshotWithFileName('screenshot');
  }

  /**
   * @Given /^I make a screenshot with the name "([^"]*)"$/
   */
  public function iMakeAScreenshotWithFileName($filename) {
    $screenshot = $this
      ->getSession()
      ->getDriver()
      ->getScreenshot();
    $dir = '/var/www/travis_artifacts';
    if (is_writeable($dir)) {
      $file_and_path = $dir . '/' . $filename . '.jpg';
      file_put_contents($file_and_path, $screenshot);
    }
  }

  /**
   * @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/
   */
  public function iFillInSelectInputWithAndSelect($field, $value, $entry) {
    $page = $this
      ->getSession()
      ->getPage();
    $inputField = $page
      ->find('css', $field);
    if (!$inputField) {
      throw new \Exception('No field found');
    }
    $this
      ->getSession()
      ->wait(1000);
    $choice = $inputField
      ->getParent()
      ->find('css', '.select2-selection');
    if (!$choice) {
      throw new \Exception('No select2 choice found');
    }
    $choice
      ->press();
    $select2Input = $page
      ->find('css', '.select2-search__field');
    if (!$select2Input) {
      throw new \Exception('No input found');
    }
    $select2Input
      ->setValue($value);
    $this
      ->getSession()
      ->wait(1000);
    $chosenResults = $page
      ->findAll('css', '.select2-results li');
    foreach ($chosenResults as $result) {
      if ($result
        ->getText() == $entry) {
        $result
          ->click();
        break;
      }
    }
  }

  /**
   * @AfterStep
   */
  public function takeScreenShotAfterFailedStep(AfterStepScope $scope) {
    if (99 === $scope
      ->getTestResult()
      ->getResultCode()) {
      $driver = $this
        ->getSession()
        ->getDriver();
      if (!$driver instanceof Selenium2Driver) {
        return;
      }
      $feature = $scope
        ->getFeature();
      $title = $feature
        ->getTitle();
      $filename = date("Ymd-H_i_s");
      if (!empty($title)) {
        $filename .= '-' . str_replace(' ', '-', strtolower($title));
      }
      $filename .= '-error';
      $this
        ->iMakeAScreenshotWithFileName($filename);
    }
  }

  /**
   * Attaches file to field with specified name.
   *
   * @When /^(?:|I )attach the file "(?P<path>[^"]*)" to hidden field "(?P<field>(?:[^"]|\\")*)"$/
   */
  public function attachFileToHiddenField($field, $path) {
    $field = $this
      ->fixStepArgument($field);
    $javascript = "jQuery('#" . $field . "').parent().removeClass('hidden')";
    $this
      ->getSession()
      ->executeScript($javascript);
    $this
      ->attachFileToField($field, $path);
  }

  /**
   * @Then I should see checked the box :checkbox
   */
  public function iShouldSeeCheckedTheBox($checkbox) {
    $checkbox = $this
      ->fixStepArgument($checkbox);
    if (!$this
      ->getSession()
      ->getPage()
      ->hasCheckedField($checkbox)) {
      $field = $this
        ->getSession()
        ->getPage()
        ->findField($checkbox);
      if (null === $field) {
        throw new \Exception(sprintf('The checkbox "%s" with id|name|label|value was not found', $checkbox));
      }
      else {
        throw new \Exception(sprintf('The checkbox "%s" is not checked', $checkbox));
      }
    }
  }

  /**
   * @Then I should see unchecked the box :checkbox
   */
  public function iShouldSeeUncheckedTheBox($checkbox) {
    $checkbox = $this
      ->fixStepArgument($checkbox);
    if (!$this
      ->getSession()
      ->getPage()
      ->hasUncheckedField($checkbox)) {
      $field = $this
        ->getSession()
        ->getPage()
        ->findField($checkbox);
      if (null === $field) {
        throw new \Exception(sprintf('The checkbox "%s" with id|name|label|value was not found', $checkbox));
      }
      else {
        throw new \Exception(sprintf('The checkbox "%s" is checked', $checkbox));
      }
    }
  }

  /**
   * Wait for AJAX to finish.
   *
   * Overwrites the default iWaitForAjaxToFinish step to increase the time-out to
   * allow tests to pass with longer running ajax requests.
   *
   * @see \Drupal\FunctionalJavascriptTests\JSWebAssert::assertWaitOnAjaxRequest()
   * @see \Drupal\DrupalExtension\Context\MinkContext::iWaitForAjaxToFinish()
   *
   * This overrides "Given I wait for AJAX to finish"
   */
  public function iWaitForAjaxToFinish() {
    $condition = <<<JS
    (function() {
      function isAjaxing(instance) {
        return instance && instance.ajaxing === true;
      }
      var d7_not_ajaxing = true;
      if (typeof Drupal !== 'undefined' && typeof Drupal.ajax !== 'undefined' && typeof Drupal.ajax.instances === 'undefined') {
        for(var i in Drupal.ajax) { if (isAjaxing(Drupal.ajax[i])) { d7_not_ajaxing = false; } }
      }
      var d8_not_ajaxing = (typeof Drupal === 'undefined' || typeof Drupal.ajax === 'undefined' || typeof Drupal.ajax.instances === 'undefined' || !Drupal.ajax.instances.some(isAjaxing))
      return (
        // Assert no AJAX request is running (via jQuery or Drupal) and no
        // animation is running.
        (typeof jQuery === 'undefined' || (jQuery.active === 0 && jQuery(':animated').length === 0)) &&
        d7_not_ajaxing && d8_not_ajaxing
      );
    }());
JS;
    $result = $this
      ->getSession()
      ->wait(20000, $condition);
    if (!$result) {
      throw new \RuntimeException('Unable to complete AJAX request.');
    }
  }

  /**
   * Set alias field as specified value
   * Example: When I set alias as: "bwayne"
   *
   * @When /^(?:|I )set alias as "(?P<value>(?:[^"]|\\")*)"$/
   */
  public function iSetAlias($value) {

    // Uncheck "Generate automatic URL alias" if social_path_manager is enabled.
    if (\Drupal::service('module_handler')
      ->moduleExists('social_path_manager')) {
      $option = $this
        ->fixStepArgument('Generate automatic URL alias');
      $this
        ->getSession()
        ->getPage()
        ->uncheckField($option);
    }

    // Fill in "URL alias" field with given value
    $field = $this
      ->fixStepArgument('URL alias');
    $value = $this
      ->fixStepArgument($value);
    $this
      ->getSession()
      ->getPage()
      ->fillField($field, $value);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SocialMinkContext::assertCheckBox public function @override MinkContext::assertCheckBox()
SocialMinkContext::assertRegionHeading public function @override MinkContext::assertRegionHeading()
SocialMinkContext::attachFileToHiddenField public function Attaches file to field with specified name.
SocialMinkContext::iFillInSelectInputWithAndSelect public function @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/
SocialMinkContext::iMakeAScreenshot public function @Given /^I make a screenshot$/
SocialMinkContext::iMakeAScreenshotWithFileName public function @Given /^I make a screenshot with the name "([^"]*)"$/
SocialMinkContext::iSetAlias public function Set alias field as specified value Example: When I set alias as: "bwayne"
SocialMinkContext::iShouldSeeCheckedTheBox public function @Then I should see checked the box :checkbox
SocialMinkContext::iShouldSeeUncheckedTheBox public function @Then I should see unchecked the box :checkbox
SocialMinkContext::iWaitForAjaxToFinish public function Wait for AJAX to finish.
SocialMinkContext::takeScreenShotAfterFailedStep public function @AfterStep