ใช้ 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 ให้ทำดังนี้
หากต้องการให้การทดสอบส่งคำสั่งไปยังอุปกรณ์ทดสอบได้ ให้เพิ่มสิทธิ์
INTERNETและACCESS_NETWORK_STATEลงในไฟล์ Manifest ในชุดซอร์สandroidTest<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />เปิดใช้ฟีเจอร์ทดลอง
enableEmulatorControlในไฟล์gradle.propertiesandroid.experimental.androidTest.enableEmulatorControl=trueเปิดใช้ตัวเลือก
emulatorControlในสคริปต์บิลด์ระดับโมดูลKotlin
testOptions { emulatorControl { enable = true } }
ดึงดูด
testOptions { emulatorControl { enable = true } }
ในสคริปต์บิลด์ระดับโมดูล ให้นำเข้าไลบรารีอุปกรณ์ Espresso ไปยังโปรเจ็กต์ ของคุณโดยทำดังนี้
Kotlin
dependencies { androidTestImplementation("androidx.test.espresso:espresso-device:1.0.1") }
ดึงดูด
dependencies { androidTestImplementation 'androidx.test.espresso:espresso-device:1.0.1' }
ทดสอบกับการเปลี่ยนแปลงการกำหนดค่าทั่วไป
Espresso Device API มีการวางแนวหน้าจอและสถานะพับได้หลายแบบ ที่คุณใช้เพื่อจำลองการเปลี่ยนแปลงการกำหนดค่าอุปกรณ์ได้ ตัวอย่างต่อไปนี้ แสดงวิธีทริกเกอร์สถานะอุปกรณ์เหล่านี้และยืนยันการเปลี่ยนแปลง UI ที่เกิดขึ้น โดยใช้กฎการทดสอบ Compose
ทดสอบกับการหมุนหน้าจอ
ตัวอย่างวิธีทดสอบสิ่งที่เกิดขึ้นกับแอปเมื่อหน้าจออุปกรณ์ หมุนมีดังนี้
ก่อนอื่น ให้กำหนดกฎการทดสอบ Compose และตั้งค่าอุปกรณ์ให้อยู่ใน สถานะเริ่มต้นที่สอดคล้องกัน (เช่น โหมดแนวตั้ง)
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) }สร้างการทดสอบที่ตั้งค่าอุปกรณ์เป็นแนวนอนระหว่างการดำเนินการทดสอบ
@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 Test Runner
จะข้ามการทดสอบบนอุปกรณ์ที่ไม่รองรับ
การกำหนดค่าที่กำลังทดสอบโดยอัตโนมัติ คุณเพิ่มกฎข้อกำหนดของอุปกรณ์ลงในการทดสอบแต่ละรายการ
หรือทั้งคลาสทดสอบได้
ตัวอย่างเช่น หากต้องการระบุว่าควรเรียกใช้การทดสอบเฉพาะในอุปกรณ์ที่รองรับ
การกางออกเป็นการกำหนดค่าแบบแบน ให้เพิ่มโค้ด @RequiresDeviceMode ต่อไปนี้
ในการทดสอบ
@Test
@RequiresDeviceMode(mode = FLAT)
fun myUnfoldedTest() {
...
}