ใช้ Espresso Device API เพื่อทดสอบแอปเมื่ออุปกรณ์มีการเปลี่ยนแปลงการกำหนดค่าที่พบบ่อย เช่น การหมุนและการกางหน้าจอ Espresso Device API เป็นเครื่องมือที่แนะนำสำหรับการดำเนินการระดับอุปกรณ์ควบคู่ไปกับกฎการทดสอบ Jetpack Compose หากคุณเพิ่งเริ่มเขียนการทดสอบ UI สำหรับ Jetpack Compose โปรดดูการทดสอบเลย์เอาต์ Compose
Espresso Device API ช่วยให้คุณทริกเกอร์การเปลี่ยนแปลงการกำหนดค่าในอุปกรณ์เสมือนจริงและดำเนินการทดสอบพร้อมกันได้ ดังนั้นจึงมีการดำเนินการหรือการยืนยัน UI เพียงครั้งเดียวในแต่ละครั้ง และผลการทดสอบจะเชื่อถือได้มากขึ้น หากคุณเพิ่งเริ่มเขียนการทดสอบ UI ด้วย Espresso โปรดดูเอกสารประกอบ
หากต้องการใช้ Espresso Device API คุณต้องมีสิ่งต่อไปนี้
- Android Studio Iguana ขึ้นไป
- ปลั๊กอิน Android Gradle 8.3 ขึ้นไป
- โปรแกรมจำลองของ Android 33.1.10 ขึ้นไป
- อุปกรณ์เสมือน Android ที่ใช้ระดับ API 24 ขึ้นไป
ตั้งค่าโปรเจ็กต์สำหรับ Espresso Device API
หากต้องการตั้งค่าโปรเจ็กต์ให้รองรับ Espresso Device API ให้ทำดังนี้
หากต้องการให้การทดสอบส่งคำสั่งไปยังอุปกรณ์ทดสอบ ให้เพิ่มสิทธิ์เข้าถึงเครือข่ายที่จำเป็นลงในไฟล์ Manifest ในชุดซอร์ส
androidTestโดยทำดังนี้<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />หากการทดสอบกำหนดเป้าหมายเป็น Android 17 (ระดับ API 37) ขึ้นไป คุณต้องประกาศสิทธิ์
ACCESS_LOCAL_NETWORKด้วย ดังนี้<uses-permission android:name="android.permission.ACCESS_LOCAL_NETWORK" />เปิดใช้แฟล็กทดลอง
enableEmulatorControlในไฟล์gradle.propertiesดังนี้android.experimental.androidTest.enableEmulatorControl=trueเปิดใช้ตัวเลือก
emulatorControlในสคริปต์บิลด์ระดับโมดูล โดยทำดังนี้Kotlin
testOptions { emulatorControl { enable = true } }
ดึงดูด
testOptions { emulatorControl { enable = true } }
ในสคริปต์บิลด์ระดับโมดูล ให้นำเข้าไลบรารี Espresso Device ไปยังโปรเจ็กต์โดยทำดังนี้
Kotlin
dependencies { androidTestImplementation("androidx.test.espresso:espresso-device:1.1.0") }
ดึงดูด
dependencies { androidTestImplementation 'androidx.test.espresso:espresso-device:1.1.0' }
ทดสอบการเปลี่ยนแปลงการกำหนดค่าที่พบบ่อย
Espresso Device API มีการวางแนวหน้าจอและสถานะพับได้หลายแบบที่คุณใช้เพื่อทริกเกอร์การเปลี่ยนแปลงการกำหนดค่าอุปกรณ์ได้ ตัวอย่างต่อไปนี้แสดงวิธีทริกเกอร์สถานะอุปกรณ์เหล่านี้และยืนยันการเปลี่ยนแปลง UI ที่เกิดขึ้นโดยใช้กฎการทดสอบ Compose
ทดสอบกับการหมุนหน้าจอ
หากต้องการทดสอบการหมุนหน้าจอ คุณสามารถใช้คลาส ScreenOrientationRule เพื่อกำหนดการวางแนวอุปกรณ์ระหว่างการทดสอบได้
ตัวอย่างวิธีทดสอบสิ่งที่จะเกิดขึ้นกับแอปเมื่อหน้าจออุปกรณ์หมุนมีดังนี้
ก่อนอื่น ให้กำหนดกฎการทดสอบ Compose และใช้คลาส
ScreenOrientationRuleเพื่อตั้งค่าอุปกรณ์ให้อยู่ในสถานะเริ่มต้นที่สอดคล้องกัน (เช่น โหมดแนวตั้ง) ดังนี้import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.assertDoesNotExist import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.test.espresso.device.EspressoDevice.onDevice import androidx.test.espresso.device.action.ScreenOrientation import androidx.test.espresso.device.rules.ScreenOrientationRule import org.junit.Rule import org.junit.Test class MyConfigurationTest { // 1. Define the Compose test rule @get:Rule val composeTestRule = createComposeRule() // 2. Define the Espresso Device rule for a consistent starting state @get:Rule val screenOrientationRule = ScreenOrientationRule(ScreenOrientation.PORTRAIT) }หากการทดสอบกำหนดเป้าหมายเป็น Android 17 (ระดับ API 37) ขึ้นไป Espresso Device API จะต้องใช้สิทธิ์
ACCESS_LOCAL_NETWORKคุณต้องตรวจสอบว่าได้รับสิทธิ์นี้ก่อนที่กฎScreenOrientationRuleจะทำงาน ใช้RuleChainของ JUnit เพื่อเรียกใช้กฎGrantPermissionRuleก่อน ดังนี้import androidx.test.rule.GrantPermissionRule import org.junit.rules.RuleChain class MyConfigurationTest { val grantPermissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_LOCAL_NETWORK) val composeTestRule = createComposeRule() val screenOrientationRule = ScreenOrientationRule(ScreenOrientation.PORTRAIT) @get:Rule val chain = RuleChain .outerRule(grantPermissionRule) .around(composeTestRule) .around(screenOrientationRule) }สร้างการทดสอบที่ตั้งค่าอุปกรณ์ให้เป็นแนวนอนระหว่างการดำเนินการทดสอบ ดังนี้
@Test fun myRotationTest() { ... // Sets the device to landscape orientation during test execution. onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE) ... }หลังจากหน้าจอหมุนแล้ว ให้ใช้
composeTestRuleเพื่อตรวจสอบว่า Composable ของคุณปรับให้เข้ากับสถานะใหม่ตามที่คาดไว้@Test fun myRotationTest() { ... // Sets the device to landscape orientation during test execution. onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE) composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed() composeTestRule.onNodeWithTag("BottomBar").assertDoesNotExist() }
ทดสอบกับการกางหน้าจอออก
ต่อไปนี้คือตัวอย่างวิธีทดสอบสิ่งที่จะเกิดขึ้นกับแอปหากอยู่ในอุปกรณ์แบบพับได้และหน้าจอกางออก
ก่อนอื่น ให้ทดสอบกับอุปกรณ์ในสถานะพับโดยเรียกใช้
onDevice().setClosedMode()ตรวจสอบว่า Composable ของคุณปรับให้เข้ากับความกว้างของหน้าจอขนาดกะทัดรัด@Test fun myUnfoldedTest() { onDevice().setClosedMode() composeTestRule.onNodeWithTag("BottomBar").assertIsDisplayed() composeTestRule.onNodeWithTag("NavRail").assertDoesNotExist() ... }หากต้องการเปลี่ยนเป็นสถานะกางออกเต็มที่ ให้เรียกใช้
onDevice().setFlatMode()ตรวจสอบว่า Composable ปรับให้เข้ากับคลาสขนาดแบบขยาย@Test fun myUnfoldedTest() { onDevice().setClosedMode() ... onDevice().setFlatMode() composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed() composeTestRule.onNodeWithTag("BottomBar").assertDoesNotExist() }
ระบุอุปกรณ์ที่การทดสอบต้องการ
หากคุณทำการทดสอบที่ดำเนินการพับบนอุปกรณ์ที่พับไม่ได้ การทดสอบก็อาจไม่สำเร็จ หากต้องการดำเนินการเฉพาะการทดสอบที่เกี่ยวข้องกับอุปกรณ์ที่ทำงานอยู่ ให้ใช้คำอธิบายประกอบ @RequiresDeviceMode เครื่องมือรันทดสอบจะข้ามการรันทดสอบในอุปกรณ์ที่ไม่รองรับการกำหนดค่าที่กำลังทดสอบโดยอัตโนมัติ คุณสามารถเพิ่มกฎข้อกำหนดของอุปกรณ์ลงในการทดสอบแต่ละรายการหรือทั้งชั้นเรียนการทดสอบได้
ตัวอย่างเช่น หากต้องการระบุว่าควรเรียกใช้การทดสอบเฉพาะในอุปกรณ์ที่รองรับการกางออกเป็นการกำหนดค่าแบบแบน ให้เพิ่มโค้ด @RequiresDeviceMode ต่อไปนี้ลงในการทดสอบ
@Test
@RequiresDeviceMode(mode = FLAT)
fun myUnfoldedTest() {
...
}