בדיקה מול שינויים בהגדרות המסך באמצעות Espresso Device API
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
אפשר להשתמש ב-Espresso Device API כדי לבדוק את האפליקציה כשמתבצעים במכשיר שינויים נפוצים בהגדרות, כמו סיבוב ופריסת המסך. Espresso Device API מאפשר לכם לדמות את השינויים בהגדרות במכשיר וירטואלי, ומבצע את הבדיקות באופן סינכרוני, כך שרק פעולת 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
לקובץ המניפסט בערכת המקור 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
}
}
גרוב
testOptions {
emulatorControl {
enable = true
}
}
בסקריפט הבנייה ברמת המודול, מייבאים את ספריית Espresso Device לפרויקט:
Kotlin
dependencies {
androidTestImplementation("androidx.test.espresso:espresso-device:1.0.1")
}
גרוב
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)
...
}
אחרי שהמסך מסתובב, בודקים שממשק המשתמש מותאם לפריסה החדשה כמו שציפיתם.
@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
. כלי ההרצה של הבדיקות מדלג באופן אוטומטי על הרצת בדיקות במכשירים שלא תומכים בהגדרה שנבדקת. אפשר להוסיף את כלל הדרישה למכשיר לכל בדיקה או לכל מחלקת בדיקה.
לדוגמה, כדי לציין שבדיקה תופעל רק במכשירים שתומכים בפתיחה לתצורה שטוחה, מוסיפים את @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 }"]]