Skip to content

Instantly share code, notes, and snippets.

@pastuhov
Last active September 27, 2024 07:06
Show Gist options
  • Save pastuhov/43674b195dc293ffd847 to your computer and use it in GitHub Desktop.
Save pastuhov/43674b195dc293ffd847 to your computer and use it in GitHub Desktop.
codeception page load wait helper
class_name: AcceptanceTester
modules:
enabled:
...
- tests\codeception\common\_support\AcceptanceHelper
<?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));
}
}
}
}
$authPage = AuthPage::openBy($I);
$I->waitPageLoad();
@joriatyBen
Copy link

Thanks. This is really helpful. Since i am struggeling with codeception and waitings, that is a really good option to prevent flaky testings.

@ildarius
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment