You are here

public function VarbaseContext::iPressModifierAndKeys in Varbase: The Ultimate Drupal CMS Starter Kit (Bootstrap Ready) 8.5

Same name and namespace in other branches
  1. 8.8 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::iPressModifierAndKeys()
  2. 8.4 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::iPressModifierAndKeys()
  3. 8.6 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::iPressModifierAndKeys()
  4. 8.7 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::iPressModifierAndKeys()
  5. 9.0.x tests/features/bootstrap/VarbaseContext.php \VarbaseContext::iPressModifierAndKeys()

#WIP#vardot: To press a modifier and other key Modifier options: { ctrl, shift, alt } Key options:

Example #1: When I press "ctrl" and "v" Example #2: When I press "alt" and "f" Example #3: When I press "ctrl" and "r"

@When I press :modifier and :key in :field field

File

tests/features/bootstrap/VarbaseContext.php, line 1107

Class

VarbaseContext
Defines application features from the specific context.

Code

public function iPressModifierAndKeys($modifier, $key, $field) {
  static $keys = array(
    'backspace' => 8,
    'tab' => 9,
    'enter' => 13,
    'shift' => 16,
    'ctrl' => 17,
    'alt' => 18,
    'pause' => 19,
    'break' => 19,
    'escape' => 27,
    'esc' => 27,
    'end' => 35,
    'home' => 36,
    'left' => 37,
    'up' => 38,
    'right' => 39,
    'down' => 40,
    'insert' => 45,
    'delete' => 46,
    'pageup' => 33,
    'pagedown' => 34,
    'capslock' => 20,
  );
  static $modifiers = array(
    'shift' => 16,
    'ctrl' => 17,
    'alt' => 18,
  );
  $key = is_numeric($key) ? $key : ord($key);
  $isCtrlKeyArg = $modifier == 'ctrl' ? "true" : "false";
  $isAltKeyArg = $modifier == 'alt' ? "true" : "false";
  $isShiftKeyArg = $modifier == 'shift' ? "true" : "false";
  $isMetaKeyArg = $modifier == 'meta' ? "true" : "false";

  // Validate the modifier keys.
  if (is_string($modifier)) {
    if (strlen($modifier) < 1) {
      throw new \Exception('Modifier parameter was empty. one of \'shift\', \'alt\', \'ctrl\'');
    }
    elseif (strlen($modifier) > 1) {

      // one of 'shift', 'alt', 'ctrl'.
      $filteredModifier = strtolower(str_replace(' ', '', $modifier));
      if (isset($modifiers[$filteredModifier])) {
        $modifier = $modifiers[$filteredModifier];
      }
      else {
        throw new \Exception('Modifier parameter must be one of \'shift\', \'alt\', \'ctrl\'');
      }
    }
  }

  // Validate the other key.
  if (is_string($key)) {
    if (strlen($key) < 1) {
      throw new \Exception('Key parameter was empty.');
    }
    elseif (strlen($key) > 1) {

      // Support for all variations, e.g. ESC, Esc, page up, pageup.
      $filteredKey = strtolower(str_replace(' ', '', $key));
      if (isset($keys[$filteredKey])) {
        $key = $keys[$filteredKey];
      }
      else {
        throw new \Exception('Key parameter must a keyboard key');
      }
    }
  }
  $elementField = $this
    ->getSession()
    ->getPage()
    ->findField($field);
  if (!$elementField) {
    throw new \Exception("Field '{$field}' not found");
  }
  $fieldid = $elementField
    ->getAttribute('id');
  $js = <<<JS
var node = document.getElementById("{<span class="php-variable">$fieldid</span>}");
var keyEvent = document.createEvent('KeyboardEvent');
keyEvent.initKeyEvent('keypress',        // typeArg,
                       true,             // canBubbleArg,
                       true,             // cancelableArg,
                       window,             // viewArg,
                       {<span class="php-variable">$isCtrlKeyArg</span>},             // ctrlKeyArg,
                       {<span class="php-variable">$isAltKeyArg</span>},            // altKeyArg,
                       {<span class="php-variable">$isShiftKeyArg</span>},            // shiftKeyArg,
                       false,            // metaKeyArg,
                       {<span class="php-variable">$key</span>},       // keyCodeArg,
                       {<span class="php-variable">$key</span>}      // charCodeArg);
                     );
node.dispatchEvent(keyEvent);
JS;
  $this
    ->getSession()
    ->executeScript($js);
}