Jetpack compose testing : how to getcurrent activity after starting new activity from testrule

532 Views Asked by At

How to get current activity ( newly started activity ) , started from ui testing. Is there anyway to get its instance using composeTestRule

1

There are 1 best solutions below

3
BenjyTec On

Usually you would write seperate test methods / a separate test class that verifies the behaviour of the newly started Activity in isolation. So I don't think there is a way to actually access properties or the context of a newly started Activity.

If you only want to check whether a new Activity was successfully started after a button press, try out the following code:

@Test
fun testStartActivity() {
    Intents.init()
    composeTestRule.setContent {
        MyAppTheme {
            MainScreen()
        }
    }
    composeTestRule.onNodeWithText("start Activity").performClick()
    intended(hasComponent(MySecondActivity::class.java.name))
    Intents.release()
}

Check out the documentation for the intended function.
If you want to test whether intent extras are passed properly, have a look at this question.