0

I'm trying to test a composable using .testTag that sits at the leaf level of the tree given the Composables call structure. The code is in the style of:

Top level Composable

@Composable
fun TopLevelComposable() {
    AComposable()
}

Child composable

@Composable
fun AComposable() {
    LeafComposable()
}

Leaf Composable

@Composable
fun LeafComposable() {
    Text(text = "stringOfText", modifier = Modifier.testTag("Text of AComposable"))
}

MainActivity.kt

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    TopLevelComposable()
            }
        }
    }
}

Though, I get an error:

java.lang.AssertionError: Failed: assertExists.
Reason: Expected exactly '1' node but could not find any node that satisfies: (TestTag = 'Text of AComposable')
However, the unmerged tree contains '1' node that matches. Are you missing `useUnmergedNode = true` in your finder?

Question

How can I enable androidTest to accept nested .testTag using Composables?

1 Answer 1

1

I don't see where you're calling .assertExists() in your example, but the error message has a helpful suggestion. In your instrumented test, add useUnmergedTree = true like so:

    composeTestRule.onNodeWithTag("Text of AComposable", useUnmergedTree = true)
        .assertExists()
        .assertIsDisplayed()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.