相互運用性

Compose は一般的なテスト フレームワークと統合されています。

Espresso との相互運用

ハイブリッド アプリでは、ビュー階層内に Compose コンポーネントがあり、 Compose コンポーザブル内のビュー(AndroidView コンポーザブルを使用)

どちらのタイプも、マッチングするために特別な手順は必要ありません。視聴回数のマッチ率 Espresso の onViewComposeTestRule を持つ Compose 要素。

@Test
fun androidViewInteropTest() {
    // Check the initial state of a TextView that depends on a Compose state.
    Espresso.onView(withText("Hello Views")).check(matches(isDisplayed()))
    // Click on the Compose button that changes the state.
    composeTestRule.onNodeWithText("Click here").performClick()
    // Check the new value.
    Espresso.onView(withText("Hello Compose")).check(matches(isDisplayed()))
}

UiAutomator との相互運用

デフォルトでは、コンポーザブルに UiAutomator からアクセスできるのは、 わかりやすい記述子(表示テキスト、コンテンツの説明など)を付ける必要があります。目標 Modifier.testTag を使用するコンポーザブルにアクセスするには、以下を有効にする必要があります。 特定のコンポーザブルのセマンティック プロパティ testTagsAsResourceId サブツリーです。この動作を有効にすると、何も持たないコンポーザブルで スクロール可能なコンポーザブル(LazyColumn など)など、その他の一意のハンドル。

コンポーザブルの階層の上位で 1 回だけセマンティック プロパティを有効にして、 Modifier.testTag を持つネストされたすべてのコンポーザブルが、 UiAutomator。

Scaffold(
    // Enables for all composables in the hierarchy.
    modifier = Modifier.semantics {
        testTagsAsResourceId = true
    }
){
    // Modifier.testTag is accessible from UiAutomator for composables nested here.
    LazyColumn(
        modifier = Modifier.testTag("myLazyColumn")
    ){
        // Content
    }
}

Modifier.testTag(tag) を持つコンポーザブルは、次のコマンドでアクセスできます。 の By.res(resourceName)resourceName と同じ tag を使用します。

val device = UiDevice.getInstance(getInstrumentation())

val lazyColumn: UiObject2 = device.findObject(By.res("myLazyColumn"))
// Some interaction with the lazyColumn.

参考情報