Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

zloyleshiy commented Aug 5, 2016

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

@ZeWaren

This comment has been minimized.

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.

@saggzz

This comment has been minimized.

Copy link

saggzz commented Feb 10, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.