asserting-node-state-and-text

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Asserting Node State and Text — Verify Through the Framework, Not Through
fetchSemanticsNode

断言节点状态和文本——通过框架验证,而不是通过
fetchSemanticsNode

Once a finder resolves to a
SemanticsNodeInteraction
, the next step is asserting it. Compose ships a typed assertion for almost every semantic property; the generic
assert(matcher)
covers the rest. This skill picks the right assertion, distinguishes "exists" from "displayed", and shows when boolean predicates belong inside
waitUntil
.
finder 解析为
SemanticsNodeInteraction
后,下一步就是断言。Compose 为几乎所有 semantics 属性都提供了类型化断言;通用的
assert(matcher)
可覆盖其余情况。本 skill 会选择合适的断言,区分“存在于 semantics 树”与“已显示”,并说明何时应在
waitUntil
中使用布尔谓词。

When to use this skill

何时使用此 skill

  • The developer wants to verify a Switch is on, a Checkbox is unchecked, a Button is enabled, a Text equals an expected string.
  • The developer asks about
    assertIsDisplayed
    vs
    assertExists
    (one verifies on-screen, the other verifies presence in the tree).
  • The developer needs a custom assertion via
    assert(matcher)
    for a property without a typed extension.
  • The developer wants to verify a collection: "exactly 3 items", "at least one is selected", "all are enabled".
  • A test compares
    node.config[...]
    directly instead of using a typed assertion.
  • 开发者希望验证 Switch 已开启、Checkbox 未选中、Button 已启用,或 Text 等于预期字符串。
  • 开发者询问
    assertIsDisplayed
    与
    assertExists
    的区别:前者验证节点是否在屏幕上,后者验证节点是否存在于树中。
  • 开发者需要通过
    assert(matcher)
    为没有类型化扩展的属性编写自定义断言。
  • 开发者需要验证集合,例如“恰好有 3 个项目”“至少有一个被选中”或“全部已启用”。
  • 测试直接比较
    node.config[...]
    ,而没有使用类型化断言。

When NOT to use this skill

何时不应使用此 skill

  • The right node cannot be located — see
    ../../finders/finding-nodes-by-tag-text-content/SKILL.md
    .
  • The assertion is geometric (width, height, position) — see
    ./asserting-bounds-and-dimensions/SKILL.md
    .
  • The check needs a custom
    SemanticsMatcher
    — see
    ../../finders/composing-semantics-matchers/SKILL.md
    .
  • The state changes asynchronously and the assertion is timing-sensitive — see
    ../../synchronization/synchronizing-with-idle/SKILL.md
    .
  • 无法定位目标节点——参见
    ../../finders/finding-nodes-by-tag-text-content/SKILL.md
    。
  • 断言涉及几何属性(宽度、高度、位置)——参见
    ./asserting-bounds-and-dimensions/SKILL.md
    。
  • 检查需要自定义
    SemanticsMatcher
    ——参见
    ../../finders/composing-semantics-matchers/SKILL.md
    。
  • 状态异步变化且断言对时序敏感——参见
    ../../synchronization/synchronizing-with-idle/SKILL.md
    。

Prerequisites

前置条件

  • A working
    ComposeTestRule
    /
    ComposeUiTest
    . See
    ../../setup/configuring-test-dependencies/SKILL.md
    .
  • The node has been located through
    onNode*
    /
    onAllNodes*
    / a navigator.
  • For
    assertIsDisplayed
    semantics, the node must be composed AND placed AND at least partially on-screen post-clip (
    Assertions.kt:30-39
    ).
  • 可正常工作的
    ComposeTestRule
    /
    ComposeUiTest
    。参见
    ../../setup/configuring-test-dependencies/SKILL.md
    。
  • 已通过
    onNode*
    /
    onAllNodes*
    / navigator 定位到目标节点。
  • 对于
    assertIsDisplayed
    的语义,节点必须已组合、已放置,并且在 post-clip 后至少部分位于屏幕内(
    Assertions.kt:30-39
    )。

