使用 Espresso Device API 測試螢幕設定變更

當裝置遇到常見的設定變更 (例如旋轉和螢幕展開) 時,請使用 Espresso Device API 測試應用程式。Espresso Device API 可讓您在虛擬裝置上模擬這些設定變更,並同步執行測試,因此一次只會發生一項 UI 動作或斷言,測試結果也更加可靠。如果您是第一次使用 Espresso 編寫 UI 測試,請參閱其說明文件

如要使用 Espresso Device API,您需要下列項目:

  • Android Studio Iguana 以上版本
  • Android Gradle 外掛程式 8.3 以上版本
  • Android Emulator 33.1.10 以上版本
  • 搭載 API 級別 24 以上版本的 Android 虛擬裝置

設定 Espresso Device API 專案

如要設定專案,讓專案支援 Espresso Device API,請按照下列步驟操作:

  1. 如要讓測試裝置執行測試傳遞指令,請在 androidTest 來源集的資訊清單檔案中新增 INTERNETACCESS_NETWORK_STATE 權限:

      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permissions.ACCESS_NETWORK_STATE" />
      
  2. gradle.properties 檔案中啟用 enableEmulatorControl 實驗性旗標:

      android.experimental.androidTest.enableEmulatorControl=true
      
  3. 在模組層級的建構指令碼中啟用 emulatorControl 選項:

    Kotlin

      testOptions {
        emulatorControl {
          enable = true
        }
      }
      

    Groovy

      testOptions {
        emulatorControl {
          enable = true
        }
      }
      
  4. 在模組層級建構指令碼中,將 Espresso Device 程式庫匯入專案:

    Kotlin

      dependencies {
        androidTestImplementation("androidx.test.espresso:espresso-device:3.5.1")
      }
      

    Groovy

      dependencies {
        androidTestImplementation 'androidx.test.espresso:espresso-device:3.5.1'
      }
      

針對常見的設定變更進行測試

Espresso Device API 提供多種螢幕方向和折疊狀態,方便您模擬裝置設定變更。

針對螢幕旋轉進行測試

以下範例說明如何測試裝置螢幕旋轉時應用程式會發生什麼情況:

  1. 首先,為了讓啟動狀態保持一致,請將裝置設為直向模式:

      import androidx.test.espresso.device.action.ScreenOrientation
      import androidx.test.espresso.device.rules.ScreenOrientationRule
      ...
      @get:Rule
      val screenOrientationRule: ScreenOrientationRule = ScreenOrientationRule(ScreenOrientation.PORTRAIT)
      
  2. 建立測試,在測試執行期間將裝置設為橫向:

      @Test
      fun myRotationTest() {
        ...
        // Sets the device to landscape orientation during test execution.
        onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
        ...
      }
      
  3. 畫面旋轉後,請檢查 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()
      }
      

測試螢幕展開時

以下範例說明如何測試應用程式在折疊式裝置和展開螢幕的情況下,應用程式會發生什麼情況:

  1. 首先,請呼叫 onDevice().setClosedMode(),測試處於折疊狀態的裝置。請確保應用程式的版面配置可配合螢幕大小調整為適當的寬度。

      @Test
      fun myUnfoldedTest() {
        onDevice().setClosedMode()
        composeTestRule.onNodeWithTag("BottomBar").assetIsDisplayed()
        composeTestRule.onNodeWithTag("NavRail").assetDoesNotExist()
        ...
      }
      
  2. 如要轉換為完全展開的狀態,請呼叫 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() {
  ...
}