Skip to content

Commit

Permalink
WebDriver: Retry fetching content description (#1939)
Browse files Browse the repository at this point in the history
  • Loading branch information
pocmo authored Sep 2, 2024
1 parent 9cd39a6 commit 8436d5e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions maestro-client/src/main/java/maestro/drivers/WebDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any>): TreeNode {
Expand Down Expand Up @@ -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
}
}

0 comments on commit 8436d5e

Please sign in to comment.