Workflow

工作流

  • 1. Pick by question type. Map the developer's question to one assertion call. Every API listed is in
    commonMain/.../Assertions.kt
    unless noted.
    QuestionAPIFile:line
    Is it in the tree?
    assertExists(errorMessageOnFail)
    SemanticsNodeInteraction.kt:124-127
    Is it gone?
    assertDoesNotExist()
    SemanticsNodeInteraction.kt:94-110
    Is it a SubcomposeLayout retained child?
    assertIsDeactivated()
    SemanticsNodeInteraction.kt:137-148
    Is it visible to the user?
    assertIsDisplayed()
    /
    assertIsNotDisplayed()
    Assertions.kt:32-53
    Is it interactive?
    assertIsEnabled()
    /
    assertIsNotEnabled()
    Assertions.kt:60-67
    Is the toggle on?
    assertIsOn()
    /
    assertIsOff()
    /
    assertIsToggleable()
    Assertions.kt:74-81, 103
    Is the radio/tab selected?
    assertIsSelected()
    /
    assertIsNotSelected()
    /
    assertIsSelectable()
    Assertions.kt:88-110
    Does it have focus?
    assertIsFocused()
    /
    assertIsNotFocused()
    Assertions.kt:117-124
    Does it equal these strings?
    assertTextEquals(vararg, includeEditableText=true)
    Assertions.kt:181-185
    Does it contain this string?
    assertTextContains(value, substring=false, ignoreCase=false)
    Assertions.kt:204-208
    Content description equals these?
    assertContentDescriptionEquals(vararg)
    Assertions.kt:139-141
    Content description contains this?
    assertContentDescriptionContains(value, substring, ignoreCase)
    Assertions.kt:158-163
    StateDescription equals X?
    assertValueEquals(value)
    Assertions.kt:216-217
    ProgressBar at this position?
    assertRangeInfoEquals(rangeInfo)
    Assertions.kt:225-227
    Has a click action?
    assertHasClickAction()
    /
    assertHasNoClickAction()
    Assertions.kt:234-243
    Anything else
    assert(matcher, messagePrefixOnError = null)
    Assertions.kt:254-267
  • 2.
    assertIsDisplayed
    vs
    assertExists
    — pick deliberately.
    A node
    assertExists()
    if it is in the semantics tree (composed). A node
    assertIsDisplayed()
    if it is composed AND placed AND at least partially visible after clipping (
    Assertions.kt:30-39
    ). For LazyColumn items not yet scrolled into view,
    assertExists()
    may fail (item not composed) or succeed (item composed but off-screen — depends on prefetch);
    assertIsDisplayed()
    is the right gate for "the user sees this".
  • 3. Use the typed assertion, not
    fetchSemanticsNode().config[...]
    .
    Typed assertions delegate to the matcher library and produce framework-formatted errors that name the failed property and dump the node (
    Assertions.kt:254-267
    — the generic
    assert
    builds
    buildGeneralErrorMessage(errorMessageOnFail, selector, node)
    ). Direct config reads bypass that error reporting and miss the auto-attached node dump.
  • 4. For collections, use the collection-typed assertions.
    assertCountEquals(expectedSize)
    for cardinality (
    Assertions.kt:276-292
    ),
    assertAny(matcher)
    for "at least one matches" (fails on empty —
    Assertions.kt:300-312
    ),
    assertAll(matcher)
    for "every node matches" (passes on empty —
    Assertions.kt:323-339
    ).
  • 5. For
    waitUntil
    predicates, use boolean APIs, not throwing assertions.
    isDisplayed()
    returns
    Boolean
    (
    Assertions.kt:351
    ) — perfect for
    rule.waitUntil { node.isDisplayed() }
    .
    isNotDisplayed()
    is its inverse (
    Assertions.kt:361
    ). Both throw if multiple nodes match the finder, but never on zero. Cross-reference:
    ../../synchronization/synchronizing-with-idle/SKILL.md
    .
  • 6. For one-off properties without a typed assertion, use
    assert(matcher)
    .
    Combine with prebuilt or composed matchers from
    ../../finders/composing-semantics-matchers/SKILL.md
    . The
    messagePrefixOnError
    lambda is for adding context when this assert is a precondition for a larger operation.
  • 1. 按问题类型选择。 将开发者的问题映射到一次断言调用。除非另有说明,下列 API 均位于
    commonMain/.../Assertions.kt
    。
    问题API文件:行
    它是否在树中?
    assertExists(errorMessageOnFail)
    SemanticsNodeInteraction.kt:124-127
    它是否已消失?
    assertDoesNotExist()
    SemanticsNodeInteraction.kt:94-110
    它是否为 SubcomposeLayout 保留的子项?
    assertIsDeactivated()
    SemanticsNodeInteraction.kt:137-148
    用户是否可以看到它?
    assertIsDisplayed()
    /
    assertIsNotDisplayed()
    Assertions.kt:32-53
    它是否可以交互?
    assertIsEnabled()
    /
    assertIsNotEnabled()
    Assertions.kt:60-67
    开关是否已开启?
    assertIsOn()
    /
    assertIsOff()
    /
    assertIsToggleable()
    Assertions.kt:74-81, 103
    单选按钮或标签页是否已选中?
    assertIsSelected()
    /
    assertIsNotSelected()
    /
    assertIsSelectable()
    Assertions.kt:88-110
    它是否拥有焦点?
    assertIsFocused()
    /
    assertIsNotFocused()
    Assertions.kt:117-124
    它是否等于这些字符串?
    assertTextEquals(vararg, includeEditableText=true)
    Assertions.kt:181-185
    它是否包含这个字符串?
    assertTextContains(value, substring=false, ignoreCase=false)
    Assertions.kt:204-208
    内容描述是否等于这些值?
    assertContentDescriptionEquals(vararg)
    Assertions.kt:139-141
    内容描述是否包含这个值?
    assertContentDescriptionContains(value, substring, ignoreCase)
    Assertions.kt:158-163
    StateDescription 是否等于 X?
    assertValueEquals(value)
    Assertions.kt:216-217
    ProgressBar 是否处于这个位置?
    assertRangeInfoEquals(rangeInfo)
    Assertions.kt:225-227
    它是否具有点击操作?
    assertHasClickAction()
    /
    assertHasNoClickAction()
    Assertions.kt:234-243
    其他情况
    assert(matcher, messagePrefixOnError = null)
    Assertions.kt:254-267
  • 2.
    assertIsDisplayed
    与
    assertExists
    ——有意识地选择。
    如果契约只是要求节点存在于 semantics 树中(已组合),使用
    assertExists()
    。如果要求节点已组合、已放置,并且裁剪后至少部分可见,使用
    assertIsDisplayed()
    。对于尚未滚动到视野中的 LazyColumn 项目,
    assertExists()
    可能失败(项目尚未组合),也可能成功(项目已组合但位于屏幕外——取决于预取行为);如果要验证“用户可以看到它”,
    assertIsDisplayed()
    才是正确的判断方式。
  • 3. 使用类型化断言,不要使用
    fetchSemanticsNode().config[...]
    。
    类型化断言会委托给 matcher 库,并生成由框架格式化的错误信息,指出失败的属性并转储节点(
    Assertions.kt:254-267
    ——通用
    assert
    会构建
    buildGeneralErrorMessage(errorMessageOnFail, selector, node)
    )。直接读取 config 会绕过错误报告,并遗漏自动附加的节点转储。
  • 4. 对于集合,使用面向集合的类型化断言。 使用
    assertCountEquals(expectedSize)
    检查基数(
    Assertions.kt:276-292
    );使用
    assertAny(matcher)
    表达“至少有一个匹配”(空集合时失败——
    Assertions.kt:300-312
    );使用
    assertAll(matcher)
    表达“每个节点都匹配”(空集合时通过——
    Assertions.kt:323-339
    )。
  • 5. 对于
    waitUntil
    谓词,使用返回 Boolean 的 API,而不是会抛出异常的断言。
    isDisplayed()
    返回
    Boolean
    (
    Assertions.kt:351
    ),非常适合
    rule.waitUntil { node.isDisplayed() }
    。
    isNotDisplayed()
    与其相反(
    Assertions.kt:361
    )。当 finder 匹配到多个节点时,两者都会抛出异常,但节点数为零时不会抛出。交叉参考:
    ../../synchronization/synchronizing-with-idle/SKILL.md
    。
  • 6. 对于没有类型化断言的一次性属性,使用
    assert(matcher)
    。
    可以将其与
    ../../finders/composing-semantics-matchers/SKILL.md
    中的预构建或组合 matcher 配合使用。当该断言是较大操作的前置条件时,可以使用
    messagePrefixOnError
    lambda 补充上下文。

