Testing Snackbar show with Espresso

爱⌒轻易说出口 提交于 2019-12-03 01:04:46
ksarmalkar

This worked for me, please try.

onView(allOf(withId(android.support.design.R.id.snackbar_text), withText("My text")))
            .check(matches(isDisplayed()));

If you use AndroidX, please use the following:

onView(withId(com.google.android.material.R.id.snackbar_text))
        .check(matches(withText(R.string.whatever_is_your_text)))

An alternative

private void checkSnackBarDisplayedByMessage(
        @StringRes int message) {
    onView(withText(message))
            .check(matches(withEffectiveVisibility(
                    ViewMatchers.Visibility.VISIBLE
            )));
}

I saw the previous answers but I thought this would be better.

@Test
public void onFabClick_shouldDisplaySnackbar() throws Exception {
  onView(withId(R.id.fab)).perform(click());

  // Compare with the text message of snackbar
  onView(withText(R.string.snackbar_message))
      .check(matches(isDisplayed()));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!