7

As the title says, it fails some times, some others it success.

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view.

Expected: is displayed on the screen to the user
Got: "AppCompatTextView{id=2131492981, res-name=snackbar_text, visibility=VISIBLE, width=444, height=71, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=0.0, text=Network Error, input-type=0, ime-target=false, has-links=false}"

The first line of stack trace suggest espresso is unable to see the Snackbar on screen. But the second line states it is in fact seeing a Snackbar with visibility=VISIBLE and text=Network Error which is correct.

I'm confused, what's going on?

This is my test code:

activityRule.launchActivity(new Intent());
onView(withText("Network Error")).check(matches(isDisplayed()));

PS: it mostly fails when I run the whole test suit; but sometimes it also fails when I just run this test alone. Some other times it passes green, but there isn't any pattern, seems random.

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206

2 Answers2

5

Late! But I hope that it is helpful for others:

Testing Snackbar show with Espresso

private void checkSnackBarDisplayedByMessage(@StringRes int message) {
    onView(withText(message))
        .check(matches(withEffectiveVisibility(
            ViewMatchers.Visibility.VISIBLE
    )));
}
Community
  • 1
  • 1
J. Fdez
  • 156
  • 2
  • 5
1

I was getting similar issue. I was able to solve it by:

  1. Disabeling animations as described here.

  2. I was displaying the SnackBar after I fetched data from server so I also had to wait until the data was fetched. I managed to solve it with IdlingResource as described in this anwser.

Then I was able to successfully check the SnackBar.

I hope my points will help.

Community
  • 1
  • 1
David Novák
  • 1,455
  • 2
  • 18
  • 30