diff --git a/src/testing/services/behat-runtime.ts b/src/testing/services/behat-runtime.ts index f032481e8..17edcdcdb 100644 --- a/src/testing/services/behat-runtime.ts +++ b/src/testing/services/behat-runtime.ts @@ -172,6 +172,33 @@ export class TestingBehatRuntimeService { const promises = coreLoadingsPromises.concat(ionLoadingsPromises); await Promise.all(promises); + + // Wait for ion-spinner to be removed from the DOM after loadings because loadings can contain spinners. + const ionSpinnerPromises: Promise[] = + Array.from(document.body.querySelectorAll('ion-spinner')) + .filter((element) => CoreDom.isElementVisible(element)) + .map((element) => + // Wait to the spinner to be removed from the DOM. + new Promise((resolve) => { + const parentElement = element.parentElement; + + if (!parentElement) { + resolve(); + + return; + } + + const observer = new MutationObserver(() => { + if (!parentElement.contains(element)) { + observer.disconnect(); + resolve(); + } + }); + + observer.observe(parentElement, { childList: true }); + })); + + await Promise.all(ionSpinnerPromises); }); }