You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Kotlin Compose UI测试:如何查找ProgressBarRangeInfo并等待其消失?

Jetpack Compose UI测试:等待CircularProgressIndicator消失的通用方案

问题背景

需要检查CircularProgressIndicator是否可见,并等待视图树中不再存在带有ProgressBarRangeInfo的节点。尝试使用app.waitUntilExactlyOneExists(hasContentDescription("ProgressBarRangeInfo"))时程序崩溃,且不想为大量该组件手动设置contentDescription,希望找到通用的按属性/类型查找节点的方法。

当前视图树信息:

Printing with useUnmergedTree = 'false'
Node #1 at (l=0.0, t=63.0, r=1080.0, b=1857.0)px
 |-Node #2 at (l=0.0, t=63.0, r=1080.0, b=1857.0)px
   IsContainer = 'true'
    |-Node #31 at (l=488.0, t=908.0, r=593.0, b=1013.0)px
      ProgressBarRangeInfo = 'ProgressBarRangeInfo(current=0.0, range=0.0..0.0, steps=0)'
      MergeDescendants = 'true'

解决方案

1. 自定义语义Matcher匹配ProgressBarRangeInfo

默认的CircularProgressIndicator会自动暴露ProgressBarRangeInfo语义属性,无需手动设置contentDescription。可以自定义Matcher直接检查该属性:

import androidx.compose.ui.semantics.ProgressBarRangeInfo
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.test.SemanticsMatcher

fun hasProgressBarRangeInfo() = SemanticsMatcher(
    "Has ProgressBarRangeInfo"
) { node ->
    node.config.contains(SemanticsProperties.ProgressBarRangeInfo)
}

2. 等待进度条节点消失

使用自定义的Matcher,调用waitUntilDoesNotExist来等待视图树中不再存在该类型节点:

app.waitUntilDoesNotExist(hasProgressBarRangeInfo())

扩展:匹配特定进度值

如果需要检查具体的进度状态,可以扩展Matcher匹配特定的current值:

fun hasProgressBarCurrentValue(current: Float) = SemanticsMatcher(
    "ProgressBar current value is $current"
) { node ->
    node.config[SemanticsProperties.ProgressBarRangeInfo]?.current == current
}

原方案崩溃原因

默认CircularProgressIndicator未设置contentDescription,hasContentDescription("ProgressBarRangeInfo")无法匹配到任何节点,导致waitUntilExactlyOneExists因超时或匹配失败抛出异常。基于语义属性的Matcher直接利用组件默认暴露的信息,更可靠通用。

内容的提问来源于stack exchange,提问作者J. Doe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 19:43:13