Patterns

模式

Pattern:
assertIsDisplayed
over
assertExists
for user-facing checks

模式:面向用户的检查使用
assertIsDisplayed
,而不是
assertExists

kotlin
// WRONG
@Test
fun submit_isVisibleAfterError() {
    rule.setContent { CheckoutScreen(state = state) }
    rule.onNodeWithTag(SubmitTag).assertExists()
}
// WRONG because: assertExists passes for nodes that are composed but off-screen, behind a
// dialog, or measured to zero size. The test does not prove the user actually sees the button.
kotlin
// RIGHT
@Test
fun submit_isVisibleAfterError() {
    rule.setContent { CheckoutScreen(state = state) }
    rule.onNodeWithTag(SubmitTag).assertIsDisplayed()
}
kotlin
// WRONG
@Test
fun submit_isVisibleAfterError() {
    rule.setContent { CheckoutScreen(state = state) }
    rule.onNodeWithTag(SubmitTag).assertExists()
}
// WRONG because: assertExists passes for nodes that are composed but off-screen, behind a
// dialog, or measured to zero size. The test does not prove the user actually sees the button.
kotlin
// RIGHT
@Test
fun submit_isVisibleAfterError() {
    rule.setContent { CheckoutScreen(state = state) }
    rule.onNodeWithTag(SubmitTag).assertIsDisplayed()
}

