Skip to content

Commit 8f82618

Browse files
committed
MOBILE-4069 behat: Allow searching text split in different elements
1 parent ebb6e39 commit 8f82618

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/testing/services/behat-dom.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { TestingBehatElementLocator, TestingBehatFindOptions } from './behat-run
2424
@Injectable({ providedIn: 'root' })
2525
export class TestingBehatDomUtilsService {
2626

27+
protected static readonly MULTI_ELEM_ALLOWED = ['P', 'SPAN', 'ION-LABEL'];
28+
2729
/**
2830
* Check if an element is clickable.
2931
*
@@ -154,6 +156,7 @@ export class TestingBehatDomUtilsService {
154156
},
155157
);
156158

159+
let fallbackCandidates: ElementsWithExact[] = [];
157160
let currentNode: Node | null = null;
158161
// eslint-disable-next-line no-cond-assign
159162
while (currentNode = treeWalker.nextNode()) {
@@ -202,9 +205,24 @@ export class TestingBehatDomUtilsService {
202205
elements.push(...this.findElementsBasedOnTextWithinWithExact(childNode, text, options));
203206
}
204207
}
208+
209+
// Allow searching text split into different elements in some cases.
210+
if (
211+
elements.length === 0 &&
212+
currentNode instanceof HTMLElement &&
213+
TestingBehatDomUtilsService.MULTI_ELEM_ALLOWED.includes(currentNode.tagName) &&
214+
currentNode.innerText.includes(text)
215+
) {
216+
// Only keep the child elements in the candidates list.
217+
fallbackCandidates = fallbackCandidates.filter(entry => !entry.element.contains(currentNode));
218+
fallbackCandidates.push({
219+
element: currentNode,
220+
exact: currentNode.innerText.trim() == text,
221+
});
222+
}
205223
}
206224

207-
return elements;
225+
return elements.length > 0 ? elements : fallbackCandidates;
208226
}
209227

210228
/**

0 commit comments

Comments
 (0)