Testing Snackbar show with Espresso

前端 未结 3 1564
陌清茗
陌清茗 2021-02-05 00:28

Is there a way to test using Espresso that the snackbar shows up with the right text?

I have a simple call to create a snackbar

Snackbar.make(mView, \"My         


        
相关标签:
3条回答
  • 2021-02-05 01:13

    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()));
    }
    
    0 讨论(0)
  • 2021-02-05 01:19

    An alternative

    private void checkSnackBarDisplayedByMessage(
            @StringRes int message) {
        onView(withText(message))
                .check(matches(withEffectiveVisibility(
                        ViewMatchers.Visibility.VISIBLE
                )));
    }
    
    0 讨论(0)
  • 2021-02-05 01:22

    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)))
    
    0 讨论(0)
提交回复
热议问题