Pattern: typed
assertIsOn
over manual config reads

模式:使用类型化
assertIsOn
,而不是手动读取 config

kotlin
// WRONG
@Test
fun darkMode_switch_isOn() {
    rule.setContent { SettingsScreen() }
    val node = rule.onNodeWithTag(DarkModeSwitchTag).fetchSemanticsNode()
    assertEquals(ToggleableState.On, node.config[SemanticsProperties.ToggleableState])
}
// WRONG because: bypasses the framework's error reporting. Failure prints a JUnit assertEquals
// diff with no node dump, no selector description, no hint about which screen the failure
// happened on.
kotlin
// RIGHT
@Test
fun darkMode_switch_isOn() {
    rule.setContent { SettingsScreen() }
    rule.onNodeWithTag(DarkModeSwitchTag).assertIsOn()
}
assertIsOn
is
assert(isOn())
(
Assertions.kt:74
);
isOn()
is
expectValue(SemanticsProperties.ToggleableState, ToggleableState.On)
(
Filters.kt:61-62
). The error names the property and dumps the node automatically.
kotlin
// WRONG
@Test
fun darkMode_switch_isOn() {
    rule.setContent { SettingsScreen() }
    val node = rule.onNodeWithTag(DarkModeSwitchTag).fetchSemanticsNode()
    assertEquals(ToggleableState.On, node.config[SemanticsProperties.ToggleableState])
}
// WRONG because: bypasses the framework's error reporting. Failure prints a JUnit assertEquals
// diff with no node dump, no selector description, no hint about which screen the failure
// happened on.
kotlin
// RIGHT
@Test
fun darkMode_switch_isOn() {
    rule.setContent { SettingsScreen() }
    rule.onNodeWithTag(DarkModeSwitchTag).assertIsOn()
}
assertIsOn
是
assert(isOn())
(
Assertions.kt:74
);
isOn()
是
expectValue(SemanticsProperties.ToggleableState, ToggleableState.On)
(
Filters.kt:61-62
)。错误信息会指出属性,并自动转储节点。

