与界面元素交互的方式主要有以下三种:
- 查找器 可让您选择一个或多个元素(或节点 语义树)进行断言或对其执行操作。
- 断言 :用于验证元素是否存在或者具有某些属性。
- 操作 :会在元素上注入模拟的用户事件,例如点击或其他手势。
其中一些 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 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 备忘单 中浏览完整列表。