Migrate androidTest to Compose

641 Views Asked by At

We are in the process of migrating our Android app to Jetpack Compose.

In our legacy Views-based app, we have a lot of androidTest tests, running on the emulator, using Espresso.

Most of our tests heavily use the Espresso withId() matcher. But of course since Composables don't have ids as Views do, we have a problem. Is there a way to assign ids to composables, and reuse existing Espresso tests using withId()? If it's not possible or somehow a bad approach, what would be the right way to search for a specific UI element on screen and click on it for instance?

Thank you for your help

1

There are 1 best solutions below

1
On BEST ANSWER

You can apply the testTag modifier to your composables.
Something like:

   Button(modifier = Modifier.testTag("myButton"), onClick = {}) {
         Text("myButton")
   }

and then you can use it with:

   rule.onNodeWithTag("myButton")
        .assertIsEnabled()