annotate vendor/jcalderonzumba/mink-phantomjs-driver/src/KeyboardTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Zumba\Mink\Driver;
Chris@0 4
Chris@0 5 use Behat\Mink\Exception\DriverException;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Class KeyboardTrait
Chris@0 9 * @package Zumba\Mink\Driver
Chris@0 10 */
Chris@0 11 trait KeyboardTrait {
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Does some normalization for the char we want to do keyboard events with.
Chris@0 15 * @param $char
Chris@0 16 * @throws DriverException
Chris@0 17 * @return string
Chris@0 18 */
Chris@0 19 protected function normalizeCharForKeyEvent($char) {
Chris@0 20 if (!is_int($char) && !is_string($char)) {
Chris@0 21 throw new DriverException("Unsupported key type, can only be integer or string");
Chris@0 22 }
Chris@0 23
Chris@0 24 if (is_string($char) && strlen($char) !== 1) {
Chris@0 25 throw new DriverException("Key can only have ONE character");
Chris@0 26 }
Chris@0 27
Chris@0 28 $key = $char;
Chris@0 29 if (is_int($char)) {
Chris@0 30 $key = chr($char);
Chris@0 31 }
Chris@0 32 return $key;
Chris@0 33 }
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Does some control and normalization for the key event modifier
Chris@0 37 * @param $modifier
Chris@0 38 * @return string
Chris@0 39 * @throws DriverException
Chris@0 40 */
Chris@0 41 protected function keyEventModifierControl($modifier) {
Chris@0 42 if ($modifier === null) {
Chris@0 43 $modifier = "none";
Chris@0 44 }
Chris@0 45
Chris@0 46 if (!in_array($modifier, array("none", "alt", "ctrl", "shift", "meta"))) {
Chris@0 47 throw new DriverException("Unsupported key modifier $modifier");
Chris@0 48 }
Chris@0 49 return $modifier;
Chris@0 50 }
Chris@0 51
Chris@0 52 /**
Chris@0 53 * Send a key-down event to the browser element
Chris@0 54 * @param $xpath
Chris@0 55 * @param $char
Chris@0 56 * @param string $modifier
Chris@0 57 * @throws DriverException
Chris@0 58 */
Chris@0 59 public function keyDown($xpath, $char, $modifier = null) {
Chris@0 60 $element = $this->findElement($xpath, 1);
Chris@0 61 $key = $this->normalizeCharForKeyEvent($char);
Chris@0 62 $modifier = $this->keyEventModifierControl($modifier);
Chris@0 63 return $this->browser->keyEvent($element["page_id"], $element["ids"][0], "keydown", $key, $modifier);
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * @param string $xpath
Chris@0 68 * @param string $char
Chris@0 69 * @param string $modifier
Chris@0 70 * @throws DriverException
Chris@0 71 */
Chris@0 72 public function keyPress($xpath, $char, $modifier = null) {
Chris@0 73 $element = $this->findElement($xpath, 1);
Chris@0 74 $key = $this->normalizeCharForKeyEvent($char);
Chris@0 75 $modifier = $this->keyEventModifierControl($modifier);
Chris@0 76 return $this->browser->keyEvent($element["page_id"], $element["ids"][0], "keypress", $key, $modifier);
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * Pressed up specific keyboard key.
Chris@0 81 *
Chris@0 82 * @param string $xpath
Chris@0 83 * @param string|integer $char could be either char ('b') or char-code (98)
Chris@0 84 * @param string $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
Chris@0 85 *
Chris@0 86 * @throws DriverException When the operation cannot be done
Chris@0 87 */
Chris@0 88 public function keyUp($xpath, $char, $modifier = null) {
Chris@0 89 $this->findElement($xpath, 1);
Chris@0 90 $element = $this->findElement($xpath, 1);
Chris@0 91 $key = $this->normalizeCharForKeyEvent($char);
Chris@0 92 $modifier = $this->keyEventModifierControl($modifier);
Chris@0 93 return $this->browser->keyEvent($element["page_id"], $element["ids"][0], "keyup", $key, $modifier);
Chris@0 94 }
Chris@0 95 }