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 | 57 | function __construct($type, $selector, array $filters = array()) |
|
|
|||
49 | { |
||
50 | 57 | $this->_type = $type === NULL ? self::DEFAULT_TYPE : $type; |
|
51 | 57 | $this->_selector = $selector; |
|
52 | 57 | $this->_filters = $filters; |
|
53 | } |
||
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 | 2 | public function is_filtered(Node $item, $index) |
|
62 | { |
||
63 | foreach ($this->filters() as $filter => $value) |
||
64 | { |
||
65 | try { |
||
66 | switch ($filter) |
||
67 | { |
||
68 | case 'at': |
||
69 | $matches_filter = $this->filter_by_at($item, $index, $value); |
||
70 | break; |
||
71 | |||
72 | case 'value': |
||
73 | $matches_filter = $this->filter_by_value($item, $index, $value); |
||
74 | 1 | break; |
|
75 | |||
76 | case 'text': |
||
77 | $matches_filter = $this->filter_by_text($item, $index, $value); |
||
78 | break; |
||
79 | |||
80 | case 'visible': |
||
81 | $matches_filter = $this->filter_by_visible($item, $index, $value); |
||
82 | 1 | break; |
|
83 | |||
84 | case 'attributes': |
||
85 | $matches_filter = $this->filter_by_attributes($item, $index, $value); |
||
86 | 1 | break; |
|
87 | |||
88 | default: |
||
89 | throw new Exception('Filter :filter does not exist', array(':filter' => $filter)); |
||
90 | } |
||
91 | |||
92 | if ( ! $matches_filter) |
||
93 | 1 | return FALSE; |
|
94 | |||
95 | } catch (Exception_Staleelement $e) { |
||
96 | return FALSE; |
||
97 | 1 | } |
|
98 | } |
||
99 | |||
100 | 2 | 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 | public function filter_by_at(Node $item, $index, $value) |
||
112 | { |
||
113 | return $index == $value; |
||
114 | } |
||
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 | public function filter_by_value(Node $item, $index, $value) |
||
125 | { |
||
126 | return $item->value() == $value; |
||
127 | } |
||
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 | public function filter_by_text(Node $item, $index, $value) |
||
138 | { |
||
139 | $text = $item->text(); |
||
140 | |||
141 | return ($text AND $value AND mb_stripos($text, $value) !== FALSE); |
||
142 | } |
||
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 | public function filter_by_visible(Node $item, $index, $value) |
||
153 | { |
||
154 | return $item->is_visible() === $value; |
||
155 | } |
||
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 | foreach ($value as $attribute_name => $attribute_val) |
||
168 | { |
||
169 | 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 | 41 | public function xpath() |
|
181 | { |
||
182 | 9 | if ( ! $this->_xpath) |
|
183 | { |
||
184 | 41 | switch ($this->type()) |
|
185 | { |
||
186 | 41 | case 'css': |
|
187 | $this->_xpath = '//'.$this->convert_css_selector_to_xpath($this->selector()); |
||
188 | 13 | break; |
|
189 | |||
190 | 30 | case 'xpath': |
|
191 | $this->_xpath = $this->selector(); |
||
192 | 1 | break; |
|
193 | |||
194 | 29 | case 'field': |
|
195 | $this->_xpath = $this->field_to_xpath($this->selector()); |
||
196 | 13 | break; |
|
197 | |||
198 | 18 | case 'label': |
|
199 | $this->_xpath = $this->label_to_xpath($this->selector()); |
||
200 | 1 | break; |
|
201 | |||
202 | 17 | case 'link': |
|
203 | $this->_xpath = $this->link_to_xpath($this->selector()); |
||
204 | 9 | break; |
|
205 | |||
206 | 14 | case 'button': |
|
207 | $this->_xpath = $this->button_to_xpath($this->selector()); |
||
208 | 12 | break; |
|
209 | |||
210 | default: |
||
211 | throw new Exception('Locator type ":type" does not exist', array(':type' => $this->_type)); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | 7 | return $this->_xpath; |
|
216 | 2 | } |
|
217 | |||
218 | /** |
||
219 | * Getter. Current type, one of css, field, xpath, label, link or button |
||
220 | * @return string |
||
221 | */ |
||
222 | 47 | public function type() |
|
226 | |||
227 | /** |
||
228 | * Getter. Current selector |
||
229 | * @return string |
||
230 | */ |
||
231 | 28 | public function selector() |
|
235 | |||
236 | /** |
||
237 | * Getter. Current filters |
||
238 | * @return array |
||
239 | */ |
||
240 | 6 | public function filters() |
|
244 | |||
245 | /** |
||
246 | * Convert field selector into xpath |
||
247 | * |
||
248 | * @param string $selector |
||
249 | * @return string |
||
250 | */ |
||
251 | 13 | public function field_to_xpath($selector) |
|
252 | { |
||
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 | 1 | public function __toString() |
|
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 | 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.