3 namespace Zumba\Mink\Driver;
5 use Behat\Mink\Exception\DriverException;
9 * @package Zumba\Mink\Driver
14 * Does some normalization for the char we want to do keyboard events with.
16 * @throws DriverException
19 protected function normalizeCharForKeyEvent($char) {
20 if (!is_int($char) && !is_string($char)) {
21 throw new DriverException("Unsupported key type, can only be integer or string");
24 if (is_string($char) && strlen($char) !== 1) {
25 throw new DriverException("Key can only have ONE character");
36 * Does some control and normalization for the key event modifier
39 * @throws DriverException
41 protected function keyEventModifierControl($modifier) {
42 if ($modifier === null) {
46 if (!in_array($modifier, array("none", "alt", "ctrl", "shift", "meta"))) {
47 throw new DriverException("Unsupported key modifier $modifier");
53 * Send a key-down event to the browser element
56 * @param string $modifier
57 * @throws DriverException
59 public function keyDown($xpath, $char, $modifier = null) {
60 $element = $this->findElement($xpath, 1);
61 $key = $this->normalizeCharForKeyEvent($char);
62 $modifier = $this->keyEventModifierControl($modifier);
63 return $this->browser->keyEvent($element["page_id"], $element["ids"][0], "keydown", $key, $modifier);
67 * @param string $xpath
69 * @param string $modifier
70 * @throws DriverException
72 public function keyPress($xpath, $char, $modifier = null) {
73 $element = $this->findElement($xpath, 1);
74 $key = $this->normalizeCharForKeyEvent($char);
75 $modifier = $this->keyEventModifierControl($modifier);
76 return $this->browser->keyEvent($element["page_id"], $element["ids"][0], "keypress", $key, $modifier);
80 * Pressed up specific keyboard key.
82 * @param string $xpath
83 * @param string|integer $char could be either char ('b') or char-code (98)
84 * @param string $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
86 * @throws DriverException When the operation cannot be done
88 public function keyUp($xpath, $char, $modifier = null) {
89 $this->findElement($xpath, 1);
90 $element = $this->findElement($xpath, 1);
91 $key = $this->normalizeCharForKeyEvent($char);
92 $modifier = $this->keyEventModifierControl($modifier);
93 return $this->browser->keyEvent($element["page_id"], $element["ids"][0], "keyup", $key, $modifier);