From 8f826185b6d1e34210b84f5dc09e85d20ccabfda Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 7 Feb 2023 13:04:33 +0100 Subject: [PATCH] MOBILE-4069 behat: Allow searching text split in different elements --- src/testing/services/behat-dom.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/testing/services/behat-dom.ts b/src/testing/services/behat-dom.ts index 28fd807f5..270ba3fc8 100644 --- a/src/testing/services/behat-dom.ts +++ b/src/testing/services/behat-dom.ts @@ -24,6 +24,8 @@ import { TestingBehatElementLocator, TestingBehatFindOptions } from './behat-run @Injectable({ providedIn: 'root' }) export class TestingBehatDomUtilsService { + protected static readonly MULTI_ELEM_ALLOWED = ['P', 'SPAN', 'ION-LABEL']; + /** * Check if an element is clickable. * @@ -154,6 +156,7 @@ export class TestingBehatDomUtilsService { }, ); + let fallbackCandidates: ElementsWithExact[] = []; let currentNode: Node | null = null; // eslint-disable-next-line no-cond-assign while (currentNode = treeWalker.nextNode()) { @@ -202,9 +205,24 @@ export class TestingBehatDomUtilsService { elements.push(...this.findElementsBasedOnTextWithinWithExact(childNode, text, options)); } } + + // Allow searching text split into different elements in some cases. + if ( + elements.length === 0 && + currentNode instanceof HTMLElement && + TestingBehatDomUtilsService.MULTI_ELEM_ALLOWED.includes(currentNode.tagName) && + currentNode.innerText.includes(text) + ) { + // Only keep the child elements in the candidates list. + fallbackCandidates = fallbackCandidates.filter(entry => !entry.element.contains(currentNode)); + fallbackCandidates.push({ + element: currentNode, + exact: currentNode.innerText.trim() == text, + }); + } } - return elements; + return elements.length > 0 ? elements : fallbackCandidates; } /**