How to get a view from within Espresso to pass into an IdlingResource?

后端 未结 4 2010
无人及你
无人及你 2021-01-03 20:59

I essentially have a custom IdlingResource that takes a View a constructor argument. I can\'t find anywhere that really talks about how to implemen

4条回答
  •  轮回少年
    2021-01-03 21:21

    If you are using ActivityScenarioRule from androidx.test.ext.junit.rules (since ActivityTestRule "will be deprecated and eventually removed from library in the future"), you can get your Activity instance and call findViewById method:

    import androidx.test.ext.junit.rules.activityScenarioRule
    import androidx.test.ext.junit.runners.AndroidJUnit4
    
    @RunWith(AndroidJUnit4::class) {
    
        @get: Rule
        var testRule = activityScenarioRule()
    
        @Test
        fun mainTestCase() {
            testRule.scenario.onActivity { activity ->
                val view = activity.findViewById(R.id.view)
            }
        }
    }
    
    

提交回复
热议问题