与界面元素交互主要有三种方式
- 查找器可让您选择一个或多个元素(或语义树中的节点),以便对其进行断言或执行操作。
- 断言用于验证元素是否存在或是否具有特定属性。
- 操作在元素上注入模拟的用户事件,例如点击或其他手势。
其中一些 API 接受一个 SemanticsMatcher
,以引用语义树中的一个或多个节点。
查找器
您可以使用 onNode
和 onAllNodes
分别选择一个或多个节点,但您也可以使用便捷查找器进行最常见的搜索,例如 onNodeWithText
和 onNodeWithContentDescription
。您可以在 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"))
您还可以使用便捷函数进行最常见的断言,例如 assertExists
、assertIsDisplayed
和 assertTextEquals
。您可以在 Compose 测试备忘单中浏览完整列表。
还有一些函数用于检查节点集合上的断言
// 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 测试备忘单中浏览完整列表。
匹配器
有多种匹配器可用于测试您的 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 测试备忘单中浏览完整列表。
其他资源
- 在 Android 上测试应用:Android 测试主着陆页提供了测试基础知识和技术的更广泛视图。
- 测试基础知识:详细了解测试 Android 应用背后的核心概念。
- 本地测试:您可以在自己的工作站上本地运行一些测试。
- 插桩测试:运行插桩测试也是一种好做法。也就是说,直接在设备上运行的测试。
- 持续集成:持续集成可让您将测试集成到部署流水线中。
- 测试不同屏幕尺寸:由于用户可用的设备众多,您应该测试不同的屏幕尺寸。
- Espresso:虽然 Espresso 旨在用于基于视图的界面,但其知识对于 Compose 测试的某些方面仍然有帮助。