Pattern:
assertTextEquals
(vararg) handles merged Text + EditableText

模式:
assertTextEquals
(vararg)可处理合并后的 Text + EditableText

kotlin
// production:
TextField(value = "hello", onValueChange = {}, label = { Text("Name") },
    modifier = Modifier.testTag(NameFieldTag))

// test:
rule.onNodeWithTag(NameFieldTag).assertTextEquals("Name", "hello")
assertTextEquals(vararg)
matches the unordered set of
SemanticsProperties.Text
plus, by default,
SemanticsProperties.EditableText
(
Assertions.kt:181-185
→
Filters.kt:274-293
). To exclude editable text from the comparison:
assertTextEquals("Name", includeEditableText = false)
.
kotlin
// production:
TextField(value = "hello", onValueChange = {}, label = { Text("Name") },
    modifier = Modifier.testTag(NameFieldTag))

// test:
rule.onNodeWithTag(NameFieldTag).assertTextEquals("Name", "hello")
assertTextEquals(vararg)
会匹配
SemanticsProperties.Text
的无序集合,并且默认还会匹配
SemanticsProperties.EditableText
(
Assertions.kt:181-185
→
Filters.kt:274-293
)。如需从比较中排除可编辑文本,请使用
assertTextEquals("Name", includeEditableText = false)
。

Pattern:
assertCountEquals
instead of
onAllNodes(...).onFirst().assertExists()

模式:使用
assertCountEquals
,而不是
onAllNodes(...).onFirst().assertExists()

kotlin
// WRONG
rule.onAllNodesWithTag(ItemTag).onFirst().assertExists()
// WRONG because: only proves there is at least one item. Drift to zero items causes
// "no node matched" instead of a count error; drift to many is silently accepted.
kotlin
// RIGHT
rule.onAllNodesWithTag(ItemTag).assertCountEquals(3)
kotlin
// WRONG
rule.onAllNodesWithTag(ItemTag).onFirst().assertExists()
// WRONG because: only proves there is at least one item. Drift to zero items causes
// "no node matched" instead of a count error; drift to many is silently accepted.
kotlin
// RIGHT
rule.onAllNodesWithTag(ItemTag).assertCountEquals(3)

Pattern:
assert(matcher)
for properties without a typed assertion

模式:对没有类型化断言的属性使用
assert(matcher)

kotlin
@Test
fun row_hasPriorityOne() {
    rule.setContent { TaskList(tasks = tasks) }

    rule.onNodeWithTag(TaskRowTag)
        .assert(SemanticsMatcher.expectValue(PriorityKey, 1))
}
PriorityKey
is a custom
SemanticsPropertyKey<Int>
set via
Modifier.semantics { priority = … }
. See
../../finders/composing-semantics-matchers/SKILL.md
for matcher composition.
kotlin
@Test
fun row_hasPriorityOne() {
    rule.setContent { TaskList(tasks = tasks) }

    rule.onNodeWithTag(TaskRowTag)
        .assert(SemanticsMatcher.expectValue(PriorityKey, 1))
}
PriorityKey
是通过
Modifier.semantics { priority = … }
设置的自定义
SemanticsPropertyKey<Int>
。有关 matcher 组合方式,请参见
../../finders/composing-semantics-matchers/SKILL.md
。

Pattern:
assertAny
(fails on empty) vs
assertAll
(passes on empty)

模式:
assertAny
(空集合时失败)与
assertAll
(空集合时通过)

