You are here

trait KeyboardTrait in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/jcalderonzumba/mink-phantomjs-driver/src/KeyboardTrait.php \Zumba\Mink\Driver\KeyboardTrait

Class KeyboardTrait @package Zumba\Mink\Driver

Hierarchy

File

vendor/jcalderonzumba/mink-phantomjs-driver/src/KeyboardTrait.php, line 11

Namespace

Zumba\Mink\Driver
View source
trait KeyboardTrait {

  /**
   * Does some normalization for the char we want to do keyboard events with.
   * @param $char
   * @throws DriverException
   * @return string
   */
  protected function normalizeCharForKeyEvent($char) {
    if (!is_int($char) && !is_string($char)) {
      throw new DriverException("Unsupported key type, can only be integer or string");
    }
    if (is_string($char) && strlen($char) !== 1) {
      throw new DriverException("Key can only have ONE character");
    }
    $key = $char;
    if (is_int($char)) {
      $key = chr($char);
    }
    return $key;
  }

  /**
   * Does some control and normalization for the key event modifier
   * @param $modifier
   * @return string
   * @throws DriverException
   */
  protected function keyEventModifierControl($modifier) {
    if ($modifier === null) {
      $modifier = "none";
    }
    if (!in_array($modifier, array(
      "none",
      "alt",
      "ctrl",
      "shift",
      "meta",
    ))) {
      throw new DriverException("Unsupported key modifier {$modifier}");
    }
    return $modifier;
  }

  /**
   * Send a key-down event to the browser element
   * @param        $xpath
   * @param        $char
   * @param string $modifier
   * @throws DriverException
   */
  public function keyDown($xpath, $char, $modifier = null) {
    $element = $this
      ->findElement($xpath, 1);
    $key = $this
      ->normalizeCharForKeyEvent($char);
    $modifier = $this
      ->keyEventModifierControl($modifier);
    return $this->browser
      ->keyEvent($element["page_id"], $element["ids"][0], "keydown", $key, $modifier);
  }

  /**
   * @param string $xpath
   * @param string $char
   * @param string $modifier
   * @throws DriverException
   */
  public function keyPress($xpath, $char, $modifier = null) {
    $element = $this
      ->findElement($xpath, 1);
    $key = $this
      ->normalizeCharForKeyEvent($char);
    $modifier = $this
      ->keyEventModifierControl($modifier);
    return $this->browser
      ->keyEvent($element["page_id"], $element["ids"][0], "keypress", $key, $modifier);
  }

  /**
   * Pressed up specific keyboard key.
   *
   * @param string         $xpath
   * @param string|integer $char could be either char ('b') or char-code (98)
   * @param string         $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
   *
   * @throws DriverException                  When the operation cannot be done
   */
  public function keyUp($xpath, $char, $modifier = null) {
    $this
      ->findElement($xpath, 1);
    $element = $this
      ->findElement($xpath, 1);
    $key = $this
      ->normalizeCharForKeyEvent($char);
    $modifier = $this
      ->keyEventModifierControl($modifier);
    return $this->browser
      ->keyEvent($element["page_id"], $element["ids"][0], "keyup", $key, $modifier);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
KeyboardTrait::keyDown public function Send a key-down event to the browser element
KeyboardTrait::keyEventModifierControl protected function Does some control and normalization for the key event modifier
KeyboardTrait::keyPress public function
KeyboardTrait::keyUp public function Pressed up specific keyboard key.
KeyboardTrait::normalizeCharForKeyEvent protected function Does some normalization for the char we want to do keyboard events with.