Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CElementQuery.php « web « include « tests « ui - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cd2d03babb174250449cb877410d0237c9a88f12 (plain)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
<?php
/*
** Zabbix
** Copyright (C) 2001-2022 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__).'/CElement.php';
require_once dirname(__FILE__).'/CElementCollection.php';
require_once dirname(__FILE__).'/CElementFilter.php';
require_once dirname(__FILE__).'/elements/CNullElement.php';
require_once dirname(__FILE__).'/elements/CFormElement.php';
require_once dirname(__FILE__).'/elements/CGridFormElement.php';
require_once dirname(__FILE__).'/elements/CCheckboxFormElement.php';
require_once dirname(__FILE__).'/elements/CTableElement.php';
require_once dirname(__FILE__).'/elements/CTableRowElement.php';
require_once dirname(__FILE__).'/elements/CWidgetElement.php';
require_once dirname(__FILE__).'/elements/CDashboardElement.php';
require_once dirname(__FILE__).'/elements/CListElement.php';
require_once dirname(__FILE__).'/elements/CDropdownElement.php';
require_once dirname(__FILE__).'/elements/CCheckboxElement.php';
require_once dirname(__FILE__).'/elements/COverlayDialogElement.php';
require_once dirname(__FILE__).'/elements/CMainMenuElement.php';
require_once dirname(__FILE__).'/elements/CMessageElement.php';
require_once dirname(__FILE__).'/elements/CMultiselectElement.php';
require_once dirname(__FILE__).'/elements/CSegmentedRadioElement.php';
require_once dirname(__FILE__).'/elements/CCheckboxListElement.php';
require_once dirname(__FILE__).'/elements/CMultifieldTableElement.php';
require_once dirname(__FILE__).'/elements/CMultilineElement.php';
require_once dirname(__FILE__).'/elements/CColorPickerElement.php';
require_once dirname(__FILE__).'/elements/CCompositeInputElement.php';
require_once dirname(__FILE__).'/elements/CPopupMenuElement.php';
require_once dirname(__FILE__).'/elements/CPopupButtonElement.php';
require_once dirname(__FILE__).'/elements/CInputGroupElement.php';
require_once dirname(__FILE__).'/elements/CHostInterfaceElement.php';
require_once dirname(__FILE__).'/elements/CFilterTabElement.php';

require_once dirname(__FILE__).'/IWaitable.php';
require_once dirname(__FILE__).'/WaitableTrait.php';
require_once dirname(__FILE__).'/CastableTrait.php';

use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\Exception\NoSuchElementException;
use Facebook\WebDriver\Exception\WebDriverException;

/**
 * Element selection query.
 */
class CElementQuery implements IWaitable {

	/**
	 * Query can be used as waitable object to wait for elements before retrieving them.
	 */
	use WaitableTrait;

	/**
	 * Query can be used as castable object to cast elements to specific type when elements are retrieved.
	 */
	use CastableTrait;

	/**
	 * Wait iteration step duration.
	 */
	const WAIT_ITERATION = 50;

	/**
	 * Element selector.
	 *
	 * @var WebDriverBy
	 */
	protected $by;

	/**
	 * Selector context (can be set to specific element or to global level).
	 *
	 * @var mixed
	 */
	protected $context;

	/**
	 * Class to be used to instantiate elements.
	 *
	 * @var string
	 */
	protected $class;

	/**
	 * Options applied to instantiated elements.
	 *
	 * @var array
	 */
	protected $options = [];

	/**
	 * Element reverse order flag.
	 *
	 * @var boolean
	 */
	protected $reverse_order = false;

	/**
	 * Shared web page instance.
	 *
	 * @var CPage
	 */
	protected static $page;

	/**
	 * Last input element selector.
	 *
	 * @var string
	 */
	protected static $selector;

	/**
	 * Initialize element query by specified selector.
	 *
	 * @param mixed  $type     selector type (method) or selector
	 * @param string $locator  locator part of selector
	 */
	public function __construct($type, $locator = null) {
		$this->class = 'CElement';
		$this->context = static::getDriver();

		if ($type !== null) {
			$this->by = static::getSelector($type, $locator);
		}
	}

