|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zumba\Mink\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Mink\Exception\DriverException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait FormManipulationTrait |
|
9
|
|
|
* @package Zumba\Mink\Driver |
|
10
|
|
|
*/ |
|
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) { |
|
21
|
|
|
$this->findElement($xpath, 1); |
|
|
|
|
|
|
22
|
|
|
$javascript = $this->javascriptTemplateRender("get_value.js.twig", array("xpath" => $xpath)); |
|
|
|
|
|
|
23
|
|
|
return $this->browser->evaluate($javascript); |
|
|
|
|
|
|
24
|
|
|
} |
|
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) { |
|
51
|
|
|
$element = $this->findElement($xpath, 1); |
|
|
|
|
|
|
52
|
|
|
$tagName = $this->browser->tagName($element["page_id"], $element["ids"][0]); |
|
53
|
|
|
if (strcmp(strtolower($tagName), "form") !== 0) { |
|
54
|
|
|
throw new DriverException("Can not submit something that is not a form"); |
|
55
|
|
|
} |
|
56
|
|
|
$this->browser->trigger($element["page_id"], $element["ids"][0], "submit"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Helper method needed for twig and javascript stuff |
|
61
|
|
|
* @param $boolValue |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function boolToString($boolValue) { |
|
65
|
|
|
if ($boolValue === true) { |
|
66
|
|
|
return "1"; |
|
67
|
|
|
} |
|
68
|
|
|
return "0"; |
|
69
|
|
|
} |
|
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) { |
|
80
|
|
|
$element = $this->findElement($xpath, 1); |
|
|
|
|
|
|
81
|
|
|
$tagName = strtolower($this->browser->tagName($element["page_id"], $element["ids"][0])); |
|
82
|
|
|
$attributes = $this->browser->attributes($element["page_id"], $element["ids"][0]); |
|
83
|
|
|
|
|
84
|
|
|
if (!in_array($tagName, array("input", "select"))) { |
|
85
|
|
|
throw new DriverException(sprintf('Impossible to select an option on the element with XPath "%s" as it is not a select or radio input', $xpath)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($tagName === "input" && $attributes["type"] != "radio") { |
|
89
|
|
|
throw new DriverException(sprintf('Impossible to select an option on the element with XPath "%s" as it is not a select or radio input', $xpath)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this->browser->selectOption($element["page_id"], $element["ids"][0], $value, $multiple); |
|
93
|
|
|
} |
|
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) { |
|
102
|
|
|
$element = $this->findElement($xpath, 1); |
|
|
|
|
|
|
103
|
|
|
$tagName = strtolower($this->browser->tagName($element["page_id"], $element["ids"][0])); |
|
104
|
|
|
$attributes = $this->browser->attributes($element["page_id"], $element["ids"][0]); |
|
105
|
|
|
if ($tagName != "input") { |
|
106
|
|
|
throw new DriverException("Can not check when the element is not of the input type"); |
|
107
|
|
|
} |
|
108
|
|
|
if (!in_array($attributes["type"], array("checkbox", "radio"))) { |
|
109
|
|
|
throw new DriverException("Can not check when the element is not checkbox or radio"); |
|
110
|
|
|
} |
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
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) { |
|
120
|
|
|
$this->inputCheckableControl($xpath); |
|
121
|
|
|
$javascript = $this->javascriptTemplateRender("check_element.js.twig", array("xpath" => $xpath, "check" => "true")); |
|
|
|
|
|
|
122
|
|
|
$this->browser->evaluate($javascript); |
|
123
|
|
|
} |
|
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) { |
|
131
|
|
|
$this->inputCheckableControl($xpath); |
|
132
|
|
|
$javascript = $this->javascriptTemplateRender("check_element.js.twig", array("xpath" => $xpath, "check" => "false")); |
|
|
|
|
|
|
133
|
|
|
$this->browser->evaluate($javascript); |
|
134
|
|
|
} |
|
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) { |
|
143
|
|
|
$this->findElement($xpath, 1); |
|
|
|
|
|
|
144
|
|
|
$javascript = $this->javascriptTemplateRender("is_checked.js.twig", array("xpath" => $xpath)); |
|
|
|
|
|
|
145
|
|
|
$checked = $this->browser->evaluate($javascript); |
|
146
|
|
|
|
|
147
|
|
|
if ($checked === null) { |
|
148
|
|
|
throw new DriverException("Can not check when the element is not checkbox or radio"); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $checked; |
|
152
|
|
|
} |
|
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) { |
|
161
|
|
|
$elements = $this->findElement($xpath, 1); |
|
|
|
|
|
|
162
|
|
|
$javascript = $this->javascriptTemplateRender("is_selected.js.twig", array("xpath" => $xpath)); |
|
|
|
|
|
|
163
|
|
|
$tagName = $this->browser->tagName($elements["page_id"], $elements["ids"][0]); |
|
164
|
|
|
if (strcmp(strtolower($tagName), "option") !== 0) { |
|
165
|
|
|
throw new DriverException("Can not assert on element that is not an option"); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $this->browser->evaluate($javascript); |
|
169
|
|
|
} |
|
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
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.