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

CPage.php « web « include « tests « ui - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cc2abc0f9fa45e6803da9b94f2fb6457b028dcf4 (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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
<?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__).'/CElementQuery.php';
require_once dirname(__FILE__).'/CommandExecutor.php';

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\RemoteWebElement;
use Facebook\WebDriver\WebDriverDimension;
use Facebook\WebDriver\Exception\NoSuchAlertException;
use Facebook\WebDriver\WebDriverExpectedCondition;

/**
 * Web page implementation.
 */
class CPage {

	/**
	 * Page defaults.
	 */
	const DEFAULT_PAGE_WIDTH = 1440;
	const DEFAULT_PAGE_HEIGHT = 1024;

	/**
	 * Web driver instance.
	 *
	 * @var RemoteWebDriver
	 */
	protected $driver;

	/**
	 * Local cookie cache.
	 *
	 * @var array
	 */
	protected static $cookie = null;

	/**
	 * Page height.
	 *
	 * @var integer
	 */
	protected $height = null;

	/**
	 * Page width.
	 *
	 * @var integer
	 */
	protected $width = null;

	/**
	 * Viewport freeze flag.
	 *
	 * @var boolean
	 */
	protected $viewportUpdated = false;

	/**
	 * Web driver and CElementQuery initialization.
	 */
	public function __construct() {
		$this->connect();
		CElementQuery::setPage($this);
	}

	/**
	 * Web driver initialization.
	 */
	public function connect() {
		$capabilities = DesiredCapabilities::chrome();
		if (defined('PHPUNIT_BROWSER_NAME')) {
			$capabilities->setBrowserName(PHPUNIT_BROWSER_NAME);
		}

		if (!defined('PHPUNIT_BROWSER_NAME') || PHPUNIT_BROWSER_NAME === 'chrome') {
			$options = new ChromeOptions();
			$options->addArguments([
				'--no-sandbox',
				'--enable-font-antialiasing=false',
				'--window-size='.self::DEFAULT_PAGE_WIDTH.','.self::DEFAULT_PAGE_HEIGHT,
				'--disable-dev-shm-usage'
			]);

			$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
		}

		$phpunit_driver_address = PHPUNIT_DRIVER_ADDRESS;

		if (strpos($phpunit_driver_address, ':') === false) {
			$phpunit_driver_address .= ':4444';
		}

		$this->driver = RemoteWebDriver::create('http://'.$phpunit_driver_address.'/wd/hub', $capabilities);

		$this->driver->manage()->window()->setSize(
				new WebDriverDimension(self::DEFAULT_PAGE_WIDTH, self::DEFAULT_PAGE_HEIGHT)
		);
	}

	/**
	 * Perform page cleanup.
	 * Close all popup windows, switch to the initial window, remove cookies.
	 */
	public function cleanup() {
		$this->resetViewport();

		if (self::$cookie !== null) {
			foreach ($this->driver->manage()->getCookies() as $cookie) {
				if ($cookie->getName() === 'zbx_session') {
					if ($cookie->getValue() !== self::$cookie['value']) {
						self::$cookie = null;
					}
					break;
				}
			}
		}

		$this->driver->manage()->deleteAllCookies();
		try {
			$this->driver->executeScript('sessionStorage.clear();');
		} catch (Exception $exception) {
			// Code is not missing here.
		}

		$windows = $this->driver->getWindowHandles();
		if (count($windows) <= 1) {
			return true;
		}

		try {
			foreach (array_slice($windows, 1) as $window) {
				$this->driver->switchTo()->window($window);
				$this->driver->close();
			}
		}
		catch (Exception $exception) {
			// Error handling is not missing here.
		}

		if (count($this->driver->getWindowHandles()) >= 1) {
			try {
				$this->driver->switchTo()->window($windows[0]);
			}
			catch (Exception $exception) {
				return false;
			}
		}

		return true;
	}

	/**
	 * Destroy web page.
	 */
	public function destroy() {
		$this->driver->quit();
		self::$cookie = null;
	}

	/**
	 * Reconnect web driver.
	 */
	public function reset() {
		$this->destroy();
		$this->connect();
	}

