3

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
gpo
  • 3,388
  • 3
  • 31
  • 53

1 Answers1

4

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()
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    Ah ah, thanks! I just found it by myself but you were faster to answer! BTW, looks like you are the one answering all my compose questions lately, thank's a lot!! – gpo Apr 24 '21 at 17:12