2

I have an activity that fires an asynchronous request to a server. Right when it is fired, I show a progress bar. When a result is returned to the activity, I set the progressbar to View.GONE.

I have two tests right now. One for testing trying to login with wrong credentials which works perfectly :

@Test
public void tryLogin__withWrongPassword() {
    onView(withId(R.id.loginEmail))
            .perform(typeText("em@ail.com"), closeSoftKeyboard());
    onView(withId(R.id.loginPassword))
            .perform(typeText("123456789"), closeSoftKeyboard());
    onView(withId(R.id.loginSubmit))
            .perform(click());
    onView(withId(R.id.loginProgressBar))
            .check(matches(isDisplayed()));

    onView(isRoot()).perform(waitFor(3000));

    onView(withId(R.id.loginProgressBar))
            .check(matches(not((isDisplayed()))));
    onView(withId(R.id.loginPassword))
            .check(matches(hasErrorText("Wrong password")));
}

And one that I test with no internet connectivity that does not work :

@Test
public void tryLogin__withoutInternet() {
    onView(withId(R.id.loginEmail))
            .perform(typeText("em@ail.com"), closeSoftKeyboard());
    onView(withId(R.id.loginPassword))
            .perform(typeText("123456789"), closeSoftKeyboard());
    onView(withId(R.id.loginSubmit))
            .perform(click());
    onView(withId(R.id.loginProgressBar))
            .check(matches(isDisplayed()));

    onView(isRoot()).perform(waitFor(3000));

    onView(withId(R.id.loginProgressBar))
            .check(matches(not((isDisplayed()))));

    onView(withText("Network error, try again later"))
            .inRoot(withDecorView(not(mActivityRule.getActivity().getWindow().getDecorView())))
            .check(matches(isDisplayed()));
}

The test fails because the progressbar in this case is shown for a fraction of a second (because the error is almost immediately dispatched from the viewmodel) and seemingly Espresso cannot catch it while it is still loading.

My question is how could this potentially be fixed in my test? I have read somewhere that Espresso cannot test progress bars and the advice was to use UIAutomator instead, however I just started with Instrumented tests and chose Espresso and it is difficult for me to find the ideal tool for this case. Moreover, it seems to be working just fine when the progressbar appears for a bit more than half a second (in my first test for example)

P.S. the waitFor(long millis) method is a utility add-on method I made to force Espresso to wait for a specified amount of time before checking something (enforcing a timeout as a quality requirement)

Edit for clarification : My main question here is if anyone has an idea why the visibility is not caught when it is active for less than an amount of time vs when it lasts for more than half a second, even if the check is done immediately after the perform(click()) call.

  • Possible duplicate of [Testing progress bar on Android with Espresso](https://stackoverflow.com/questions/35186902/testing-progress-bar-on-android-with-espresso) – Martin Zeitler Aug 25 '18 at 19:07
  • Not a duplicate. Funny you should mention this, since it is the post I am referring to above that does not help so as to prevent similar answers. – John Gkikas Aug 25 '18 at 19:10
  • your failure to apply the provided workaround does not make it any less of a duplicate; if there were eg. one custom `ViewMatcher`, which would be able to match a `Progressbar` properly (or almost properly), it would have been rather an interesting question. – Martin Zeitler Aug 25 '18 at 20:19
  • i am not even having the same problem as the person in the referred question. the question itself is also different. In his/her case it doesn't even work, and the setup is different (I am introducing a waiting mechanism in a different way). Check clarification edit. – John Gkikas Aug 25 '18 at 21:54
  • 1
    you possibly should read the answers and not only the question and you would realize why I won't retract my close vote ...forget about `Espresso` and just use `UiAutomator` instead. a custom `ViewMatcher` wouldn't even work, by the way `Espresso` works - and why it fails for `Progressbar`. – Martin Zeitler Aug 25 '18 at 22:13

0 Answers0