Espresso Device API'yi kullanarak ekran yapılandırma değişikliklerine göre test etme
Koleksiyonlar ile düzeninizi koruyun
İçeriği tercihlerinize göre kaydedin ve kategorilere ayırın.
Cihazda döndürme ve ekranı açma gibi yaygın yapılandırma değişiklikleri olduğunda uygulamanızı test etmek için Espresso Device API'yi kullanın. Espresso
Device API, bu yapılandırma değişikliklerini sanal bir cihazda simüle etmenize olanak tanır ve testlerinizi senkron olarak yürütür. Bu nedenle, her seferinde yalnızca bir kullanıcı arayüzü işlemi veya onaylama gerçekleşir ve test sonuçlarınız daha güvenilir olur. Espresso ile kullanıcı arayüzü testleri yazmaya yeni başladıysanız dokümanlarına göz atın.
Espresso Device API'yi kullanmak için aşağıdakilere ihtiyacınız vardır:
- Android Studio Iguana veya sonraki sürümler
- Android Gradle eklentisi 8.3 veya sonraki sürümler
- Android Emulator 33.1.10 veya sonraki sürümler
- API düzeyi 24 veya daha yeni bir sürümü çalıştıran Android sanal cihaz
Projenizi Espresso Device API için ayarlama
Projenizi Espresso Device API'yi destekleyecek şekilde ayarlamak için aşağıdakileri yapın:
Testin, test cihazına komut göndermesine izin vermek için INTERNET
ve ACCESS_NETWORK_STATE
izinlerini androidTest
kaynak kümesindeki manifest dosyasına ekleyin:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
enableEmulatorControl
deneysel işaretini gradle.properties
dosyasında etkinleştirin:
android.experimental.androidTest.enableEmulatorControl=true
Modül düzeyindeki derleme komut dosyasında emulatorControl
seçeneğini etkinleştirin:
Kotlin
testOptions {
emulatorControl {
enable = true
}
}
Groovy
testOptions {
emulatorControl {
enable = true
}
}
Modül düzeyindeki derleme komut dosyasında Espresso Device kitaplığını projenize aktarın:
Kotlin
dependencies {
androidTestImplementation("androidx.test.espresso:espresso-device:1.0.1")
}
Groovy
dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-device:1.0.1'
}
Sık yapılan yapılandırma değişikliklerine karşı test etme
Espresso Device API'de, cihaz yapılandırma değişikliklerini simüle etmek için kullanabileceğiniz birden fazla ekran yönü ve katlanabilir durum bulunur.
Ekran döndürmeye karşı test etme
Cihaz ekranı döndürüldüğünde uygulamanızda ne olduğunu test etme ile ilgili bir örneği aşağıda bulabilirsiniz:
Öncelikle tutarlı bir başlangıç durumu için cihazı dikey moda ayarlayın:
import androidx.test.espresso.device.action.ScreenOrientation
import androidx.test.espresso.device.rules.ScreenOrientationRule
...
@get:Rule
val screenOrientationRule: ScreenOrientationRule = ScreenOrientationRule(ScreenOrientation.PORTRAIT)
Test yürütme sırasında cihazı yatay yöne ayarlayan bir test oluşturun:
@Test
fun myRotationTest() {
...
// Sets the device to landscape orientation during test execution.
onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
...
}
Ekran döndükten sonra kullanıcı arayüzünün yeni düzene beklendiği gibi uyum sağladığını kontrol edin.
@Test
fun myRotationTest() {
...
// Sets the device to landscape orientation during test execution.
onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("BottomBar").assertDoesNotExist()
}
Ekran açılma durumuna göre test etme
Katlanabilir bir cihazda bulunan uygulamanızın ekranı açıldığında ne olacağını test etme örneğini aşağıda bulabilirsiniz:
Öncelikle onDevice().setClosedMode()
numarasını arayarak cihazı katlı durumdayken test edin. Uygulamanızın düzeninin, kompakt ekran genişliğine uyum sağladığından emin olun.
@Test
fun myUnfoldedTest() {
onDevice().setClosedMode()
composeTestRule.onNodeWithTag("BottomBar").assetIsDisplayed()
composeTestRule.onNodeWithTag("NavRail").assetDoesNotExist()
...
}
Tamamen açılmış duruma geçmek için onDevice().setFlatMode()
işlevini çağırın. Uygulamanın düzeninin genişletilmiş boyut sınıfına uyum sağladığını kontrol edin.
@Test
fun myUnfoldedTest() {
onDevice().setClosedMode()
...
onDevice().setFlatMode()
composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("BottomBar").assetDoesNotExist()
}
Testlerinizin hangi cihazlara ihtiyacı olduğunu belirtin
Katlanabilir olmayan bir cihazda katlama işlemleri gerçekleştiren bir test çalıştırıyorsanız testin başarısız olması muhtemeldir. Yalnızca çalışan cihazla alakalı testleri yürütmek için @RequiresDeviceMode
ek açıklamasını kullanın. Test çalıştırıcı, test edilen yapılandırmayı desteklemeyen cihazlarda test çalıştırmayı otomatik olarak atlar. Cihaz koşulu kuralını her teste veya test sınıfının tamamına ekleyebilirsiniz.
Örneğin, bir testin yalnızca düz yapılandırmaya açılmayı destekleyen cihazlarda çalıştırılması gerektiğini belirtmek için testinize aşağıdaki @RequiresDeviceMode
kodunu ekleyin:
@Test
@RequiresDeviceMode(mode = FLAT)
fun myUnfoldedTest() {
...
}
Bu sayfadaki içerik ve kod örnekleri, İçerik Lisansı sayfasında açıklanan lisanslara tabidir. Java ve OpenJDK, Oracle ve/veya satış ortaklarının tescilli ticari markasıdır.
Son güncelleme tarihi: 2025-09-04 UTC.
[[["Anlaması kolay","easyToUnderstand","thumb-up"],["Sorunumu çözdü","solvedMyProblem","thumb-up"],["Diğer","otherUp","thumb-up"]],[["İhtiyacım olan bilgiler yok","missingTheInformationINeed","thumb-down"],["Çok karmaşık / çok fazla adım var","tooComplicatedTooManySteps","thumb-down"],["Güncel değil","outOfDate","thumb-down"],["Çeviri sorunu","translationIssue","thumb-down"],["Örnek veya kod sorunu","samplesCodeIssue","thumb-down"],["Diğer","otherDown","thumb-down"]],["Son güncelleme tarihi: 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 }"]]