测试 API

与界面元素交互的方式主要有以下三种:

  • 借助查找器,您可以选择一个或多个元素(或语义树中的节点),以进行断言或对其执行操作。
  • 断言:用于验证元素是否存在或者具有某些属性。
  • 操作:会在元素上注入模拟的用户事件,例如点击或其他手势。

其中一些 API 接受 SemanticsMatcher 来引用语义树中的一个或多个节点。

查找器

您可以使用 onNodeonAllNodes 分别选择一个或多个节点,但也可以使用便捷查找器来执行最常见的搜索,例如 onNodeWithTextonNodeWithContentDescription。您可以在 Compose Testing 备忘单中浏览完整列表。

选择单个节点

composeTestRule.onNode(<<SemanticsMatcher>>, useUnmergedTree = false): SemanticsNodeInteraction
// Example
composeTestRule
    .onNode(hasText("Button")) // Equivalent to onNodeWithText("Button")

选择多个节点

composeTestRule
    .onAllNodes(<<SemanticsMatcher>>): SemanticsNodeInteractionCollection
// Example
composeTestRule
    .onAllNodes(hasText("Button")) // Equivalent to onAllNodesWithText("Button")

未合并的树

某些节点会合并其子项的语义信息。例如,如果某个按钮具有两个文本元素,则会合并这两个文本元素标签:

MyButton {
    Text("Hello")
    Text("World")
}

在测试中,使用 printToLog() 显示语义树:

composeTestRule.onRoot().printToLog("TAG")

此代码会生成以下输出:

Node #1 at (...)px
 |-Node #2 at (...)px
   Role = 'Button'
   Text = '[Hello, World]'
   Actions = [OnClick, GetTextLayoutResult]
   MergeDescendants = 'true'

如果您需要匹配未合并的树的节点,可以将 useUnmergedTree 设为 true

composeTestRule.onRoot(useUnmergedTree = true).printToLog("TAG")

此代码会生成以下输出:

Node #1 at (...)px
 |-Node #2 at (...)px
   OnClick = '...'
   MergeDescendants = 'true'
    |-Node #3 at (...)px
    | Text = '[Hello]'
    |-Node #5 at (83.0, 86.0, 191.0, 135.0)px
      Text = '[World]'

useUnmergedTree 参数在所有查找器中都可用。例如,此处用于 onNodeWithText 查找器。

composeTestRule
    .onNodeWithText("World", useUnmergedTree = true).assertIsDisplayed()

断言

通过对具有一个或多个匹配器的查找器返回的 SemanticsNodeInteraction 调用 assert(),可以检查断言:

// Single matcher:
composeTestRule
    .onNode(matcher)
    .assert(hasText("Button")) // hasText is a SemanticsMatcher

// Multiple matchers can use and / or
composeTestRule
    .onNode(matcher).assert(hasText("Button") or hasText("Button2"))

您还可以将便捷函数用于最常见的断言,例如 assertExistsassertIsDisplayedassertTextEquals。您可以在 Compose Testing 备忘单中浏览完整列表。

还有一些函数用于检查一系列节点上的断言:

// Check number of matched nodes
composeTestRule
    .onAllNodesWithContentDescription("Beatle").assertCountEquals(4)
// At least one matches
composeTestRule
    .onAllNodesWithContentDescription("Beatle").assertAny(hasTestTag("Drummer"))
// All of them match
composeTestRule
    .onAllNodesWithContentDescription("Beatle").assertAll(hasClickAction())

操作

如需在节点上注入操作,请调用 perform…() 函数:

composeTestRule.onNode(...).performClick()

下面是一些操作示例:

performClick(),
performSemanticsAction(key),
performKeyPress(keyEvent),
performGesture { swipeLeft() }

您可以在 Compose Testing 备忘单中浏览完整列表。

匹配器

有多种匹配器可用于测试 Compose 代码。

分层匹配器

分层匹配器允许您在语义树中向上或向下移动并执行匹配。

fun hasParent(matcher: SemanticsMatcher): SemanticsMatcher
fun hasAnySibling(matcher: SemanticsMatcher): SemanticsMatcher
fun hasAnyAncestor(matcher: SemanticsMatcher): SemanticsMatcher
fun hasAnyDescendant(matcher: SemanticsMatcher):  SemanticsMatcher

下面是一些使用这些匹配器的示例:

composeTestRule.onNode(hasParent(hasText("Button")))
    .assertIsDisplayed()

选择器

创建测试的另一种方法是使用选择器,这样可提高一些测试的可读性。

composeTestRule.onNode(hasTestTag("Players"))
    .onChildren()
    .filter(hasClickAction())
    .assertCountEquals(4)
    .onFirst()
    .assert(hasText("John"))

您可以在 Compose Testing 备忘单中浏览完整列表。

其他资源

  • 在 Android 平台上测试应用:Android 测试主着陆页会为您提供关于测试基础知识和技术的更全面的视图。
  • 测试基础知识:详细了解测试 Android 应用背后的核心概念。
  • 本地测试:您可以在自己的工作站上本地运行一些测试。
  • 插桩测试:最好同时运行插桩测试。也就是说,直接在设备上运行的测试。
  • 持续集成:借助持续集成,您可以将测试集成到部署流水线中。
  • 测试不同的屏幕尺寸:由于用户可使用许多设备,因此您应该针对不同的屏幕尺寸进行测试。
  • Espresso:虽然适用于基于 View 的界面,但掌握 Espresso 对 Compose 测试的某些方面仍然很有帮助。