Flaky Android Espresso Test – Snackbar

1) All devices/emulators being tested have animations disabled.

2) I have an @BeforeClass which builds my Credentials object.

  • Android Dual Sim Emulator
  • DataBinding not working after Upgrade Android Studio 2.3
  • I am getting an error “The method show(FragmentManager, String)”
  • Have a disabled onClick?
  • RemotePlaybackClient, stop() not getting callback
  • How can I refresh the Gallery after I inserted an Image in android?
  • 3) I have an IntenServiceIdlingResource and an EventBusIdlingResource, registered in @Before.

    4) When the sign in button is clicked, the IntentService fires off. In this case, the server (a mocked server) is returning a 500 error. That information is posted back to the UI from the IntentService, via greenrobot’s EventBus, and a Snackbar is shown with the error message.

    Here’s the code for the test:

    public void a_userNamePasswordTest() throws Exception {
        // email input
        ViewInteraction userNameView = onView(withId(R.id.email));
        // verify it's on screen and enabled
        // set the username
        userNameView.perform(scrollTo(), replaceText(credentials.username), closeSoftKeyboard());
        // password input
        ViewInteraction passwordView = onView(withId(R.id.password));
        // verify it's on screen and enabled
        // set the password.
        passwordView.perform(scrollTo(), replaceText(credentials.password), closeSoftKeyboard());
        // sign in button
        ViewInteraction signInButton = onView(withId(R.id.email_sign_in_button));
        // verify the button
                isDisplayed(), isEnabled(), withText("Sign In"), withContentDescription("Sign In")
        // clickity click the button
        signInButton.perform(scrollTo(), click());
        // verify the snackbar text
        onView(withText(startsWith("Server Error: 500"))).check(matches(isDisplayed()));

    This is the exception I usually get:

    SignInExceptionTest > a_userNamePasswordTest[Nexus_6P_API_23(AVD) –
    6.0] FAILED
    android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: a string starting with
    “Server Error: 500”

    According to my logging, my idling resources are working. But looking at the timestamps for the logs, the exception is occurring about 5 seconds after the idling resources have gone idle.

    It seems like there’s a delay between when the resources go idle, and when it attempts to look for the view.

    Other possibly pertinent details:

    • minSdk: 20
    • compile & targetSdk: 25
    • buildTools: 25.0.2
    • support-library: 25.1.1
    • espresso-core: 2.2.2
    • gradle plugin 2.3.0-beta3

    How can I fix this test so it’s not flaky, besides inflating how long my snackbars are displayed?

  • Check if device has a camera?
  • Event for VideoView playback state or MediaController play/pause
  • Android AlarmManager stops when activity die
  • How can I include a button in a Toast notification?
  • AppCompat and Fragment not working
  • Restore default actionbar layout
  • One Solution collect form web for “Flaky Android Espresso Test – Snackbar”

    Espresso enforces a 5 second wait after an IdlingResource becomes active before it is checked again for being idle.

    This has 2 negative impacts in my case.

    First, it increases how long it takes the test to run. An extra 5 seconds (or multiple of 5) on every test can really add up.

    Second, since the snackbar is displayed immediately, and only shows for about 3.5 seconds, pretty much any time you’re waiting for an IdlingResource, the snackbar will be gone before Espresso realizes that the resource is idle, making it difficult to test.

    ConditionWatcher, described here:

    And code here:

    Provides a solution to both these issues. Rather than 5 seconds, it defaults to 250ms, and this value can be adjusted (setWatchInterval).

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.