測試 API

與 UI 元素互動的方式主要有三種:

  • 「Finder」可讓您選取一或多個元素 (或語意樹狀結構中的「節點」) 來進行宣告或執行操作。
  • 宣告是用來驗證元素是否存在或具有特定屬性。
  • 動作會在元素上插入模擬使用者事件,例如點擊或其他手勢。

其中部分 API 接受 SemanticsMatcher 來參照語意樹狀結構中的一或多個節點

搜尋器

您可以分別使用 onNodeonAllNodes 選取一個或多個節點,也可以使用方便的搜尋工具進行一般的搜尋,例如 onNodeWithTextonNodeWithContentDescription。您可以在 Compose Testing 一覽表中瀏覽完整清單。

請選取單一節點

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 測試一覽表中瀏覽完整清單。

你也可以使用函式來檢查對一組節點的宣告:

// 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 一覽表中瀏覽完整清單。

比對器

您可以使用各種比對器測試撰寫程式碼。

階層比對器

階層比對器可讓您調整語意樹狀結構,並執行比對作業。

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 為基礎的 UI 設計,但您仍可運用相關知識,進行部分 Compose 測試。