ใช้ 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.propertiesandroid.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เพื่อตรวจสอบว่าคอมโพสได้ปรับให้เข้ากับสถานะใหม่ตามที่คาดไว้@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()ตรวจสอบว่าคอมโพสได้ปรับให้เข้ากับความกว้างหน้าจอขนาดกะทัดรัด@Test fun myUnfoldedTest() { onDevice().setClosedMode() composeTestRule.onNodeWithTag("BottomBar").assertIsDisplayed() composeTestRule.onNodeWithTag("NavRail").assertDoesNotExist() ... }หากต้องการเปลี่ยนไปใช้สถานะกางออกเต็มที่ ให้เรียกใช้
onDevice().setFlatMode()ตรวจสอบว่าคอมโพสได้ปรับให้เข้ากับคลาสขนาดที่ขยาย@Test fun myUnfoldedTest() { onDevice().setClosedMode() ... onDevice().setFlatMode() composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed() composeTestRule.onNodeWithTag("BottomBar").assertDoesNotExist() }
ระบุอุปกรณ์ที่การทดสอบต้องใช้
หากคุณเรียกใช้การทดสอบที่ดำเนินการพับในอุปกรณ์ที่พับไม่ได้ การทดสอบมีแนวโน้มที่จะล้มเหลว หากต้องการเรียกใช้เฉพาะการทดสอบที่เกี่ยวข้องกับอุปกรณ์ที่กำลังทำงานอยู่ ให้ใช้คำอธิบายประกอบ @RequiresDeviceMode ตัวเรียกใช้การทดสอบจะข้ามการเรียกใช้การทดสอบในอุปกรณ์ที่ไม่รองรับการกำหนดค่าที่กำลังทดสอบโดยอัตโนมัติ คุณสามารถเพิ่มกฎข้อกำหนดของอุปกรณ์ลงในการทดสอบแต่ละรายการหรือคลาสการทดสอบทั้งหมดได้
ตัวอย่างเช่น หากต้องการระบุว่าควรเรียกใช้การทดสอบในอุปกรณ์ที่รองรับการกางออกเป็นการกำหนดค่าแบบแบนเท่านั้น ให้เพิ่มโค้ด @RequiresDeviceMode ต่อไปนี้ลงในการทดสอบ
@Test
@RequiresDeviceMode(mode = FLAT)
fun myUnfoldedTest() {
...
}