	/**
	 * Login as specified user.
	 *
	 * @param string  $sessionid
	 * @param integer $userid
	 *
	 * @return $this
	 */
	public function login($sessionid = '09e7d4286dfdca4ba7be15e0f3b2b55a', $userid = 1) {
		$session = CDBHelper::getRow('SELECT status FROM sessions WHERE sessionid='.zbx_dbstr($sessionid));

		if (!$session) {
			DBexecute('INSERT INTO sessions (sessionid,userid) VALUES ('.zbx_dbstr($sessionid).','.$userid.')');
		}
		elseif ($session['status'] != 0) {	/* ZBX_SESSION_ACTIVE */
			DBexecute('UPDATE sessions SET status=0 WHERE sessionid='.zbx_dbstr($sessionid));
		}

		if (self::$cookie !== null) {
			$cookie = json_decode(base64_decode(urldecode(self::$cookie['value'])), true);
		}

		if (self::$cookie === null || $sessionid !== $cookie['sessionid']) {
			$data = ['sessionid' => $sessionid];

			$config = CDBHelper::getRow('SELECT session_key FROM config WHERE configid=1');
			$data['sign'] = hash_hmac('sha256', json_encode($data), $config['session_key'], false);

			$path = parse_url(PHPUNIT_URL, PHP_URL_PATH);
			self::$cookie = [
				'name' => 'zbx_session',
				'value' => base64_encode(json_encode($data)),
				'domain' => parse_url(PHPUNIT_URL, PHP_URL_HOST),
				'path' => rtrim(substr($path, 0, strrpos($path, '/')), '/')
			];

			$this->driver->get(PHPUNIT_URL);
		}

		$this->driver->manage()->addCookie(self::$cookie);

		return $this;
	}

	/**
	 * Logout and clean cookies.
	 */
	public function logout() {
		try {
			// Before logout open page without any scripts, otherwise session might be restored and logout won't work.
			$this->open('setup.php');

			$session = null;

			if (self::$cookie === null) {
				foreach ($this->driver->manage()->getCookies() as $cookie) {
					if ($cookie->getName() === 'zbx_session') {
						$session = $cookie->getValue();
						break;
					}
				}
			}
			else {
				$session = self::$cookie['value'];
			}

			if ($session !== null) {
				DBExecute('DELETE FROM sessions WHERE sessionid='.zbx_dbstr($session));
			}

			$this->driver->manage()->deleteAllCookies();
			self::$cookie = null;
		}
		catch (\Exception $e) {
			throw new \Exception('Cannot logout user: '.$e->getTraceAsString());
		}
	}

	/**
	 * Open specified URL.
	 *
	 * @param string $url   URL to be opened.
	 *
	 * @return $this
	 */
	public function open($url) {
		$this->driver->get(PHPUNIT_URL.$url);

		return $this;
	}

	/**
	 * Get page title.
	 *
	 * @return string
	 */
	public function getTitle() {
		return $this->driver->getTitle();
	}

	/**
	 * Get current page URL.
	 *
	 * @return string
	 */
	public function getCurrentUrl() {
		return $this->driver->getCurrentURL();
	}

	/**
	 * Set width and height of viewport.
	 *
	 * @param int $width
	 * @param int $height
	 */
	protected function setViewport($width, $height) {
		try {
			CommandExecutor::executeCustom($this->driver, [
				'cmd' => 'Emulation.setDeviceMetricsOverride',
				'params' => [
					'width'				=> $width,
					'height'			=> $height,
					'deviceScaleFactor'	=> 1,
					'mobile'			=> false,
					'fitWindow'			=> false
				]
			]);
		} catch (Exception $exception) {
			// Code is not missing here.
		}
	}