	/**
	 * Get selector from type and locator.
	 *
	 * @param mixed  $type     selector type (method) or selector
	 * @param string $locator  locator part of selector
	 *
	 * @return WebDriverBy
	 */
	public static function getSelector($type, $locator = null) {
		if ($type instanceof WebDriverBy) {
			return $type;
		}

		if ($locator === null) {
			if (!is_array($type)) {
				$parts = explode(':', $type, 2);
				if (count($parts) !== 2) {
					throw new Exception('Element selector "'.$type.'" is not well formatted.');
				}

				list($type, $locator) = $parts;
			}
			else {
				$selectors = [];
				foreach ($type as $selector) {
					$selectors[] = './/'.CXPathHelper::fromSelector($selector);
				}

				$type = 'xpath';
				$locator = implode('|', $selectors);
			}
		}
		else if (is_array($locator)) {
			foreach ($locator as $selector) {
				$selectors[] = './/'.CXPathHelper::fromSelector($type, $selector);
			}

			$type = 'xpath';
			$locator = implode('|', $selectors);
		}

		$mapping = [
			'css' => 'cssSelector',
			'class' => 'className',
			'tag' => 'tagName',
			'link' => 'linkText',
			'button' => function () use ($locator) {
				return WebDriverBy::xpath('.//button[normalize-space(text())='.CXPathHelper::escapeQuotes($locator).']');
			}
		];

		if (array_key_exists($type, $mapping)) {
			if (is_callable($mapping[$type])) {
				return call_user_func($mapping[$type]);
			}
			else {
				$type = $mapping[$type];
			}
		}

		return call_user_func([WebDriverBy::class, $type], $locator);
	}

	/**
	 * Set query context.
	 *
	 * @param CElement $context    context to be set
	 */
	public function setContext($context) {
		$this->context = $context;
	}

	/**
	 * Get query context.
	 *
	 * @return CElement
	 */
	public function getContext() {
		return $this->context;
	}

	/**
	 * Get last selector.
	 *
	 * @return string|null
	 */
	public static function getLastSelector() {
		return static::$selector;
	}

	/**
	 * Set web page instance.
	 *
	 * @param CPage $page    web page instance to be set
	 */
	public static function setPage($page) {
		self::$page = $page;
	}

	/**
	 * Get web driver instance.
	 *
	 * @return RemoteWebDriver
	 */
	public static function getDriver() {
		if (self::$page === null) {
			return null;
		}

		return self::$page->getDriver();
	}

	/**
	 * Set reversed element order flag.
	 *
	 * @param boolean $order    order to set
	 *
	 * @return $this
	 */
	public function setReversedOrder($order = true) {
		$this->reverse_order = $order;

		return $this;
	}

	/**
	 * Get web page instance.
	 *
	 * @return CPage
	 */
	public static function getPage() {
		return self::$page;
	}

	/**
	 * Apply chained element query.
	 *
	 * @param mixed  $type     selector type (method) or selector
	 * @param string $locator  locator part of selector
	 *
	 * @return $this
	 */
	public function query($type, $locator = null) {
		$prefix = ($this->by->getMechanism() !== 'xpath')
			? './/'.CXPathHelper::fromWebDriverBy($this->by)
			: $this->by->getValue();

		if ($this->reverse_order) {
			$prefix .= '[1]';
			$this->reverse_order = false;
		}

		$by = self::getSelector($type, $locator);
		$suffix = ($by->getMechanism() !== 'xpath')
				? '//'.CXPathHelper::fromWebDriverBy($by)
				: $by->getValue();

		if (substr($suffix, 0, 1) !== '/') {
			$suffix = '/'.$suffix;
		}

		$this->by = static::getSelector('xpath', $prefix.$suffix);

		return $this;
	}

	/**
	 * Get wait instance.
	 *
	 * @return WebDriverWait
	 */
	public static function wait($timeout = 20, $iteration = null) {
		if ($iteration === null) {
			$iteration = self::WAIT_ITERATION;
		}

		return static::getDriver()->wait($timeout, self::WAIT_ITERATION);
	}

