与界面元素互动的方式主要有以下三种:
- 查找器:可供您选择一个或多个元素(或语义树中的节点),以进行断言或对其执行操作。
- 断言:用于验证元素是否存在或者具有某些属性。
- 操作:会在元素上注入模拟的用户事件,例如点击或其他手势。
其中一些 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 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 测试备忘单中浏览完整列表。
其他资源
- 在 Android 平台上测试应用:此 Android 测试主着陆页提供了更广泛的测试基础知识和技巧。
- 测试基础知识:详细了解 Android 应用测试背后的核心概念。
- 本地测试:您可以在自己的工作站上本地运行一些测试。
- 插桩测试:最好也运行插桩测试。也就是说,直接在设备上运行的测试。
- 持续集成:借助持续集成,您可以将测试集成到部署流水线中。
- 测试不同的屏幕尺寸:由于用户可使用多种设备,因此您应测试不同的屏幕尺寸。
- Espresso:虽然 Espresso 旨在用于基于 View 的界面,但其知识仍有助于进行 Compose 测试的某些方面。