1 | <?php |
||
11 | trait FormManipulationTrait { |
||
12 | |||
13 | |||
14 | /** |
||
15 | * Returns the value of a given xpath element |
||
16 | * @param string $xpath |
||
17 | * @return string |
||
18 | * @throws DriverException |
||
19 | */ |
||
20 | public function getValue($xpath) { |
||
25 | |||
26 | /** |
||
27 | * @param string $xpath |
||
28 | * @param string $value |
||
29 | * @throws DriverException |
||
30 | */ |
||
31 | public function setValue($xpath, $value) { |
||
32 | $this->findElement($xpath, 1); |
||
33 | //This stuff is BECAUSE the way the driver works for setting values when being checkboxes, radios, etc. |
||
34 | if (is_bool($value)) { |
||
35 | $value = $this->boolToString($value); |
||
36 | } elseif (is_numeric($value)) { |
||
37 | $value = (string) $value; |
||
38 | } |
||
39 | |||
40 | $javascript = $this->javascriptTemplateRender("set_value.js.twig", array("xpath" => $xpath, "value" => json_encode($value))); |
||
41 | $this->browser->evaluate($javascript); |
||
42 | } |
||
43 | |||
44 | |||
45 | /** |
||
46 | * Submits a form given an xpath selector |
||
47 | * @param string $xpath |
||
48 | * @throws DriverException |
||
49 | */ |
||
50 | public function submitForm($xpath) { |
||
58 | |||
59 | /** |
||
60 | * Helper method needed for twig and javascript stuff |
||
61 | * @param $boolValue |
||
62 | * @return string |
||
63 | */ |
||
64 | protected function boolToString($boolValue) { |
||
70 | |||
71 | /** |
||
72 | * Selects an option |
||
73 | * @param string $xpath |
||
74 | * @param string $value |
||
75 | * @param bool $multiple |
||
76 | * @return bool |
||
77 | * @throws DriverException |
||
78 | */ |
||
79 | public function selectOption($xpath, $value, $multiple = false) { |
||
94 | |||
95 | /** |
||
96 | * Check control over an input element of radio or checkbox type |
||
97 | * @param $xpath |
||
98 | * @return bool |
||
99 | * @throws DriverException |
||
100 | */ |
||
101 | protected function inputCheckableControl($xpath) { |
||
113 | |||
114 | /** |
||
115 | * We click on the checkbox or radio when possible and needed |
||
116 | * @param string $xpath |
||
117 | * @throws DriverException |
||
118 | */ |
||
119 | public function check($xpath) { |
||
124 | |||
125 | /** |
||
126 | * We click on the checkbox or radio when possible and needed |
||
127 | * @param string $xpath |
||
128 | * @throws DriverException |
||
129 | */ |
||
130 | public function uncheck($xpath) { |
||
135 | |||
136 | /** |
||
137 | * Checks if the radio or checkbox is checked |
||
138 | * @param string $xpath |
||
139 | * @return bool |
||
140 | * @throws DriverException |
||
141 | */ |
||
142 | public function isChecked($xpath) { |
||
153 | |||
154 | /** |
||
155 | * Checks if the option is selected or not |
||
156 | * @param string $xpath |
||
157 | * @return bool |
||
158 | * @throws DriverException |
||
159 | */ |
||
160 | public function isSelected($xpath) { |
||
170 | } |
||
171 |
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
Idable
provides a methodequalsId
that 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.