@@ -716,6 +716,66 @@ public function i_switch_to_the_browser_tab_opened_by_the_app() {
716716 $ this ->getSession ()->switchToWindow ($ names [1 ]);
717717 }
718718
719+ /**
720+ * Force cron tasks instead of waiting for the next scheduled execution.
721+ *
722+ * @When /^I run cron tasks in the app$/
723+ */
724+ public function i_run_cron_tasks_in_the_app () {
725+ $ session = $ this ->getSession ();
726+
727+ // Force cron tasks execution and wait until they are completed.
728+ $ operationid = random_string ();
729+
730+ $ session ->executeScript (
731+ "cronProvider.forceSyncExecution().then(() => { window['behat_ {$ operationid }_completed'] = true; }); "
732+ );
733+ $ this ->spin (
734+ function () use ($ session , $ operationid ) {
735+ return $ session ->evaluateScript ("window['behat_ {$ operationid }_completed'] || false " );
736+ },
737+ false ,
738+ 60 ,
739+ new ExpectationException ('Forced cron tasks in the app took too long to complete ' , $ session )
740+ );
741+
742+ // Trigger Angular change detection multiple times in case some changes have
743+ // side-effects that result in further pending operations.
744+ for ($ ticks = 5 ; $ ticks > 0 ; $ ticks --) {
745+ $ session ->executeScript ($ this ->islegacy ? 'appRef.tick(); ' : 'changeDetector.detectChanges(); ' );
746+ }
747+ }
748+
749+ /**
750+ * Wait until loading has finished.
751+ *
752+ * @When /^I wait loading to finish in the app$/
753+ */
754+ public function i_wait_loading_to_finish_in_the_app () {
755+ $ session = $ this ->getSession ();
756+
757+ $ this ->spin (
758+ function () use ($ session ) {
759+ $ session ->executeScript ($ this ->islegacy ? 'appRef.tick(); ' : 'changeDetector.detectChanges(); ' );
760+
761+ $ nodes = $ this ->find_all ('css ' , 'core-loading ion-spinner ' );
762+
763+ foreach ($ nodes as $ node ) {
764+ if (!$ node ->isVisible ()) {
765+ continue ;
766+ }
767+
768+ return false ;
769+ }
770+
771+ return true ;
772+ },
773+ false ,
774+ 60 ,
775+ new ExpectationException ('"Loading took too long to complete ' , $ session )
776+ );
777+ }
778+
719779 /**
720780 * Closes the current browser tab.
721781 *
@@ -780,4 +840,17 @@ public function parse_negation(string $not): bool {
780840 return !empty ($ not );
781841 }
782842
843+ /**
844+ * Replaces $WWWROOT for the url of the Moodle site.
845+ *
846+ * @Transform /^(.*\$WWWROOT.*)$/
847+ * @param string $text Text.
848+ * @return string
849+ */
850+ public function replace_wwwroot ($ text ) {
851+ global $ CFG ;
852+
853+ return str_replace ('$WWWROOT ' , $ CFG ->behat_wwwroot , $ text );
854+ }
855+
783856}
0 commit comments