diff --git a/tests/behat/app_behat_runtime.js b/tests/behat/app_behat_runtime.js index 4d0c9a940..937142cec 100644 --- a/tests/behat/app_behat_runtime.js +++ b/tests/behat/app_behat_runtime.js @@ -526,6 +526,33 @@ } }; + /** + * Scroll an element into view. + * + * @param {object} locator Element locator. + * @return {string} OK if successful, or ERROR: followed by message + */ + const behatScrollTo = function(locator) { + log('Action - scrollTo', { locator }); + + try { + let element = findElementsBasedOnText(locator)[0]; + + if (!element) { + return 'ERROR: No matches for text'; + } + + element = element.closest('ion-item') ?? element.closest('button') ?? element; + + element.scrollIntoView(); + + log('Action - Scrolled to', { locator, element }); + return 'OK'; + } catch (error) { + return 'ERROR: ' + error.message; + } + } + /** * Check whether an item is selected or not. * @@ -678,6 +705,7 @@ pressStandard : behatPressStandard, closePopup : behatClosePopup, find : behatFind, + scrollTo : behatScrollTo, isSelected : behatIsSelected, press : behatPress, setField : behatSetField, diff --git a/tests/behat/behat_app.php b/tests/behat/behat_app.php index 2386f1b2a..67d5ab2ef 100644 --- a/tests/behat/behat_app.php +++ b/tests/behat/behat_app.php @@ -181,6 +181,29 @@ class behat_app extends behat_base { $this->wait_for_pending_js(); } + /** + * Scroll to an element in the app. + * + * @When /^I scroll to (".+") in the app$/ + * @param string $locator + */ + public function i_scroll_to_in_the_app(string $locator) { + $locator = $this->parse_element_locator($locator); + $locatorjson = json_encode($locator); + + $this->spin(function() use ($locatorjson) { + $result = $this->evaluate_script("return window.behat.scrollTo($locatorjson);"); + + if ($result !== 'OK') { + throw new DriverException('Error finding item - ' . $result); + } + + return true; + }); + + $this->wait_for_pending_js(); + } + /** * Trigger swipe gesture. *