A tool to launch an activity with intent in instrumented tests and assert composables in the activity, solving the limitation of ComposeTestRule that does not accept an intent as an input for launching activities. Read the medium article to understand why this is essential.
dependencies {
androidTestImplementation("io.github.aungthiha:compose-ui-test:1.0.1")
}
runAndroidComposeUiTest
uses ActivityScenario
internally to launch an Activity
runAndroidComposeUiTest<YourActivity>(
startActivityIntent = Intent(
ApplicationProvider.getApplicationContext(),
MainActivity::class.java
).putExtra("key", "value")
) {
// assert composables
// example assertion below
// onNodeWithText("hello").assertExists().assertIsDisplayed()
}
You can also directly use ActivityScenario
to launch an Activity the way you prefer
runAndroidComposeUiTest(
activityLauncher = {
ActivityScenario.launch<YourActivity>(
Intent(ApplicationProvider.getApplicationContext(), YourActivity::class.java)
.putExtra("key", "value"),
Bundle().apply {
putString("key", "value")
}
)
}
) {
// assert composables
}
This is a good way if you want to launch the activity the same way for all your test cases.
This createAndroidComposeRule
function is written using the idea from Michal Moczulski
@get:Rule
val composeTestRule = createAndroidComposeRule<YourActivity>(
Intent(ApplicationProvider.getApplicationContext(), MainActivity::class.java)
.putExtra("key", "value")
)