Skip to content

Instantly share code, notes, and snippets.

@pastuhov
Last active December 27, 2021 21:42
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • 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();
@zloyleshiy
Copy link

zloyleshiy commented Aug 5, 2016

хм. использую этот код в CEST
новые методы успешно создаются
а вот 'amOnPage' перетираться не хочет :( используется дефолтный
если изменить имя функции например на 'amOnPages' - то метод успешно добавляется в $I

@ZeWaren
Copy link

ZeWaren commented Oct 27, 2019

Thanks for this code. This is the right way to wait for a page change after a click.

@joriaty-ben
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