|
10 | 10 |
|
11 | 11 | namespace hipanel\tests\_support\Helper;
|
12 | 12 |
|
| 13 | +use Codeception\TestInterface; |
| 14 | + |
13 | 15 | class WaitHelper extends \Codeception\Module
|
14 | 16 | {
|
| 17 | + private $webDriver = null; |
| 18 | + private $webDriverModule = null; |
| 19 | + |
15 | 20 | /**
|
16 | 21 | * @var int Default delay to wait while page updating
|
17 | 22 | */
|
18 |
| - protected int $defaultDelay = 5; |
| 23 | + protected int $defaultDelay = 1; |
| 24 | + |
| 25 | + public function _before(TestInterface $test) |
| 26 | + { |
| 27 | + if (!$this->hasModule('WebDriver') && !$this->hasModule('Selenium2')) { |
| 28 | + throw new \Exception('PageWait uses the WebDriver. Please be sure that this module is activated.'); |
| 29 | + } |
| 30 | + |
| 31 | + // Use WebDriver |
| 32 | + if ($this->hasModule('WebDriver')) { |
| 33 | + $this->webDriverModule = $this->getModule('WebDriver'); |
| 34 | + $this->webDriver = $this->webDriverModule->webDriver; |
| 35 | + } |
| 36 | + } |
19 | 37 |
|
20 | 38 | /**
|
21 | 39 | * @param int $timeOut
|
22 | 40 | * @throws \Codeception\Exception\ModuleException
|
23 | 41 | */
|
24 |
| - public function waitForPageUpdate($timeOut = 180): void |
| 42 | + public function waitForPageUpdate(int $timeOut = 180): void |
25 | 43 | {
|
26 |
| - $I = $this->getModule('WebDriver'); |
| 44 | + $this->webDriverModule = $this->getModule('WebDriver'); |
27 | 45 |
|
28 | 46 | try {
|
29 |
| - $I->waitForJS('return $.active == 0;', $timeOut); |
| 47 | + $this->webDriverModule->waitForJS('return $.active == 0;', $timeOut); |
| 48 | + |
| 49 | + $this->webDriverModule->debug('JS check passed: no active requests.'); |
30 | 50 | } catch (\Facebook\WebDriver\Exception\JavascriptErrorException $exception) {
|
31 |
| - $I->wait($this->defaultDelay); |
| 51 | + $this->webDriverModule->wait($this->defaultDelay); |
| 52 | + |
| 53 | + $this->webDriverModule->debug('JS Error: ' . $exception->getMessage()); |
32 | 54 | }
|
33 | 55 | }
|
| 56 | + |
| 57 | + public function waitPageLoad($timeout = 10) |
| 58 | + { |
| 59 | + $this->webDriverModule->waitForJs('return document.readyState == "complete"', $timeout); |
| 60 | + $this->waitAjaxLoad($timeout); |
| 61 | + } |
| 62 | + |
| 63 | + public function waitAjaxLoad($timeout = 10) |
| 64 | + { |
| 65 | + $this->webDriverModule->waitForJS('return !!window.jQuery && window.jQuery.active == 0;', $timeout); |
| 66 | + $this->webDriverModule->wait($this->defaultDelay); |
| 67 | + } |
34 | 68 | }
|
0 commit comments