diff --git a/maestro-client/src/main/java/maestro/drivers/WebDriver.kt b/maestro-client/src/main/java/maestro/drivers/WebDriver.kt index 9287483cc7..d994cb6c8d 100644 --- a/maestro-client/src/main/java/maestro/drivers/WebDriver.kt +++ b/maestro-client/src/main/java/maestro/drivers/WebDriver.kt @@ -171,8 +171,19 @@ class WebDriver(val isStudio: Boolean) : Driver { ensureOpen() // retrieve view hierarchy from DOM - val contentDesc = executeJS("return window.maestro.getContentDescription()") - ?: throw IllegalStateException("Could not retrieve hierarchy through maestro.getContentDescription()") + // There are edge cases where executeJS returns null, and we cannot get the hierarchy. In this situation + // we retry multiple times until throwing an error eventually. (See issue #1936) + var contentDesc: Any? = null + var retry = 0 + while (contentDesc == null) { + contentDesc = executeJS("return window.maestro.getContentDescription()") + if (contentDesc == null) { + retry++ + } + if (retry == RETRY_FETCHING_CONTENT_DESCRIPTION) { + throw IllegalStateException("Could not retrieve hierarchy through maestro.getContentDescription() (tried $retry times") + } + } // parse into TreeNodes fun parse(domRepresentation: Map): TreeNode { @@ -394,5 +405,6 @@ class WebDriver(val isStudio: Boolean) : Driver { companion object { private const val SCREENSHOT_DIFF_THRESHOLD = 0.005 + private const val RETRY_FETCHING_CONTENT_DESCRIPTION = 10 } }