测试 API

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

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

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

查找器

您可以使用 onNodeonAllNodes 选择一个或多个节点 但您也可以使用便捷查找器查找最常见的 搜索内容,例如onNodeWithTextonNodeWithContentDescription。您可以在以下位置浏览完整列表: Compose 测试备忘单

选择单个节点

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 的某些方面仍然有帮助 测试。