17 April 2023 Leave a comment Tech-Help
Jetpack compose changed how we build and test Android apps quite a bit. In this blog post, we collect challenging questions around espresso, UI testing and jetpack compose.
How to test entering text in Jetpack compose TextField?
Let’s say you want to fill a jetpack compose text field and test whether the value was inserted.
How do we do that? Well, first, take care that you have assigned a test-tag modifier, like this:
Then, in your test case you can access the field like this to set a value and then check it:
But let’s say there is no way for you to modify the app source. In this case, it’s also possible to access a compose field via the label instead of a test tag:
In your test, assert the field like this:
How to test an app with Espresso, that has bits of Jetpack Compose?
You might have an app that is built with the standard Android SDK, but has a couple of fragments that are built with Jetpack Compose.
Your first approach might be to create a ComposeTestRule, but it will generate a blank ComponentActivity and display an empty screen.
The problem
This is a problem. You want to integrate with an existing Espresso test, so this approach won’t be suitable.
Alternatively, you could use createAndroidComposeRule(), but this will also rely on an ActivityTestRule underneath and launch the Activity, potentially altering the behavior of your existing test. 🤔
The solution
The solution is to use createEmptyComposeRule() . It will keep the Espresso test the same way it is, but also interact with some compose elements:
Alternative approach
You can also use a framework-agnostic mobile test automation tool such as Repeato to get your job done. Repeato does not care about which technology you used to built your app.
Check out our getting-started video to get an idea of how it’s done.