	/**
	 * Wait until condition is met for target.
	 *
	 * @param IWaitable $target       target for wait operation
	 * @param string    $condition    condition to be waited for
	 * @param array     $params       condition params
	 */
	public static function waitUntil($target, $condition, $params = []) {
		$selector = $target->getSelectorAsText();
		if ($selector !== null) {
			$selector = ' located by '.$selector;
		}

		$callable = call_user_func_array([$target, CElementFilter::getConditionCallable($condition)], $params);
		self::wait()->until($callable, 'Failed to wait for element'.$selector.' to be '.$condition.'.');
	}

	/**
	 * Get one element located by specified query.
	 *
	 * @param boolean $should_exist    if method is allowed to return null as a result
	 *
	 * @return CElement
	 */
	public function one($should_exist = true) {
		$class = $this->class;
		$parent = ($this->context !== static::getDriver()) ? $this->context : null;

		for ($i = 0; $i < 2; $i++) {
			try {
				if (!$this->reverse_order) {
					$element = $this->context->findElement($this->by);
				}
				else {
					$elements = $this->context->findElements($this->by);
					if (!$elements) {
						throw new NoSuchElementException(null);
					}

					$element = end($elements);
				}

				break;
			}
			catch (NoSuchElementException $exception) {
				if (!$should_exist) {
					return new CNullElement(array_merge($this->options, ['parent' => $parent, 'by' => $this->by]));
				}

				throw $exception;
			}
			// Workaround for communication errors present on Jenkins
			catch (WebDriverException $exception) {
				if (strpos($exception->getMessage(), 'START_MAP') === false) {
					throw $exception;
				}
			}
		}

		return call_user_func([$class, 'createInstance'], $element, array_merge($this->options, [
			'parent' => $parent,
			'by' => $this->by
		]));
	}

	/**
	 * Get all elements located by specified query.
	 *
	 * @return CElement
	 */
	public function all() {
		$class = $this->class;

		try {
			$elements = $this->context->findElements($this->by);
		}
		// Workaround for communication errors present on Jenkins
		catch (WebDriverException $exception) {
			if (strpos($exception->getMessage(), 'START_MAP') === false) {
				throw $exception;
			}

			$elements = $this->context->findElements($this->by);
		}

		if ($this->reverse_order) {
			$elements = array_reverse($elements);
		}

		if ($this->class !== 'RemoteWebElement') {
			foreach ($elements as &$element) {
				$element = call_user_func([$class, 'createInstance'], $element, $this->options);
			}
			unset($element);
		}

		return new CElementCollection($elements, $class);
	}

	/**
	 * Get count of elements located by specified query.
	 *
	 * @return integer
	 */
	public function count() {
		return $this->all()->count();
	}

	/**
	 * Set element class and options.
	 *
	 * @param string $class      class to be used to instantiate elements
	 * @param array  $options    additional options passed to object
	 *
	 * @return $this
	 */
	public function cast($class, $options = []) {
		$this->class = $class;
		$this->options = $options;

		return $this;
	}

	/**
	 * @inheritdoc
	 */
	public function getClickableCondition() {
		$target = $this;

		return function () use ($target) {
			return $target->one(false)->isClickable();
		};
	}

	/**
	 * @inheritdoc
	 */
	public function getReadyCondition() {
		$driver = static::getDriver();

		return function () use ($driver) {
			return $driver->executeScript('return document.readyState === \'complete\' && (window.jQuery||{active:0}).active === 0;');
		};
	}

	/**
	 * @inheritdoc
	 */
	public function getPresentCondition() {
		$target = $this;

		return function () use ($target) {
			return $target->one(false)->isValid();
		};
	}

	/**
	 * @inheritdoc
	 */
	public function getTextPresentCondition($text) {
		$target = $this;

		return function () use ($target, $text) {
			$element = $target->one(false);
			if (!$element->isValid()) {
				return false;
			}

			return (strpos($element->getText(), $text) !== false);
		};
	}

