1 | <?php |
||
15 | class Locator { |
||
16 | |||
17 | const DEFAULT_TYPE = 'css'; |
||
18 | |||
19 | /** |
||
20 | * Converted xpath cache |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $_xpath; |
||
24 | |||
25 | /** |
||
26 | * The type of the locator, can be css, field, xpath, label, link or button |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $_type; |
||
30 | |||
31 | /** |
||
32 | * The selector used to generate the xpath |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $_selector; |
||
36 | |||
37 | /** |
||
38 | * Additional filters to apply after the xpath is found |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $_filters; |
||
42 | |||
43 | /** |
||
44 | * @param string $type |
||
45 | * @param string $selector |
||
46 | * @param array $filters |
||
47 | */ |
||
48 | 67 | function __construct($type, $selector, array $filters = array()) |
|
54 | |||
55 | /** |
||
56 | * Check if a Node item matches the current filters |
||
57 | * @param Node $item |
||
58 | * @param integer $index |
||
59 | * @return boolean |
||
60 | */ |
||
61 | 3 | public function is_filtered(Node $item, $index) |
|
62 | { |
||
63 | 3 | foreach ($this->filters() as $filter => $value) |
|
64 | { |
||
65 | try { |
||
66 | switch ($filter) |
||
67 | { |
||
68 | 3 | case 'at': |
|
69 | 2 | $matches_filter = $this->filter_by_at($item, $index, $value); |
|
70 | 2 | break; |
|
71 | |||
72 | 2 | case 'value': |
|
73 | 2 | $matches_filter = $this->filter_by_value($item, $index, $value); |
|
74 | 2 | break; |
|
75 | |||
76 | 2 | case 'text': |
|
77 | 1 | $matches_filter = $this->filter_by_text($item, $index, $value); |
|
78 | 1 | break; |
|
79 | |||
80 | 1 | case 'visible': |
|
81 | 1 | $matches_filter = $this->filter_by_visible($item, $index, $value); |
|
82 | 1 | break; |
|
83 | |||
84 | 1 | case 'attributes': |
|
85 | 1 | $matches_filter = $this->filter_by_attributes($item, $index, $value); |
|
86 | 1 | break; |
|
87 | |||
88 | default: |
||
89 | 1 | throw new Exception('Filter :filter does not exist', array(':filter' => $filter)); |
|
90 | } |
||
91 | |||
92 | 3 | if ( ! $matches_filter) |
|
93 | 3 | return FALSE; |
|
94 | |||
95 | 1 | } catch (Exception_Staleelement $e) { |
|
96 | 3 | return FALSE; |
|
97 | } |
||
98 | } |
||
99 | |||
100 | 3 | return TRUE; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Try matching "at" filter |
||
105 | * |
||
106 | * @param Node $item |
||
107 | * @param integer $index |
||
108 | * @param string $value |
||
109 | * @return boolean |
||
110 | */ |
||
111 | 2 | public function filter_by_at(Node $item, $index, $value) |
|
115 | |||
116 | /** |
||
117 | * Try matching "value" filter |
||
118 | * |
||
119 | * @param Node $item |
||
120 | * @param integer $index |
||
121 | * @param string $value |
||
122 | * @return boolean |
||
123 | */ |
||
124 | 2 | public function filter_by_value(Node $item, $index, $value) |
|
128 | |||
129 | /** |
||
130 | * Try matching "text" filter |
||
131 | * |
||
132 | * @param Node $item |
||
133 | * @param integer $index |
||
134 | * @param string $value |
||
135 | * @return boolean |
||
136 | */ |
||
137 | 2 | public function filter_by_text(Node $item, $index, $value) |
|
143 | |||
144 | /** |
||
145 | * Try matching "visible" filter |
||
146 | * |
||
147 | * @param Node $item |
||
148 | * @param integer $index |
||
149 | * @param boolean $value |
||
150 | * @return boolean |
||
151 | */ |
||
152 | 1 | public function filter_by_visible(Node $item, $index, $value) |
|
156 | |||
157 | /** |
||
158 | * Try matching "attributes" filter |
||
159 | * |
||
160 | * @param Node $item |
||
161 | * @param integer $index |
||
162 | * @param array $value |
||
163 | * @return boolean |
||
164 | */ |
||
165 | 1 | public function filter_by_attributes(Node $item, $index, array $value) |
|
166 | { |
||
167 | 1 | foreach ($value as $attribute_name => $attribute_val) |
|
168 | { |
||
169 | 1 | if ($item->attribute($attribute_name) != $attribute_val) |
|
170 | 1 | return FALSE; |
|
171 | } |
||
172 | |||
173 | 1 | return TRUE; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Return the xpath representaiton of the selector, using the appropriate method |
||
178 | * @return string |
||
179 | */ |
||
180 | 49 | public function xpath() |
|
181 | { |
||
182 | 49 | if ( ! $this->_xpath) |
|
183 | { |
||
184 | 49 | switch ($this->type()) |
|
185 | { |
||
186 | 49 | case 'css': |
|
187 | 20 | $this->_xpath = '//'.$this->convert_css_selector_to_xpath($this->selector()); |
|
188 | 20 | break; |
|
189 | |||
190 | 35 | case 'xpath': |
|
191 | 1 | $this->_xpath = $this->selector(); |
|
192 | 1 | break; |
|
193 | |||
194 | 34 | case 'field': |
|
195 | 17 | $this->_xpath = $this->field_to_xpath($this->selector()); |
|
196 | 17 | break; |
|
197 | |||
198 | 21 | case 'label': |
|
199 | 1 | $this->_xpath = $this->label_to_xpath($this->selector()); |
|
200 | 1 | break; |
|
201 | |||
202 | 20 | case 'link': |
|
203 | 9 | $this->_xpath = $this->link_to_xpath($this->selector()); |
|
204 | 9 | break; |
|
205 | |||
206 | 14 | case 'button': |
|
207 | 12 | $this->_xpath = $this->button_to_xpath($this->selector()); |
|
208 | 12 | break; |
|
209 | |||
210 | default: |
||
211 | 2 | throw new Exception('Locator type ":type" does not exist', array(':type' => $this->_type)); |
|
212 | } |
||
213 | } |
||
214 | |||
215 | 47 | return $this->_xpath; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * Getter. Current type, one of css, field, xpath, label, link or button |
||
220 | * @return string |
||
221 | */ |
||
222 | 58 | public function type() |
|
226 | |||
227 | /** |
||
228 | * Getter. Current selector |
||
229 | * @return string |
||
230 | */ |
||
231 | 49 | public function selector() |
|
235 | |||
236 | /** |
||
237 | * Getter. Current filters |
||
238 | * @return array |
||
239 | */ |
||
240 | 18 | public function filters() |
|
244 | |||
245 | /** |
||
246 | * Convert field selector into xpath |
||
247 | * |
||
248 | * @param string $selector |
||
249 | * @return string |
||
250 | */ |
||
251 | 17 | public function field_to_xpath($selector) |
|
263 | |||
264 | /** |
||
265 | * Convert label selector to xpath |
||
266 | * @param string $selector |
||
267 | * @return string |
||
268 | */ |
||
269 | 1 | public function label_to_xpath($selector) |
|
280 | |||
281 | /** |
||
282 | * Convert link selector to xpath |
||
283 | * @param string $selector |
||
284 | * @return string |
||
285 | */ |
||
286 | 9 | public function link_to_xpath($selector) |
|
295 | |||
296 | /** |
||
297 | * Convert button selector to xpath |
||
298 | * @param string $selector |
||
299 | * @return string |
||
300 | */ |
||
301 | 12 | public function button_to_xpath($selector) |
|
314 | |||
315 | /** |
||
316 | * Return a pretty printed representation of the locator |
||
317 | * @return string |
||
318 | */ |
||
319 | 2 | public function __toString() |
|
320 | { |
||
321 | 2 | if ($this->filters()) |
|
322 | { |
||
323 | 1 | $filters = " filters: ".json_encode($this->filters()); |
|
324 | } |
||
325 | else |
||
326 | { |
||
327 | 1 | $filters = ''; |
|
328 | } |
||
329 | |||
330 | 2 | return "Locator: ({$this->type()}) {$this->selector()}".$filters; |
|
331 | } |
||
332 | |||
333 | /** |
||
334 | * Convert a CSS selector to XPath. |
||
335 | * Uses Symfony CSS Selector 2.8 and above API if possible. |
||
336 | * Otherwise fallback to pre-2.8 static interface. |
||
337 | * |
||
338 | * @param string $css_selector CSS selector |
||
339 | * @return string XPath selector |
||
340 | */ |
||
341 | 20 | private function convert_css_selector_to_xpath($css_selector) |
|
350 | } |
||
351 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.