Search code examples
androidtestingandroid-jetpack-compose

Node not found with androidTests despite tagging


I'm rendering UI composables conditionally, based on UiState, however, tagging composables at the root with testTag doesn't make them findable in instrumentation tests.

Error:

 java.lang.AssertionError: Failed: assertExists.
 Reason: Expected exactly '1' node but could not find any node that satisfies: (TestTag = 'string')

The composable is passed as content parameter (content: @Composable () -> Unit) to another composable. The code is of the form:

 ParentComposable(
     <parameters>,
     content = {
         when (uiState) {
             is UiState.Loading -> {
                 LoadingScreen(
                     modifier =
  Modifier.testTag("string")
                 )
             }
         }
     }
 )

I'm mockking the UiState (it's stored in the ViewModel) so I'm sure I should be hitting the condition from this point of view. I even see the screen on my device and yet the test fails to find the node. Really at odds here how to fix this.


What I tried

LoadingScreen composable fails to be found. I tried the execution of assertion with useUnmergedTree set to true but that fails too.


Solution

  • I debugged further and realized the modifier that was passed down, ultimately was used by a composable I was conditionally not showing. Once I changed the tagging to the correct composables the tests succeeded.