|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zumba\Mink\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Mink\Exception\DriverException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class KeyboardTrait |
|
9
|
|
|
* @package Zumba\Mink\Driver |
|
10
|
|
|
*/ |
|
11
|
|
|
trait KeyboardTrait { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Does some normalization for the char we want to do keyboard events with. |
|
15
|
|
|
* @param $char |
|
16
|
|
|
* @throws DriverException |
|
17
|
|
|
* @return string |
|
18
|
|
|
*/ |
|
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"); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
if (is_string($char) && strlen($char) !== 1) { |
|
25
|
|
|
throw new DriverException("Key can only have ONE character"); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$key = $char; |
|
29
|
|
|
if (is_int($char)) { |
|
30
|
|
|
$key = chr($char); |
|
31
|
|
|
} |
|
32
|
|
|
return $key; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Does some control and normalization for the key event modifier |
|
37
|
|
|
* @param $modifier |
|
38
|
|
|
* @return string |
|
39
|
|
|
* @throws DriverException |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function keyEventModifierControl($modifier) { |
|
42
|
|
|
if ($modifier === null) { |
|
43
|
|
|
$modifier = "none"; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (!in_array($modifier, array("none", "alt", "ctrl", "shift", "meta"))) { |
|
47
|
|
|
throw new DriverException("Unsupported key modifier $modifier"); |
|
48
|
|
|
} |
|
49
|
|
|
return $modifier; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Send a key-down event to the browser element |
|
54
|
|
|
* @param $xpath |
|
55
|
|
|
* @param $char |
|
56
|
|
|
* @param string $modifier |
|
57
|
|
|
* @throws DriverException |
|
58
|
|
|
*/ |
|
59
|
|
View Code Duplication |
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); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $xpath |
|
68
|
|
|
* @param string $char |
|
69
|
|
|
* @param string $modifier |
|
70
|
|
|
* @throws DriverException |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
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); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Pressed up specific keyboard key. |
|
81
|
|
|
* |
|
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') |
|
85
|
|
|
* |
|
86
|
|
|
* @throws DriverException When the operation cannot be done |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
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); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.