การทดสอบ API

คุณโต้ตอบกับองค์ประกอบ UI ได้ 3 วิธีหลักๆ ดังนี้

  • ตัวค้นหา ช่วยให้คุณเลือกองค์ประกอบอย่างน้อย 1 รายการ (หรือ โหนด ในแผนผังความหมาย) เพื่อทำการยืนยันหรือดำเนินการกับองค์ประกอบเหล่านั้น
  • การยืนยัน ใช้เพื่อตรวจสอบว่าองค์ประกอบมีอยู่หรือมีแอตทริบิวต์บางอย่าง
  • การดำเนินการ จะแทรกเหตุการณ์ของผู้ใช้ที่จำลองขึ้นในองค์ประกอบ เช่น การคลิกหรือท่าทางสัมผัสอื่นๆ

API บางรายการเหล่านี้ยอมรับ SemanticsMatcher เพื่ออ้างอิง โหนด อย่างน้อย 1 รายการในแผนผังความหมาย

ตัวค้นหา

คุณสามารถใช้ onNode และ onAllNodes เพื่อเลือกโหนดอย่างน้อย 1 รายการ ตามลำดับ แต่คุณยังใช้ตัวค้นหาที่สะดวกสำหรับการค้นหาที่พบบ่อยที่สุด ได้ด้วย เช่น 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")

แผนผังที่ยังไม่ได้ผสาน

โหนดบางรายการจะผสานข้อมูลความหมายของโหนดลูก ตัวอย่างเช่น ปุ่มที่มีองค์ประกอบของข้อความ 2 รายการจะผสานป้ายกำกับองค์ประกอบของข้อความ ดังนี้

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()

การยืนยัน

ตรวจสอบการยืนยันโดยเรียกใช้ assert() ใน SemanticsNodeInteraction ที่ตัวค้นหาแสดงผลพร้อม Matcher อย่างน้อย 1 รายการ

// 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

Matcher

มี Matcher หลากหลายประเภทให้คุณใช้ทดสอบโค้ด Compose

Matcher แบบลำดับชั้น

Matcher แบบลำดับชั้นช่วยให้คุณขึ้นหรือลงในแผนผังความหมายและทำการจับคู่ได้

fun hasParent(matcher: SemanticsMatcher): SemanticsMatcher
fun hasAnySibling(matcher: SemanticsMatcher): SemanticsMatcher
fun hasAnyAncestor(matcher: SemanticsMatcher): SemanticsMatcher
fun hasAnyDescendant(matcher: SemanticsMatcher):  SemanticsMatcher

ตัวอย่างการใช้ Matcher เหล่านี้มีดังนี้

composeTestRule.onNode(hasParent(hasText("Button")))
    .assertIsDisplayed()

ตัวเลือก

วิธีสร้างการทดสอบอีกวิธีหนึ่งคือการใช้ ตัวเลือก ซึ่งอาจทำให้การทดสอบบางรายการอ่านง่ายขึ้น

composeTestRule.onNode(hasTestTag("Players"))
    .onChildren()
    .filter(hasClickAction())
    .assertCountEquals(4)
    .onFirst()
    .assert(hasText("John"))

คุณสามารถดูรายการทั้งหมดได้ในเอกสารช่วยเตือนการทดสอบ Compose

แหล่งข้อมูลเพิ่มเติม