	/**
	 * Setting "frozen" viewport size.
	 */
	public function updateViewport() {
		try {
			if (!$this->driver->executeScript('return !!window.chrome;')) {
				throw new Exception();
			}
		} catch (Exception $exception) {
			return false;
		}

		try {
			// Calculate page width and height depending on sidemenu and scrollbars presence.
			$size = $this->driver->executeScript(
				'var side = document.getElementsByClassName("sidebar")[0];'.
				'var wrapper = document.getElementsByClassName("wrapper")[0];'.

				'var width = ((typeof side !== "undefined") ? side.scrollWidth : 0)'.
					'+ ((typeof wrapper !== "undefined") ? wrapper.scrollWidth : 0);'.
				'var height = Math.max((typeof wrapper !== "undefined") ? wrapper.scrollHeight : 0,'.
				'(typeof side !== "undefined") ? side.scrollHeight : 0);'.
				'return'.
					'[(width !== 0) ? width : window.getComputedStyle(document.documentElement)["width"],'.
					'(height !== 0) ? height : window.getComputedStyle(document.documentElement)["height"],'.
					'(typeof wrapper !== "undefined" && wrapper.scrollWidth >'.
						'parseInt(window.getComputedStyle(wrapper)["width"], 10)) ? 20 : 0];'
				);

			$this->width = (int)$size[0];

			// Screenshot is 1px smaller to ensure that scroll is still present.
			$this->height = (int)$size[1] - 1;

			if ($this->height > self::DEFAULT_PAGE_HEIGHT || $this->width > self::DEFAULT_PAGE_WIDTH) {
				$this->setViewport(max([
					// Add 20px to page width when vertical scroll presents.
					$this->width + (int)$size[2], self::DEFAULT_PAGE_WIDTH]),
					max([$this->height, self::DEFAULT_PAGE_HEIGHT
				]));

				$this->viewportUpdated = true;
			}
		} catch (Exception $exception) {
			// Code is not missing here.
		}

		return true;
	}

	/**
	 * Resetting viewport size to default.
	 */
	public function resetViewport() {
		if ($this->viewportUpdated === false) {
			return;
		}

		if (isset($this->height) && $this->height > self::DEFAULT_PAGE_HEIGHT) {
			try {
				CommandExecutor::executeCustom($this->driver, [
					'cmd' => 'Emulation.clearDeviceMetricsOverride',
					'params' => ['clear' => true]
				]);
			} catch (Exception $exception) {
				// Code is not missing here.
			}

			$this->height = self::DEFAULT_PAGE_HEIGHT;
		}

		$this->viewportUpdated = false;
	}

	/**
	 * Take screenshot of current page.
	 *
	 * @return string
	 */
	protected function takePageScreenshot() {
		if ($this->viewportUpdated === true || !$this->updateViewport()) {
			return $this->driver->takeScreenshot();
		}

		$screenshot = $this->driver->takeScreenshot();
		$this->resetViewport();

		return $screenshot;
	}

	/**
	 * Take screenshot of current page or page element.
	 *
	 * @param CElement|null $element    page element to get screenshot of
	 *
	 * @return string
	 */
	public function takeScreenshot($element = null) {
		$screenshot = $this->takePageScreenshot();

		if ($element !== null) {
			$screenshot = CImageHelper::getImageRegion($screenshot, $element->getRect());
		}

		return $screenshot;
	}

	/**
	 * Get browser logs.
	 *
	 * @return array
	 */
	public function getBrowserLog() {
		return $this->driver->manage()->getLog('browser');
	}

	/**
	 * Get page source.
	 *
	 * @return type
	 */
	public function getSource() {
		return $this->driver->getPageSource();
	}

	/**
	 * Wait until page is ready.
	 */
	public function waitUntilReady() {
		return (new CElementQuery(null))->waitUntilReady();
	}

	/**
	 * Check if alert is present.
	 *
	 * @return boolean
	 */
	public function isAlertPresent() {
		return ($this->getAlertText() !== null);
	}

	/**
	 * Get alert text.
	 *
	 * @return string|null
	 */
	public function getAlertText() {
		try {
			return $this->driver->switchTo()->alert()->getText();
		}
		catch (NoSuchAlertException $exception) {
			return null;
		}
	}

	/**
	 * Wait until alert is present and accept it.
	 */
	public function acceptAlert() {
		CElementQuery::wait()->until(WebDriverExpectedCondition::alertIsPresent());
		$this->driver->switchTo()->alert()->accept();
	}

	/**
	 * Wait until alert is present and dismiss it.
	 */
	public function dismissAlert() {
		CElementQuery::wait()->until(WebDriverExpectedCondition::alertIsPresent());
		$this->driver->switchTo()->alert()->dismiss();
	}

