MOBILE-3926 behat: Add split-view scoping to find

main
Noel De Martin 2021-12-01 14:24:43 +01:00
parent e9266ad9fd
commit 9724088a87
2 changed files with 23 additions and 13 deletions

View File

@ -349,10 +349,16 @@
* Function to find elements based on their text or Aria label.
*
* @param {object} locator Element locator.
* @param {boolean} insideSplitView Whether to search only inside the split view contents.
* @return {HTMLElement} Found elements
*/
const findElementsBasedOnText = function(locator) {
const topContainer = document.querySelector('ion-alert, ion-popover, ion-action-sheet, core-ion-tab.show-tab ion-page.show-page, ion-page.show-page, html');
const findElementsBasedOnText = function(locator, insideSplitView) {
let topContainer = document.querySelector('ion-alert, ion-popover, ion-action-sheet, core-ion-tab.show-tab ion-page.show-page, ion-page.show-page, html');
if (insideSplitView) {
topContainer = topContainer.querySelector('core-split-view ion-router-outlet');
}
let container = topContainer;
if (topContainer && locator.near) {
@ -382,7 +388,7 @@
if (filteredElements.length > 0) {
return filteredElements;
}
} while ((container = getParentElement(container)) && container !== topContainer);
} while (container !== topContainer && (container = getParentElement(container)) && container !== topContainer);
return [];
};
@ -497,21 +503,23 @@
};
/**
* Function to find an arbitrary item based on its text or aria label.
* Function to find an arbitrary element based on its text or aria label.
*
* @param {object} locator Element locator.
* @param {boolean} insideSplitView Whether to search only inside the split view contents.
* @return {string} OK if successful, or ERROR: followed by message
*/
const behatFind = function(locator) {
log('Action - Find', locator);
const behatFind = function(locator, insideSplitView) {
log('Action - Find', { locator, insideSplitView });
try {
const element = findElementsBasedOnText(locator)[0];
const element = findElementsBasedOnText(locator, insideSplitView)[0];
if (!element) {
return 'ERROR: No matches for text';
}
log('Action - Found', { locator, insideSplitView, element });
return 'OK';
} catch (error) {
return 'ERROR: ' + error.message;
@ -574,7 +582,6 @@
return isElementVisible(title, document.body);
});
if (titles.length > 1) {
return 'ERROR: Too many possible titles';
} else if (!titles.length) {
@ -597,7 +604,6 @@
const behatSetField = function(field, value) {
log('Action - Set field ' + field + ' to: ' + value);
const found = findElementsBasedOnText({ text: field, selector: 'input, textarea, [contenteditable="true"]' })[0];
if (!found) {
return 'ERROR: No matches for text';
@ -656,6 +662,8 @@
* @return {object} Component instance
*/
const behatGetComponentInstance = function(selector, className) {
log('Action - Get component instance ' + selector + ', ' + className);
const activeElement = Array.from(document.querySelectorAll(`.ion-page:not(.ion-page-hidden) ${selector}`)).pop();
if (!activeElement || !activeElement.__ngContext__) {

View File

@ -154,16 +154,18 @@ class behat_app extends behat_base {
/**
* Finds elements in the app.
*
* @Then /^I should( not)? find (".+") in the app$/
* @Then /^I should( not)? find (".+")( inside the split-view content)? in the app$/
* @param bool $not
* @param string $locator
* @param bool $insidesplitview
*/
public function i_find_in_the_app(bool $not, string $locator) {
public function i_find_in_the_app(bool $not, string $locator, bool $insidesplitview = false) {
$locator = $this->parse_element_locator($locator);
$locatorjson = json_encode($locator);
$insidesplitviewjson = json_encode($insidesplitview);
$this->spin(function() use ($not, $locatorjson) {
$result = $this->evaluate_script("return window.behat.find($locatorjson);");
$this->spin(function() use ($not, $locatorjson, $insidesplitviewjson) {
$result = $this->evaluate_script("return window.behat.find($locatorjson, $insidesplitviewjson);");
if ($not && $result === 'OK') {
throw new DriverException('Error, found an item that should not be found');