From 4632eeb932242da4a30780bfc4804fefc1c85dbe Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 7 Jul 2022 10:39:36 +0200 Subject: [PATCH] MOBILE-4110 behat: Fix JS calls --- .../tests/behat/behat_app.php | 4 +- .../tests/behat/behat_app_helper.php | 47 +------------------ 2 files changed, 3 insertions(+), 48 deletions(-) diff --git a/local_moodleappbehat/tests/behat/behat_app.php b/local_moodleappbehat/tests/behat/behat_app.php index 4b7bbbf70..befe86f47 100644 --- a/local_moodleappbehat/tests/behat/behat_app.php +++ b/local_moodleappbehat/tests/behat/behat_app.php @@ -565,7 +565,7 @@ class behat_app extends behat_app_helper { */ public function i_pull_to_refresh_in_the_app() { $this->spin(function() { - $result = $this->js('await window.behat.pullToRefresh();'); + $result = $this->runtime_js('pullToRefresh()'); if ($result !== 'OK') { throw new DriverException('Error pulling to refresh - ' . $result); @@ -824,7 +824,7 @@ class behat_app extends behat_app_helper { $this->getSession()->switchToWindow($names[1]); } - $this->js('window.close()'); + $this->evaluate_script('window.close()'); $this->getSession()->switchToWindow($names[0]); } diff --git a/local_moodleappbehat/tests/behat/behat_app_helper.php b/local_moodleappbehat/tests/behat/behat_app_helper.php index 6b9fa3a9e..9f8236183 100644 --- a/local_moodleappbehat/tests/behat/behat_app_helper.php +++ b/local_moodleappbehat/tests/behat/behat_app_helper.php @@ -433,51 +433,6 @@ class behat_app_helper extends behat_base { } } - /** - * Evaluate and execute scripts checking for promises if needed. - * - * @param string $script - * @return mixed Resolved promise result. - */ - protected function js(string $script) { - $scriptnoreturn = preg_replace('/^return\s+/', '', $script); - $scriptnoreturn = preg_replace('/;$/', '', $scriptnoreturn); - - if (!preg_match('/^await\s+/', $scriptnoreturn)) { - // No async. - return $this->evaluate_script($script); - } - - $script = preg_replace('/^await\s+/', '', $scriptnoreturn); - - $start = microtime(true); - $promisevariable = 'PROMISE_RESULT_' . time(); - $timeout = self::get_extended_timeout(); - - $res = $this->evaluate_script("Promise.resolve($script) - .then(result => window.$promisevariable = result) - .catch(error => window.$promisevariable = 'Async code rejected: ' + error?.message)"); - - do { - if (microtime(true) - $start > $timeout) { - throw new DriverException("Async script not resolved after $timeout seconds"); - } - - // 0.1 seconds. - usleep(100000); - } while (!$this->evaluate_script("'$promisevariable' in window")); - - $result = $this->evaluate_script("window.$promisevariable"); - - $this->evaluate_script("delete window.$promisevariable"); - - if (is_string($result) && strrpos($result, 'Async code rejected:') === 0) { - throw new DriverException($result); - } - - return $result; - } - /** * Evaluate and execute methods from the Behat runtime. * @@ -485,7 +440,7 @@ class behat_app_helper extends behat_base { * @return mixed Result. */ protected function runtime_js(string $script) { - return $this->js("window.behat.$script"); + return $this->evaluate_script("window.behat.$script"); } /**