Last active
September 27, 2024 07:06
-
-
Save pastuhov/43674b195dc293ffd847 to your computer and use it in GitHub Desktop.
codeception page load wait helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name: AcceptanceTester | |
modules: | |
enabled: | |
... | |
- tests\codeception\common\_support\AcceptanceHelper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace tests\codeception\common\_support; | |
use Codeception\Exception\ModuleException; | |
/** | |
* | |
*/ | |
class AcceptanceHelper extends \Codeception\Module | |
{ | |
private $webDriver = null; | |
private $webDriverModule = null; | |
/** | |
* Event hook before a test starts. | |
* | |
* @param \Codeception\TestCase $test | |
* | |
* @throws \Exception | |
*/ | |
public function _before(\Codeception\TestCase $test) | |
{ | |
if (!$this->hasModule('WebDriver') && !$this->hasModule('Selenium2')) { | |
throw new \Exception('PageWait uses the WebDriver. Please be sure that this module is activated.'); | |
} | |
// Use WebDriver | |
if ($this->hasModule('WebDriver')) { | |
$this->webDriverModule = $this->getModule('WebDriver'); | |
$this->webDriver = $this->webDriverModule->webDriver; | |
} | |
} | |
/** | |
* Ожидание загрузки ajax. | |
* | |
* @param $timeout | |
*/ | |
public function waitAjaxLoad($timeout = 10) | |
{ | |
$this->webDriverModule->waitForJS('return !!window.jQuery && window.jQuery.active == 0;', $timeout); | |
$this->webDriverModule->wait(1); | |
$this->dontSeeJsError(); | |
} | |
/** | |
* Ожидание загрузки страницы. | |
* | |
* @param $timeout | |
*/ | |
public function waitPageLoad($timeout = 10) | |
{ | |
$this->webDriverModule->waitForJs('return document.readyState == "complete"', $timeout); | |
$this->waitAjaxLoad($timeout); | |
$this->dontSeeJsError(); | |
} | |
/** | |
* Переход на страницу. | |
* | |
* @param $link | |
* @param $timeout | |
*/ | |
public function amOnPage($link, $timeout = 10) | |
{ | |
$this->webDriverModule->amOnPage($link); | |
$this->waitPageLoad($timeout); | |
} | |
/** | |
* @param $identifier | |
* @param $elementID | |
* @param $excludeElements | |
* @param $element | |
*/ | |
public function dontSeeVisualChanges($identifier, $elementID = null, $excludeElements = null, $element = false) | |
{ | |
if ($element !== false) { | |
$this->webDriverModule->moveMouseOver($element); | |
} | |
$this->getModule('VisualCeption')->dontSeeVisualChanges($identifier, $elementID, $excludeElements); | |
$this->dontSeeJsError(); | |
} | |
/** | |
* Проверяем отсутствие ошибок в консоли. | |
*/ | |
public function dontSeeJsError() | |
{ | |
$logs = $this->webDriver->manage()->getLog('browser'); | |
foreach ($logs as $log) { | |
if ($log['level'] == 'SEVERE') { | |
throw new ModuleException($this, 'Some error in JavaScript: ' . json_encode($log)); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$authPage = AuthPage::openBy($I); | |
$I->waitPageLoad(); |
Thanks. This is really helpful. Since i am struggeling with codeception and waitings, that is a really good option to prevent flaky testings.
nice
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this code. This is the right way to wait for a page change after a click.