From e784850b5a85fa683d948246451ca14cb428791d Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Fri, 5 Dec 2014 11:47:08 +0800 Subject: [PATCH] MDL-48318 behat: Fixed JS Exception M is not created on all pages, like cron and Steps which check for exported data. So we need to ensure M is defined, before using it in behat_hooks::wait_for_pending_js() --- lib/tests/behat/behat_hooks.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 81b1182b966..e7f64f0b77f 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -404,7 +404,18 @@ class behat_hooks extends behat_base { for ($i = 0; $i < self::EXTENDED_TIMEOUT * 10; $i++) { $pending = ''; try { - $jscode = 'return ' . self::PAGE_READY_JS . ' ? "" : M.util.pending_js.join(":");'; + $jscode = + 'if (typeof M === "undefined") { + if (document.readyState === "complete") { + return ""; + } else { + return "incomplete"; + } + } else if (' . self::PAGE_READY_JS . ') { + return ""; + } else { + return M.util.pending_js.join(":"); + }'; $pending = $this->getSession()->evaluateScript($jscode); } catch (NoSuchWindow $nsw) { // We catch an exception here, in case we just closed the window we were interacting with.