kotlin
// "at least one row is selected" — must have at least one row
rule.onAllNodesWithTag(RowTag).assertAny(isSelected())

// "every row is enabled" — accepts zero rows (passes vacuously)
rule.onAllNodesWithTag(RowTag).assertAll(isEnabled())
assertAny
throws
AssertionError("Failed to assertAny … no node matched")
on an empty collection (
Assertions.kt:305-307
).
assertAll
returns successfully on an empty collection (
Assertions.kt:323-339
). Pick deliberately.
kotlin
// "at least one row is selected" — must have at least one row
rule.onAllNodesWithTag(RowTag).assertAny(isSelected())

// "every row is enabled" — accepts zero rows (passes vacuously)
rule.onAllNodesWithTag(RowTag).assertAll(isEnabled())
assertAny
在空集合上抛出
AssertionError("Failed to assertAny … no node matched")
(
Assertions.kt:305-307
)。
assertAll
在空集合上会成功返回(
Assertions.kt:323-339
)。应根据预期语义谨慎选择。

Pattern:
isDisplayed()
inside
waitUntil

模式:在
waitUntil
中使用
isDisplayed()

kotlin
// WRONG
rule.waitUntil { rule.onNodeWithTag(SnackbarTag).assertIsDisplayed(); true }
// WRONG because: assertIsDisplayed throws on the first poll where the snackbar isn't yet
// shown. waitUntil expects a boolean — predicates that throw will fail-fast.
kotlin
// RIGHT
rule.waitUntil(timeoutMillis = 2_000) {
    rule.onAllNodesWithTag(SnackbarTag).fetchSemanticsNodes().isNotEmpty() &&
        rule.onNodeWithTag(SnackbarTag).isDisplayed()
}
rule.onNodeWithTag(SnackbarTag).assertTextContains("Saved")
isDisplayed()
returns
false
when zero nodes match and only throws on multiple matches (
Assertions.kt:343-351
). Cross-reference:
../../synchronization/synchronizing-with-idle/SKILL.md
for
waitUntil
vs
mainClock.advanceTimeUntil
.
kotlin
// WRONG
rule.waitUntil { rule.onNodeWithTag(SnackbarTag).assertIsDisplayed(); true }
// WRONG because: assertIsDisplayed throws on the first poll where the snackbar isn't yet
// shown. waitUntil expects a boolean — predicates that throw will fail-fast.
kotlin
// RIGHT
rule.waitUntil(timeoutMillis = 2_000) {
    rule.onAllNodesWithTag(SnackbarTag).fetchSemanticsNodes().isNotEmpty() &&
        rule.onNodeWithTag(SnackbarTag).isDisplayed()
}
rule.onNodeWithTag(SnackbarTag).assertTextContains("Saved")
当没有节点匹配时,
isDisplayed()
返回
false
;只有匹配到多个节点时才会抛出异常(
Assertions.kt:343-351
)。有关
waitUntil
与
mainClock.advanceTimeUntil
的区别,请交叉参考
../../synchronization/synchronizing-with-idle/SKILL.md
。

Pattern:
assertIsDeactivated
for SubcomposeLayout retained children

模式:使用
assertIsDeactivated
验证 SubcomposeLayout 保留的子项

kotlin
// LookaheadLayout / SubcomposeLayout may retain a previously composed slot for reuse.
// To verify the slot is currently deactivated (kept, not active):
rule.onNodeWithTag(RetainedSlotTag).assertIsDeactivated()
assertIsDeactivated
fetches the node without skipping deactivated ones (
SemanticsNodeInteraction.kt:137-148
) and checks
node.layoutInfo.isDeactivated
.
kotlin
// LookaheadLayout / SubcomposeLayout may retain a previously composed slot for reuse.
// To verify the slot is currently deactivated (kept, not active):
rule.onNodeWithTag(RetainedSlotTag).assertIsDeactivated()
assertIsDeactivated
获取节点时不会跳过已停用的节点(
SemanticsNodeInteraction.kt:137-148
),并检查
node.layoutInfo.isDeactivated
。