	/**
	 * Emulate key presses.
	 *
	 * @param array|string $keys   keys to be pressed
	 */
	public function pressKey($keys) {
		if (!is_array($keys)) {
			$keys = [$keys];
		}

		$keyboard = $this->driver->getKeyboard();
		foreach ($keys as $key) {
			$keyboard->pressKey($key);
		}
	}

	/**
	 * Create CElementQuery instance.
	 * @see CElementQuery
	 *
	 * @param string $type     selector type (method) or selector
	 * @param string $locator  locator part of selector
	 *
	 * @return CElementQuery
	 */
	public function query($type, $locator = null) {
		return new CElementQuery($type, $locator);
	}

	/**
	 * Get web driver instance.
	 *
	 * @return RemoteWebDriver
	 */
	public function getDriver() {
		return $this->driver;
	}

	/**
	 * Remove focus from the element.
	 */
	public function removeFocus() {
		try {
			$this->driver->executeScript('for (var i = 0; i < 5; i++) if (document.activeElement.tagName !== "BODY")'.
					' document.activeElement.blur(); else break;');
		}
		catch (\Exception $ex) {
			throw new \Exception('Cannot remove focus.');
		}
	}

	/**
	 * Refresh page.
	 *
	 * @return $this
	 */
	public function refresh() {
		$this->driver->navigate()->refresh();

		return $this;
	}

	/**
	 * Switching to frame or iframe.
	 *
	 * @param CElement|string|array|null $element    iframe element
	 *
	 * @return $this
	 */
	public function switchTo($element = null) {
		if ($element === null) {
			$this->driver->switchTo()->defaultContent();

			return $this;
		}

		if (is_string($element)) {
			$element = $this->query($element)->one(false);
		}
		elseif (is_array($element)) {
			$element = $this->query($element[0], $element[1])->one(false);
		}

		if ($element instanceof RemoteWebElement) {
			$this->driver->switchTo()->frame($element);
		}
		else {
			throw new \Exception('Cannot switch to frame that is not an element.');
		}

		return $this;
	}

	/**
	 * Allows to login with user credentials.
	 *
	 * @param string $alias     Username on login screen
	 * @param string $password  Password on login screen
	 * @param string $url		Direct link to certain Zabbix page
	 */
	public function userLogin($alias, $password, $url = 'index.php') {
		if (self::$cookie === null) {
			$this->driver->get(PHPUNIT_URL);
		}

		$this->logout();
		$this->open($url);
		$this->query('id:name')->waitUntilVisible()->one()->fill($alias);
		$this->query('id:password')->one()->fill($password);
		$this->query('id:enter')->one()->click();
		$this->waitUntilReady();

		// Make sure that logged in page is opened.
		try {
			$this->query('xpath://aside[@class="sidebar"]//a[text()="User settings"]')->exists();
		}
		catch (\Exception $ex) {
			throw new \Exception('"User settings" menu is not found on page. Probably user is not logged in.');
		}
	}

	/**
	 * Check page title text.
	 *
	 * @param string $title		page title
	 *
	 * @throws Exception
	 */
	public function assertTitle($title) {
		global $ZBX_SERVER_NAME;

		if ($ZBX_SERVER_NAME !== '') {
			$title = $ZBX_SERVER_NAME.NAME_DELIMITER.$title;
		}

		$text = $this->getTitle();
		if ($text !== $title) {
			throw new \Exception('Title of the page "'.$text.'" is not equal to "'.$title.'".');
		}
	}

	/**
	 * Check page header.
	 *
	 * @param string $header	page header to be compared
	 *
	 * @throws Exception
	 */
	public function assertHeader($header) {
		$text = $this->query('xpath://h1[@id="page-title-general"]')->one()->getText();

		if ($text !== $header) {
			throw new \Exception('Header of the page "'.$text.'" is not equal to "'.$header.'".');
		}
	}

	/**
	 * Scroll page to the top position.
	 */
	public function scrollToTop() {
		$this->getDriver()->executeScript('document.getElementsByClassName(\'wrapper\')[0].scrollTo(0, 0)');
	}
}