與 UI 元素互動的方式主要有三種:
- 「Finder」可讓您選取一或多個元素 (或語意樹狀結構中的「節點」) 做為斷言或執行動作。
- 宣告是用來驗證元素是否存在或具有特定屬性。
- 動作會在元素上插入模擬使用者事件,例如點擊或其他手勢。
其中部分 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:雖然適用於以 View 為基礎的 UI,但 Espresso 知識在 Compose 測試的某些方面仍非常實用。