Kiểm thử dựa trên các thay đổi về cấu hình màn hình bằng Espresso Device API
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Sử dụng Espresso Device API để kiểm thử ứng dụng khi thiết bị trải qua các thay đổi thường gặp về cấu hình, chẳng hạn như xoay và mở màn hình. Espresso Device API cho phép bạn mô phỏng những thay đổi về cấu hình này trên một thiết bị ảo và thực thi các kiểm thử một cách đồng bộ, do đó, chỉ có một thao tác hoặc câu khẳng định trên giao diện người dùng xảy ra tại một thời điểm và kết quả kiểm thử của bạn sẽ đáng tin cậy hơn. Nếu bạn mới bắt đầu viết các chương trình kiểm thử giao diện người dùng bằng Espresso, hãy xem tài liệu của Espresso.
Để sử dụng Espresso Device API, bạn cần có những điều kiện sau:
- Android Studio Iguana trở lên
- Trình bổ trợ Android cho Gradle 8.3 trở lên
- Trình mô phỏng Android 33.1.10 trở lên
- Thiết bị ảo Android chạy API cấp 24 trở lên
Thiết lập dự án cho Espresso Device API
Để thiết lập dự án sao cho dự án đó hỗ trợ Espresso Device API, hãy làm như sau:
Để cho phép kiểm thử truyền các lệnh đến thiết bị kiểm thử, hãy thêm các quyền INTERNET
và ACCESS_NETWORK_STATE
vào tệp kê khai trong nhóm tài nguyên androidTest
:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Bật cờ thử nghiệm enableEmulatorControl
trong tệp gradle.properties
:
android.experimental.androidTest.enableEmulatorControl=true
Bật lựa chọn emulatorControl
trong tập lệnh bản dựng ở cấp mô-đun:
Kotlin
testOptions {
emulatorControl {
enable = true
}
}
Groovy
testOptions {
emulatorControl {
enable = true
}
}
Trong tập lệnh bản dựng cấp mô-đun, hãy nhập thư viện Espresso Device vào dự án của bạn:
Kotlin
dependencies {
androidTestImplementation("androidx.test.espresso:espresso-device:1.0.1")
}
Groovy
dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-device:1.0.1'
}
Kiểm thử đối với các thay đổi thường gặp về cấu hình
Espresso Device API có nhiều trạng thái màn hình và trạng thái gập mà bạn có thể dùng để mô phỏng các thay đổi về cấu hình thiết bị.
Kiểm thử dựa trên tính năng xoay màn hình
Dưới đây là ví dụ về cách kiểm thử điều gì sẽ xảy ra với ứng dụng của bạn khi màn hình thiết bị xoay:
Trước tiên, để có trạng thái bắt đầu nhất quán, hãy đặt thiết bị ở chế độ dọc:
import androidx.test.espresso.device.action.ScreenOrientation
import androidx.test.espresso.device.rules.ScreenOrientationRule
...
@get:Rule
val screenOrientationRule: ScreenOrientationRule = ScreenOrientationRule(ScreenOrientation.PORTRAIT)
Tạo một chương trình kiểm thử đặt thiết bị ở hướng ngang trong quá trình thực thi kiểm thử:
@Test
fun myRotationTest() {
...
// Sets the device to landscape orientation during test execution.
onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
...
}
Sau khi màn hình xoay, hãy kiểm tra để đảm bảo giao diện người dùng thích ứng với bố cục mới như dự kiến.
@Test
fun myRotationTest() {
...
// Sets the device to landscape orientation during test execution.
onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("BottomBar").assertDoesNotExist()
}
Kiểm thử khi màn hình mở ra
Dưới đây là ví dụ về cách kiểm thử những gì sẽ xảy ra với ứng dụng của bạn nếu ứng dụng đó nằm trên một thiết bị có thể gập lại và màn hình mở ra:
Trước tiên, hãy kiểm thử khi thiết bị ở trạng thái gập bằng cách gọi onDevice().setClosedMode()
. Đảm bảo rằng bố cục của ứng dụng thích ứng với chiều rộng màn hình nhỏ gọn.
@Test
fun myUnfoldedTest() {
onDevice().setClosedMode()
composeTestRule.onNodeWithTag("BottomBar").assetIsDisplayed()
composeTestRule.onNodeWithTag("NavRail").assetDoesNotExist()
...
}
Để chuyển sang trạng thái mở hoàn toàn, hãy gọi onDevice().setFlatMode()
. Kiểm tra để đảm bảo bố cục của ứng dụng thích ứng với lớp kích thước mở rộng.
@Test
fun myUnfoldedTest() {
onDevice().setClosedMode()
...
onDevice().setFlatMode()
composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("BottomBar").assetDoesNotExist()
}
Chỉ định những thiết bị mà các kiểm thử của bạn cần
Nếu bạn đang chạy một quy trình kiểm thử thực hiện các thao tác gập trên một thiết bị không gập được, thì quy trình kiểm thử đó có thể sẽ không thành công. Để chỉ thực thi những kiểm thử có liên quan đến thiết bị đang chạy, hãy dùng chú giải @RequiresDeviceMode
. Trình chạy kiểm thử sẽ tự động bỏ qua việc chạy kiểm thử trên những thiết bị không hỗ trợ cấu hình đang được kiểm thử. Bạn có thể thêm quy tắc yêu cầu về thiết bị vào từng bài kiểm thử hoặc toàn bộ lớp kiểm thử.
Ví dụ: để chỉ định rằng một quy trình kiểm thử chỉ được chạy trên những thiết bị hỗ trợ mở ra thành cấu hình phẳng, hãy thêm mã @RequiresDeviceMode
sau đây vào quy trình kiểm thử:
@Test
@RequiresDeviceMode(mode = FLAT)
fun myUnfoldedTest() {
...
}
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-09-04 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 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 }"]]