How to test if a fragment is visible with Espresso

寵の児 提交于 2019-12-23 20:09:13

问题


I wrote a very simple Activity with a Button. When the button is clicked, I will start a new Fragment. Now I want to test this logic in my Espresso UI Test. So I wrote this UI Test for my Activity

@RunWith(AndroidJUnit4::class)
@LargeTest
class MainMenuUiTest {

@get: Rule
val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)

 @Test
 fun switchToFragment() {
     onView(withId(R.id.btn)).perform()
 }

}

How can I test that my Fragment is displayed when the button is clicked?


回答1:


Try the following:

@Test
fun switchToFragment() {
    onView(withId(R.id.btn)).perform(click())
    onView(withId(R.id.fragment)).check(matches(isDisplayed()))
}

If you're just learning about Espresso, the official guide is a good place to start. Also, here's the Espresso cheat sheet.



来源:https://stackoverflow.com/questions/54491130/how-to-test-if-a-fragment-is-visible-with-espresso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!