ทดสอบการเปลี่ยนแปลงการกำหนดค่าหน้าจอด้วย Espresso Device API
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ใช้ Espresso Device API เพื่อทดสอบแอปเมื่ออุปกรณ์มีการเปลี่ยนแปลงการกำหนดค่าทั่วไป เช่น การหมุนและการกางหน้าจอ Espresso
Device API ช่วยให้คุณจำลองการเปลี่ยนแปลงการกำหนดค่าเหล่านี้ในอุปกรณ์เสมือนและ
เรียกใช้การทดสอบแบบซิงโครนัสได้ ดังนั้นจึงมีการดำเนินการ UI หรือการยืนยันเพียงอย่างเดียวในแต่ละครั้ง และผลการทดสอบจึงเชื่อถือได้มากขึ้น หากเพิ่งเริ่มเขียนการทดสอบ UI ด้วย Espresso โปรดดูเอกสารประกอบ
หากต้องการใช้ Espresso Device API คุณต้องมีสิ่งต่อไปนี้
- Android Studio Iguana ขึ้นไป
- ปลั๊กอิน Android Gradle 8.3 ขึ้นไป
- Android Emulator 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.properties
android.experimental.androidTest.enableEmulatorControl=true
เปิดใช้ตัวเลือก emulatorControl
ในสคริปต์บิลด์ระดับโมดูล
ดังนี้
Kotlin
testOptions {
emulatorControl {
enable = true
}
}
Groovy
testOptions {
emulatorControl {
enable = true
}
}
ในสคริปต์บิลด์ระดับโมดูล ให้นำเข้าไลบรารีอุปกรณ์ Espresso
ลงในโปรเจ็กต์ของคุณ
Kotlin
dependencies {
androidTestImplementation("androidx.test.espresso:espresso-device:1.0.1")
}
Groovy
dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-device:1.0.1'
}
ทดสอบกับการเปลี่ยนแปลงการกำหนดค่าทั่วไป
Espresso Device API มีการวางแนวหน้าจอและสถานะการพับหลายแบบ
ที่คุณใช้เพื่อจำลองการเปลี่ยนแปลงการกำหนดค่าอุปกรณ์ได้
ทดสอบกับการหมุนหน้าจอ
ตัวอย่างวิธีทดสอบสิ่งที่เกิดขึ้นกับแอปเมื่อหน้าจออุปกรณ์
หมุนมีดังนี้
ก่อนอื่น ให้ตั้งค่าอุปกรณ์เป็นโหมดแนวตั้งเพื่อให้มีสถานะเริ่มต้นที่สอดคล้องกัน
import androidx.test.espresso.device.action.ScreenOrientation
import androidx.test.espresso.device.rules.ScreenOrientationRule
...
@get:Rule
val screenOrientationRule: ScreenOrientationRule = ScreenOrientationRule(ScreenOrientation.PORTRAIT)
สร้างการทดสอบที่ตั้งค่าอุปกรณ์เป็นแนวนอนระหว่างการดำเนินการทดสอบ
ดังนี้
@Test
fun myRotationTest() {
...
// Sets the device to landscape orientation during test execution.
onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
...
}
หลังจากหมุนหน้าจอแล้ว ให้ตรวจสอบว่า UI ปรับให้เข้ากับเลย์เอาต์ใหม่ตามที่คาดไว้
@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").assetIsDisplayed()
composeTestRule.onNodeWithTag("NavRail").assetDoesNotExist()
...
}
หากต้องการเปลี่ยนไปใช้สถานะที่กางออกจนสุด ให้เรียกใช้
onDevice().setFlatMode()
ตรวจสอบว่าเลย์เอาต์ของแอปปรับให้เข้ากับ
คลาสขนาดที่ขยายแล้ว
@Test
fun myUnfoldedTest() {
onDevice().setClosedMode()
...
onDevice().setFlatMode()
composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("BottomBar").assetDoesNotExist()
}
ระบุอุปกรณ์ที่การทดสอบของคุณต้องการ
หากคุณทำการทดสอบที่ดำเนินการพับบนอุปกรณ์ที่พับไม่ได้ การทดสอบน่าจะล้มเหลว หากต้องการเรียกใช้เฉพาะการทดสอบที่เกี่ยวข้อง
กับอุปกรณ์ที่กำลังทำงาน ให้ใช้คำอธิบายประกอบ @RequiresDeviceMode
Test Runner
จะข้ามการทดสอบในอุปกรณ์ที่ไม่รองรับ
การกำหนดค่าที่กำลังทดสอบโดยอัตโนมัติ คุณเพิ่มกฎข้อกำหนดของอุปกรณ์ลงในการทดสอบแต่ละรายการ
หรือทั้งคลาสทดสอบได้
ตัวอย่างเช่น หากต้องการระบุว่าควรเรียกใช้การทดสอบในอุปกรณ์ที่รองรับ
การกางออกเป็นการกำหนดค่าแบบแบนเท่านั้น ให้เพิ่มโค้ด @RequiresDeviceMode
ต่อไปนี้
ในการทดสอบ
@Test
@RequiresDeviceMode(mode = FLAT)
fun myUnfoldedTest() {
...
}
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-09-04 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-09-04 UTC"],[],[],null,["Use the Espresso Device API to test your app when the device undergoes common\nconfiguration changes, such as rotation and screen unfolding. The Espresso\nDevice API lets you simulate these configuration changes on a virtual device and\nexecutes your tests synchronously, so only one UI action or assertion happens at\na time and your test results are more reliable. If you're new to writing UI\ntests with Espresso, see its [documentation](/training/testing/espresso).\n\nTo use the Espresso Device API, you need the following:\n\n- Android Studio Iguana or higher\n- Android Gradle plugin 8.3 or higher\n- Android Emulator 33.1.10 or higher\n- Android virtual device that runs API level 24 or higher\n\nSet up your project for the Espresso Device API\n\nTo set up your project so it supports the Espresso Device API, do the following:\n\n1. To let the test pass commands to the test device, add the\n `INTERNET` and `ACCESS_NETWORK_STATE` permissions to the manifest file in the `androidTest` source set:\n\n ```\n \u003cuses-permission android:name=\"android.permission.INTERNET\" /\u003e\n \u003cuses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" /\u003e\n \n ```\n2. Enable the `enableEmulatorControl` experimental flag in the\n `gradle.properties` file:\n\n ```\n android.experimental.androidTest.enableEmulatorControl=true\n \n ```\n3. Enable the `emulatorControl` option in the module-level build\n script:\n\n Kotlin \n\n ```kotlin\n testOptions {\n emulatorControl {\n enable = true\n }\n }\n \n ```\n\n Groovy \n\n ```groovy\n testOptions {\n emulatorControl {\n enable = true\n }\n }\n \n ```\n4. In the module-level build script, import the Espresso Device library\n into your project:\n\n Kotlin \n\n ```kotlin\n dependencies {\n androidTestImplementation(\"androidx.test.espresso:espresso-device:1.0.1\")\n }\n \n ```\n\n Groovy \n\n ```groovy\n dependencies {\n androidTestImplementation 'androidx.test.espresso:espresso-device:1.0.1'\n }\n \n ```\n\nTest against common configuration changes\n\nThe Espresso Device API has multiple screen orientation and foldable states that\nyou can use to simulate device configuration changes.\n\nTest against screen rotation\n\nHere's an example of how to test what happens to your app when the device screen\nrotates:\n\n1. First, for a consistent starting state set the device to portrait\n mode:\n\n ```kotlin\n import androidx.test.espresso.device.action.ScreenOrientation\n import androidx.test.espresso.device.rules.ScreenOrientationRule\n ...\n @get:Rule\n val screenOrientationRule: ScreenOrientationRule = ScreenOrientationRule(ScreenOrientation.PORTRAIT)\n \n ```\n2. Create a test that sets the device to landscape orientation during test\n execution:\n\n ```kotlin\n @Test\n fun myRotationTest() {\n ...\n // Sets the device to landscape orientation during test execution.\n onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)\n ...\n }\n \n ```\n3. After the screen rotates, check that the UI adapts to the new layout as expected.\n\n ```kotlin\n @Test\n fun myRotationTest() {\n ...\n // Sets the device to landscape orientation during test execution.\n onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)\n composeTestRule.onNodeWithTag(\"NavRail\").assertIsDisplayed()\n composeTestRule.onNodeWithTag(\"BottomBar\").assertDoesNotExist()\n }\n \n ```\n\nTest against screen unfolding\n\nHere's an example of how to test what happens to your app if it's on a foldable\ndevice and the screen unfolds:\n\n1. First, test with the device in the folded state by calling\n `onDevice().setClosedMode()`. Make sure that your app's layout\n adapts to the compact screen width.\n\n ```kotlin\n @Test\n fun myUnfoldedTest() {\n onDevice().setClosedMode()\n composeTestRule.onNodeWithTag(\"BottomBar\").assetIsDisplayed()\n composeTestRule.onNodeWithTag(\"NavRail\").assetDoesNotExist()\n ...\n }\n \n ```\n2. To transition to a fully unfolded state, call\n `onDevice().setFlatMode()`. Check that the app's layout adapts to\n the expanded size class.\n\n ```kotlin\n @Test\n fun myUnfoldedTest() {\n onDevice().setClosedMode()\n ...\n onDevice().setFlatMode()\n composeTestRule.onNodeWithTag(\"NavRail\").assertIsDisplayed()\n composeTestRule.onNodeWithTag(\"BottomBar\").assetDoesNotExist()\n }\n \n ```\n\nSpecify what devices your tests need\n\nIf you're running a test that performs folding actions on a device that isn't\nfoldable, the test will likely fail. To execute only the tests that are relevant\nto the running device, use the `@RequiresDeviceMode` annotation. The test runner\nautomatically skips running tests on devices that don't support the\nconfiguration being tested. You can add the device requirement rule to each test\nor an entire test class.\n\nFor example, to specify that a test should only be run on devices that support\nunfolding to a flat configuration, add the following `@RequiresDeviceMode` code\nto your test: \n\n @Test\n @RequiresDeviceMode(mode = FLAT)\n fun myUnfoldedTest() {\n ...\n }"]]