Is there an API to wait for a composable to be dis...
# compose
z
Is there an API to wait for a composable to be displayed before taking action on it in an Espresso test? I run into flaky tests where a composable will exist, but when I do something like
performClick
on it, sometimes nothing will happen as it is not displayed yet. If I hack it to wait briefly before trying to click it, the click goes through successfully. It seems intentional that there is no
SemanticsMatcher
for
isDisplayed
but what is the alternative?
z
assertIsDisplayed
z
I’m aware of that one, but that will throw an exception if the node isn’t visible yet. I’m running into cases where I need manually wait before I can make that assertion, or perform an action like click
z
oh so you want something like
Copy code
rule.waitUntil { isDisplayed() }
z
yup!
Something like this works, I was just surprised that it doesn’t exist which made me wonder if we’re doing something wrong.
Copy code
fun ComposeTestRule.waitUntilDisplayed(
  matcher: SemanticsMatcher,
  timeoutMillis: Long = DEFAULT_WAIT_TIMEOUT
): SemanticsNodeInteraction {
  val deadline = System.currentTimeMillis() + timeoutMillis

  while (true) {
    try {
      return waitUntilExists(matcher).assertIsDisplayed()
    } catch (error: AssertionError) {
      if (System.currentTimeMillis() < deadline) {
        android.os.SystemClock.sleep(50)
      } else {
        throw error
      }
    }
  }
}
z
can you file a feature request for this?
i’ll propose exposing our internal
checkIsDisplayed
method, don’t see any reason why that couldn’t be public
z
yeah I’d be happy to, do I file it via the google bug tracker or is there somewhere else to file feature requests?
z
yep the google one, linked in the channel info
z
👍 thanks! btw I was at your talk on the new text APIs at droidcon sf and enjoyed it!
z
np, and thanks! hoping to ship a prototype of
OutputTransformation
soon 😅
🎉 4
also can you link your FR here please
z
z
🤯 talk about turnaround time!
Thanks a million!
z
you had good timing, I just finished another change 😛
😂 1
🥳 1
z
🫡
j
Nice addition. Until now I've been using
composeRule.waitUntilAtLeastOneExists(hasTestTag("x"))
which works, but doesn't mean displayed yet.
z
Wanna +1 that FR?
j
I do! Done.
gratitude thank you 1
v
My team also has also had custom implementation for this specific case for a long time, +1'd
gratitude thank you 1