Mandatory rules

强制规则

  • MUST use the typed assertion (
    assertIsOn
    ,
    assertIsEnabled
    ,
    assertTextEquals
    , …) over
    fetchSemanticsNode().config[...]
    . The framework's error message includes the selector, the node dump, and the failed clause.
  • MUST use
    assertIsDisplayed()
    when the contract is "the user sees this"; MUST use
    assertExists()
    only when the contract is "this is in the semantics tree" (e.g. checking presence in unmerged tree without on-screen requirement).
  • MUST use the boolean
    isDisplayed()
    /
    isNotDisplayed()
    inside
    waitUntil { … }
    blocks; MUST NOT call throwing
    assertIs*
    from inside
    waitUntil
    .
  • MUST use
    assertCountEquals
    for collection cardinality; MUST NOT index
    [0]
    to imply "the only one".
  • MUST NOT repeat the same assertion across chained calls when one composed matcher would do — see
    ../../finders/composing-semantics-matchers/SKILL.md
    .
  • PREFERRED: prefer tag-anchored lookups before assertions. Skydoves hot take #1.
  • 必须使用类型化断言(
    assertIsOn
    、
    assertIsEnabled
    、
    assertTextEquals
    等),而不是
    fetchSemanticsNode().config[...]
    。框架的错误信息包含 selector、节点转储和失败的断言条件。
  • 当契约要求“用户可以看到它”时,必须使用
    assertIsDisplayed()
    ;只有当契约要求“它存在于 semantics 树中”时,才使用
    assertExists()
    (例如检查未合并树中的存在性,而不考虑是否位于屏幕上)。
  • 在
    waitUntil { … }
    块中,必须使用布尔型
    isDisplayed()
    /
    isNotDisplayed()
    ;不得在
    waitUntil
    中调用会抛出异常的
    assertIs*
    。
  • 必须使用
    assertCountEquals
    检查集合基数;不得通过索引
    [0]
    暗示“只有这一个”。
  • 当一个组合后的 matcher 可以完成检查时,不得在链式调用中重复相同断言——参见
    ../../finders/composing-semantics-matchers/SKILL.md
    。
  • **优先:**在断言之前使用以 tag 为锚点的查找。Skydoves 热门观点 #1。

Verification

验证

  • No assertion reads
    node.config[...]
    directly. All property checks go through
    assertIs*
    /
    assertTextEquals
    /
    assert(matcher)
    .
  • assertIsDisplayed
    is used for user-visible checks;
    assertExists
    only when on-screen visibility is irrelevant.
  • waitUntil { … }
    blocks use
    isDisplayed()
    /
    isNotDisplayed()
    /
    fetchSemanticsNodes().size
    — no throwing
    assertIs*
    calls inside.
  • assertAny
    /
    assertAll
    choice matches the intended empty-collection semantics.
  • ./gradlew :app:connectedDebugAndroidTest
    (device) or
    :app:testDebugUnitTest
    (host) passes.
  • Failure messages mention the offending semantic property by name (proof the typed assertion is in use).
  • 没有断言直接读取
    node.config[...]
    。所有属性检查都通过
    assertIs*
    /
    assertTextEquals
    /
    assert(matcher)
    完成。
  • 用户可见性检查使用
    assertIsDisplayed
    ;只有不关心是否位于屏幕上时才使用
    assertExists
    。
  • waitUntil { … }
    块使用
    isDisplayed()
    /
    isNotDisplayed()
    /
    fetchSemanticsNodes().size
    ,不包含会抛出异常的
    assertIs*
    调用。
  • assertAny
    /
    assertAll
    的选择符合预期的空集合语义。
  • ./gradlew :app:connectedDebugAndroidTest
    (设备)或
    :app:testDebugUnitTest
    (主机)通过。
  • 错误信息会指出发生问题的 semantics 属性名称,证明使用的是类型化断言。

References

参考资料