From d178865b384ab885962d2d7c2e52e370a2379691 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Tue, 12 Feb 2019 10:13:51 +0000 Subject: [PATCH] MDL-63977 Behat: Organise app functions in window.behat object --- lib/behat/behat_base.php | 2 +- lib/tests/behat/app_behat_runtime.js | 19 ++++++++++++++----- lib/tests/behat/behat_app.php | 10 +++++----- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/behat/behat_base.php b/lib/behat/behat_base.php index e971a35a1ed..cd8f0dc4d46 100644 --- a/lib/behat/behat_base.php +++ b/lib/behat/behat_base.php @@ -486,7 +486,7 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext { } // Check on page to see if it's an app page. Safest way is to look for added JavaScript. - return $this->getSession()->evaluateScript('typeof window.behatPress') === 'function'; + return $this->getSession()->evaluateScript('typeof window.behat') === 'object'; } /** diff --git a/lib/tests/behat/app_behat_runtime.js b/lib/tests/behat/app_behat_runtime.js index b07b53e6373..d88fa92b7b8 100644 --- a/lib/tests/behat/app_behat_runtime.js +++ b/lib/tests/behat/app_behat_runtime.js @@ -389,7 +389,7 @@ * @param {string} button Type of button to press * @return {string} OK if successful, or ERROR: followed by message */ - window.behatPressStandard = function(button) { + var behatPressStandard = function(button) { log('Action - Click standard button: ' + button); var selector; switch (button) { @@ -438,7 +438,7 @@ * * @return {string} OK if successful, or ERROR: followed by message */ - window.behatClosePopup = function() { + var behatClosePopup = function() { log('Action - Close popup'); var backdrops = Array.from(document.querySelectorAll('ion-backdrop')); @@ -474,7 +474,7 @@ * @param {string} near Optional 'near' text - if specified, must have a single match on page * @return {string} OK if successful, or ERROR: followed by message */ - window.behatPress = function(text, near) { + var behatPress = function(text, near) { log('Action - Press ' + text + (near === undefined ? '' : ' - near ' + near)); var found; @@ -510,7 +510,7 @@ * * @return {string} OK: followed by header text if successful, or ERROR: followed by message. */ - window.behatGetHeader = function() { + var behatGetHeader = function() { log('Action - Get header'); var result = null; @@ -541,7 +541,7 @@ * @param {string} value New value * @return {string} OK or ERROR: followed by message */ - window.behatSetField = function(field, value) { + var behatSetField = function(field, value) { log('Action - Set field ' + field + ' to: ' + value); // Find input(s) with given placeholder. @@ -635,4 +635,13 @@ return 'OK'; }; + + // Make some functions publicly available for Behat to call. + window.behat = { + pressStandard : behatPressStandard, + closePopup : behatClosePopup, + press : behatPress, + setField : behatSetField, + getHeader : behatGetHeader, + }; })(); diff --git a/lib/tests/behat/behat_app.php b/lib/tests/behat/behat_app.php index 322d5cf92a0..f84b356ee42 100644 --- a/lib/tests/behat/behat_app.php +++ b/lib/tests/behat/behat_app.php @@ -358,7 +358,7 @@ class behat_app extends behat_base { */ public function i_press_the_standard_button_in_the_app(string $button) { $this->spin(function($context, $args) use ($button) { - $result = $this->getSession()->evaluateScript('return window.behatPressStandard("' . + $result = $this->getSession()->evaluateScript('return window.behat.pressStandard("' . $button . '");'); if ($result !== 'OK') { throw new DriverException('Error pressing standard button - ' . $result); @@ -376,7 +376,7 @@ class behat_app extends behat_base { */ public function i_close_the_popup_in_the_app() { $this->spin(function($context, $args) { - $result = $this->getSession()->evaluateScript('return window.behatClosePopup();'); + $result = $this->getSession()->evaluateScript('return window.behat.closePopup();'); if ($result !== 'OK') { throw new DriverException('Error closing popup - ' . $result); } @@ -434,7 +434,7 @@ class behat_app extends behat_base { } else { $nearbit = ''; } - $result = $context->getSession()->evaluateScript('return window.behatPress("' . + $result = $context->getSession()->evaluateScript('return window.behat.press("' . addslashes_js($text) . '"' . $nearbit .');'); if ($result !== 'OK') { throw new DriverException('Error pressing item - ' . $result); @@ -457,7 +457,7 @@ class behat_app extends behat_base { */ public function i_set_the_field_in_the_app(string $field, string $value) { $this->spin(function($context, $args) use ($field, $value) { - $result = $this->getSession()->evaluateScript('return window.behatSetField("' . + $result = $this->getSession()->evaluateScript('return window.behat.setField("' . addslashes_js($field) . '", "' . addslashes_js($value) . '");'); if ($result !== 'OK') { throw new DriverException('Error setting field - ' . $result); @@ -479,7 +479,7 @@ class behat_app extends behat_base { */ public function the_header_should_be_in_the_app(string $text) { $result = $this->spin(function($context, $args) { - $result = $this->getSession()->evaluateScript('return window.behatGetHeader();'); + $result = $this->getSession()->evaluateScript('return window.behat.getHeader();'); if (substr($result, 0, 3) !== 'OK:') { throw new DriverException('Error getting header - ' . $result); } -- 2.43.0