diff --git a/tests/behat/behat_app.php b/tests/behat/behat_app.php index ba732ee74..a7cb59a8b 100644 --- a/tests/behat/behat_app.php +++ b/tests/behat/behat_app.php @@ -716,6 +716,66 @@ class behat_app extends behat_base { $this->getSession()->switchToWindow($names[1]); } + /** + * Force cron tasks instead of waiting for the next scheduled execution. + * + * @When /^I run cron tasks in the app$/ + */ + public function i_run_cron_tasks_in_the_app() { + $session = $this->getSession(); + + // Force cron tasks execution and wait until they are completed. + $operationid = random_string(); + + $session->executeScript( + "cronProvider.forceSyncExecution().then(() => { window['behat_{$operationid}_completed'] = true; });" + ); + $this->spin( + function() use ($session, $operationid) { + return $session->evaluateScript("window['behat_{$operationid}_completed'] || false"); + }, + false, + 60, + new ExpectationException('Forced cron tasks in the app took too long to complete', $session) + ); + + // Trigger Angular change detection multiple times in case some changes have + // side-effects that result in further pending operations. + for ($ticks = 5; $ticks > 0; $ticks--) { + $session->executeScript($this->islegacy ? 'appRef.tick();' : 'changeDetector.detectChanges();'); + } + } + + /** + * Wait until loading has finished. + * + * @When /^I wait loading to finish in the app$/ + */ + public function i_wait_loading_to_finish_in_the_app() { + $session = $this->getSession(); + + $this->spin( + function() use ($session) { + $session->executeScript($this->islegacy ? 'appRef.tick();' : 'changeDetector.detectChanges();'); + + $nodes = $this->find_all('css', 'core-loading ion-spinner'); + + foreach ($nodes as $node) { + if (!$node->isVisible()) { + continue; + } + + return false; + } + + return true; + }, + false, + 60, + new ExpectationException('"Loading took too long to complete', $session) + ); + } + /** * Closes the current browser tab. * @@ -780,4 +840,17 @@ class behat_app extends behat_base { return !empty($not); } + /** + * Replaces $WWWROOT for the url of the Moodle site. + * + * @Transform /^(.*\$WWWROOT.*)$/ + * @param string $text Text. + * @return string + */ + public function replace_wwwroot($text) { + global $CFG; + + return str_replace('$WWWROOT', $CFG->behat_wwwroot, $text); + } + } diff --git a/tests/behat/behat_local_moodlemobileapp.php b/tests/behat/behat_local_moodlemobileapp.php deleted file mode 100644 index 1629e0e41..000000000 --- a/tests/behat/behat_local_moodlemobileapp.php +++ /dev/null @@ -1,113 +0,0 @@ -. - -/** - * Behat step definitions. - * - * @package local_apps - * @copyright 2010 Moodle Pty Ltd (http://moodle.com) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use Behat\Mink\Exception\ExpectationException; - -// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. - -require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); - -/** - * Behat step definitions. - * - * @package local_apps - * @copyright 2010 Moodle Pty Ltd (http://moodle.com) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class behat_local_moodlemobileapp extends behat_base { - - /** - * Force cron tasks instead of waiting for the next scheduled execution. - * - * @When /^I run cron tasks in the app$/ - */ - public function i_run_cron_tasks_in_the_app() { - $session = $this->getSession(); - - // Force cron tasks execution and wait until they are completed. - $operationid = random_string(); - - $session->executeScript( - "cronProvider.forceSyncExecution().then(() => { window['behat_{$operationid}_completed'] = true; });" - ); - $this->spin( - function() use ($session, $operationid) { - return $session->evaluateScript("window['behat_{$operationid}_completed'] || false"); - }, - false, - 60, - new ExpectationException('Forced cron tasks in the app took too long to complete', $session) - ); - - // Trigger Angular digest cycle multiple times in case some changes have - // side-effects that result in further pending operations. - for ($ticks = 5; $ticks > 0; $ticks--) { - $session->executeScript('appRef.tick();'); - } - } - - /** - * Wait until loading has finished. - * - * @When /^I wait loading to finish in the app$/ - */ - public function i_wait_loading_to_finish_in_the_app() { - $session = $this->getSession(); - - $this->spin( - function() use ($session) { - $session->executeScript('appRef.tick();'); - - $nodes = $this->find_all('css', 'core-loading ion-spinner'); - - foreach ($nodes as $node) { - if (!$node->isVisible()) { - continue; - } - - return false; - } - - return true; - }, - false, - 60, - new ExpectationException('"Loading took too long to complete', $session) - ); - } - - /** - * Replaces $WWWROOT for the url of the Moodle site. - * - * @Transform /^(.*\$WWWROOT.*)$/ - * @param string $text Text. - * @return string - */ - public function arg_replace_wwwroot($text) { - global $CFG; - - return str_replace('$WWWROOT', $CFG->behat_wwwroot, $text); - } - -}