1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2018 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
require_once 'vendor/autoload.php';
require_once dirname(__FILE__).'/CBaseElement.php';
require_once dirname(__FILE__).'/CElementQuery.php';
require_once dirname(__FILE__).'/IWaitable.php';
require_once dirname(__FILE__).'/WaitableTrait.php';
require_once dirname(__FILE__).'/CastableTrait.php';
/**
* Generic web page element.
*/
class CElement extends CBaseElement implements IWaitable {
/**
* Element can be used as waitable object to wait for element state changes.
*/
use WaitableTrait;
/**
* Element can be used as castable object to cast element to specific type.
*/
use CastableTrait;
/**
* Element selector.
*
* @var WebDriverBy
*/
protected $by;
/**
* Parent element (if any).
* Parent element is never set for elements retrieved from element collection.
*
* @var CElement
*/
protected $parent;
/**
* Initialize element.
*
* @param RemoteWebElement $element
* @param type $options
*/
public function __construct(RemoteWebElement $element, $options = []) {
$this->setElement($element);
$this->normalize();
foreach ($options as $key => $value) {
$this->$key = $value;
}
}
/**
* Simplified selector for elements that can be located directly on page.
* @throws Exception
*/
public static function find() {
throw new Exception('Element cannot be located without selector.');
}
/**
* Invalidate element state.
* This method should be overridden in order to reset cached objects that will be broken during reload operation.
* @see CBaseElement::reload
*/
public function invalidate() {
// Code is not missing here.
}
/**
* Reload stalled element if reload is possible.
*
* @return $this
*
* @throws Exception
*/
public function reload() {
if ($this->by === null) {
throw new Exception('Cannot reload stalled element selected as a part of multi-element selection.');
}
if ($this->parent !== null) {
$this->parent->reload();
}
$this->invalidate();
$query = new CElementQuery($this->by);
if ($this->parent !== null) {
$query->setContext($this->parent);
}
$this->setElement($query->one());
$this->normalize();
return $this;
}
/**
* Set new base element.
*
* @param RemoteWebElement $element element to be set
*/
protected function setElement(RemoteWebElement $element) {
$this->executor = $element->executor;
$this->id = $element->id;
$this->fileDetector = $element->fileDetector;
}
/**
* Perform element selector normalization.
* This method can be overridden to check if element is selected properly.
*/
protected function normalize() {
// Code is not missing here.
}
/**
* Get parent selection query.
*
* @param mixed $type selector type (method) or selector
* @param string $locator locator part of selector
*
* @return CElementQuery
*/
public function parents($type, $locator = null) {
return $this->query('xpath:./ancestor::'.CXPathHelper::fromSelector($type, $locator));
}
/**
* Get element query with current element context.
*
* @param mixed $type selector type (method) or selector
* @param string $locator locator part of selector
*
* @return CElementQuery
*/
public function query($type, $locator = null) {
$query = new CElementQuery($type, $locator);
$query->setContext($this);
return $query;
}
/**
* Dispatch HTML event to an element.
*
* @param string $event event type
*
* @return $this
*/
public function fireEvent($event = 'change') {
$driver = CElementQuery::getDriver();
$driver->executeScript('arguments[0].dispatchEvent(new Event(arguments[1]));', [$this, $event]);
return $this;
}
/**
* Highlight element by setting orange border around it.
* This method should be used for test debugging purposes only.
*
* @return $this
*/
public function highlight() {
$driver = CElementQuery::getDriver();
$driver->executeScript('arguments[0].style.border="3px solid #ff9800";', [$this]);
return $this;
}
/**
* Get instance of specified element class from current element.
*
* @param string $class class to be casted to
* @param array $options additional options passed to object
*
* @return CElement
*/
public function cast($class, $options = []) {
return new $class($this, array_merge($options, ['parent' => $this->parent, 'by' => $this->by]));
}
/**
* Check if element is clickable.
*
* @return boolean
*/
public function isClickable() {
return $this->isDisplayed() && $this->isEnabled();
}
/**
* @inheritdoc
*/
public function getClickableCondition() {
$target = $this;
return function () use ($target) {
return $target->isClickable();
};
}
/**
* @inheritdoc
*/
public function getPresentCondition() {
$target = $this;
return function () use ($target) {
return !$target->isStalled();
};
}
/**
* @inheritdoc
*/
public function getVisibleCondition() {
$target = $this;
return function () use ($target) {
return $target->isVisible();
};
}
/**
* @inheritdoc
*/
public function getTextPresentCondition($text) {
$target = $this;
return function () use ($target, $text) {
return (strpos($target->getText(), $text) !== false);
};
}
/**
* @inheritdoc
*/
public function getAttributesPresentCondition($attributes) {
$target = $this;
return function () use ($target, $attributes) {
foreach ($attributes as $key => $value) {
if (is_numeric($key) && $target->getAttribute($value) === null) {
return false;
}
elseif ($target->getAttribute($key) !== $value) {
return false;
}
}
return true;
};
}
/**
* @inheritdoc
*/
public function getReadyCondition() {
return $this->getClickableCondition();
}
/**
* Wait until element changes it's state from stalled to normal.
*
* @return $this
* @throws Exception
*/
public function waitUntilReloaded() {
if ($this->by === null) {
throw new Exception('Cannot wait for element reload on element selected in multi-element query.');
}
$element = $this;
CElementQuery::wait()->until(function () use ($element) {
if ($element->isStalled()) {
$element->reload();
return !$element->isStalled();
}
return null;
}
);
return $this;
}
/**
* Wait until element is selected.
*
* @return $this
*/
public function waitUntilSelected() {
CElementQuery::wait()->until(WebDriverExpectedCondition::elementToBeSelected($this));
return $this;
}
}
|