From e755ff568dc82e5b61b772344a270cb16e453171 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 8 Jun 2022 16:39:06 +0200 Subject: [PATCH] MOBILE-4069 behat: Stop searching containers once element found --- src/testing/services/behat-dom.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/testing/services/behat-dom.ts b/src/testing/services/behat-dom.ts index e0b1af642..e3d892407 100644 --- a/src/testing/services/behat-dom.ts +++ b/src/testing/services/behat-dom.ts @@ -326,7 +326,7 @@ export class TestsBehatDomUtils { * @return First found element. */ static findElementBasedOnText(locator: TestBehatElementLocator, containerName = ''): HTMLElement { - return this.findElementsBasedOnText(locator, containerName)[0]; + return this.findElementsBasedOnText(locator, containerName, true)[0]; } /** @@ -334,13 +334,25 @@ export class TestsBehatDomUtils { * * @param locator Element locator. * @param containerName Whether to search only inside a specific container. + * @param stopWhenFound Stop looking in containers once an element is found. * @return Found elements */ - protected static findElementsBasedOnText(locator: TestBehatElementLocator, containerName = ''): HTMLElement[] { + protected static findElementsBasedOnText( + locator: TestBehatElementLocator, + containerName = '', + stopWhenFound = false, + ): HTMLElement[] { const topContainers = this.getCurrentTopContainerElements(containerName); + let elements: HTMLElement[] = []; - return topContainers.reduce((elements, container) => - elements.concat(this.findElementsBasedOnTextInContainer(locator, container)), []); + for (let i = 0; i < topContainers.length; i++) { + elements = elements.concat(this.findElementsBasedOnTextInContainer(locator, topContainers[i])); + if (stopWhenFound && elements.length) { + break; + } + } + + return elements; } /** @@ -357,7 +369,7 @@ export class TestsBehatDomUtils { let container: HTMLElement | null = topContainer; if (locator.within) { - const withinElements = this.findElementsBasedOnText(locator.within); + const withinElements = this.findElementsBasedOnTextInContainer(locator.within, topContainer); if (withinElements.length === 0) { throw new Error('There was no match for within text'); @@ -375,7 +387,7 @@ export class TestsBehatDomUtils { } if (topContainer && locator.near) { - const nearElements = this.findElementsBasedOnText(locator.near); + const nearElements = this.findElementsBasedOnTextInContainer(locator.near, topContainer); if (nearElements.length === 0) { throw new Error('There was no match for near text');