---
title: "onRootWithViewInteraction"
description: "Scopes the Compose interaction to the View hierarchy matched by the provided Espresso ViewInteraction."
type: "function"
lastmod: "2026-04-23T11:19:38.815688Z"
---
## API Reference

### onRootWithViewInteraction

> Source set: Android

```kotlin
@ExperimentalTestApi
fun ComposeUiTest.onRootWithViewInteraction(
    interaction: ViewInteraction
): SemanticsNodeInteractionsProvider
```

Scopes the Compose interaction to the View hierarchy matched by the provided Espresso
`ViewInteraction`.

It resolves the View from the Espresso [interaction](/jetpack-compose/androidx.compose.foundation/foundation/interfaces/Interaction), locates all Compose roots within that view
hierarchy, and creates a new, scoped SemanticsNodeInteractionsProvider.

## Code Examples
### onRootWithViewInteractionBasicSample
```kotlin
fun onRootWithViewInteractionBasicSample() {
    // Select the "Header" View container
    val headerInteraction = onView(withId(header_id))
    // Scope the Compose interaction to only the Header
    composeTestRule
        .onRootWithViewInteraction(headerInteraction)
        .onNodeWithContentDescription("Settings")
        .performClick()
}
```
### onRootWithViewInteractionFragmentSample
```kotlin
fun onRootWithViewInteractionFragmentSample() {
    // Select the container for the Detail Fragment
    val detailContainerInteraction = onView(withId(detail_fragment_container_id))
    // Assert that the submit button exists/is enabled only in the detail fragment
    composeTestRule
        .onRootWithViewInteraction(detailContainerInteraction)
        .onNodeWithText("Submit")
        .assertIsEnabled()
}
```
### onRootWithViewInteractionRecyclerViewSample
```kotlin
fun onRootWithViewInteractionRecyclerViewSample() {
    // Select the specific View row containing "Item #5"
    val specificRowInteraction =
        onView(allOf(withId(recycler_item_root_id), hasDescendant(withText("Item #5"))))
    // Scope interaction to that specific row View
    composeTestRule
        .onRootWithViewInteraction(specificRowInteraction)
        .onNodeWithTag("fav_icon")
        .assertIsDisplayed()
        .performClick()
}
```