	/**
	 * @inheritdoc
	 */
	public function getAttributesPresentCondition($attributes) {
		$target = $this;

		return function () use ($target, $attributes) {
			$element = $target->one(false);
			if (!$element->isValid()) {
				return false;
			}

			foreach ($attributes as $key => $value) {
				if (is_numeric($key) && $element->getAttribute($value) === null) {
					return false;
				}
				elseif ($element->getAttribute($key) !== $value) {
					return false;
				}
			}

			return true;
		};
	}

	/**
	 * @inheritdoc
	 */
	public function getVisibleCondition() {
		$target = $this;

		return function () use ($target) {
			return $target->one(false)->isVisible();
		};
	}

	/**
	 * @inheritdoc
	 */
	public function getSelectedCondition() {
		$target = $this;

		return function () use ($target) {
			return $target->one(false)->isSelected();
		};
	}

	/**
	 * Check that the corresponding element exists.
	 *
	 * @return boolean
	 */
	public function exists() {
		return $this->one(false)->isValid();
	}

	/**
	 * Get input element from container.
	 *
	 * @param CElement     $target    container element
	 * @param string       $prefix    xpath prefix
	 * @param array|string $class     element classes to look for
	 *
	 * @return CElement|CNullElement
	 */
	public static function getInputElement($target, $prefix = './', $class = null) {
		$classes = [
			'CElement'					=> [
				// TODO: change after DEV-1630 (1) is resolved.
				'/input[@name][not(@type) or @type="text" or @type="password"][not(@style) or not(contains(@style,"display: none"))]',
				'/textarea[@name]'
			],
			'CListElement'				=> '/select[@name]',
			'CDropdownElement'			=> '/z-select[@name]',
			'CCheckboxElement'			=> '/input[@name][@type="checkbox" or @type="radio"]',
			'CMultiselectElement'		=> [
				'/div[contains(@class, "multiselect-control")]',
				'/div/div[contains(@class, "multiselect-control")]' // TODO: remove after fix DEV-1071.
			],
			'CSegmentedRadioElement'	=> [
				'/ul[contains(@class, "radio-list-control")]',
				'/ul/li/ul[contains(@class, "radio-list-control")]',
				'/div/ul[contains(@class, "radio-list-control")]' // TODO: remove after fix DEV-1071.
			],
			'CCheckboxListElement'		=> [
				'/div/div[@class="columns-wrapper columns-3"]', // TODO: fix after DEV-1859.
				'/ul[contains(@class, "checkbox-list")]',
				'/ul[contains(@class, "list-check-radio")]'
			],
			'CHostInterfaceElement'		=> [
				'/div/div[contains(@class, "interface-container")]/../..'
			],
			'CMultifieldTableElement'	=> [
				'/table',
				'/div/table', // TODO: remove after fix DEV-1071.
				'/*[contains(@class, "table-forms-separator")]/table'
			],
			'CCompositeInputElement'	=> [
				'/div[contains(@class, "range-control")]',
				'/div[contains(@class, "calendar-control")]'
			],
			'CColorPickerElement'		=> '/div[contains(@class, "color-picker")]',
			'CMultilineElement'			=> '/div[contains(@class, "multilineinput-control")]',
			'CInputGroupElement'		=> '/div[contains(@class, "macro-input-group")]'
		];

		if ($class !== null) {
			if (!is_array($class)) {
				$class = [$class];
			}

			foreach (array_keys($classes) as $name) {
				if (!in_array($name, $class)) {
					unset($classes[$name]);
				}
			}
		}

		foreach ($classes as $class => $selectors) {
			if (!is_array($selectors)) {
				$selectors = [$selectors];
			}

			$xpaths = [];
			foreach ($selectors as $selector) {
				$xpaths[] = $prefix.$selector;
			}

			static::$selector = 'xpath:'.implode('|', $xpaths);
			$element = $target->query(static::$selector)->cast($class)->one(false);
			if ($element->isValid()) {
				return $element;
			}
		}

		static::$selector = null;
		return new CNullElement(['locator' => 'input element']);
	}
}