13

Problem: I'm having an issue while running my Espresso test, after the perform(click()) method is called on the login button, the test keeps running but doesn't go any further until 45 seconds pass and the test automatically fails. Meanwhile, the login happens normally.

Context: I have an Activity with two Fragments side by side, the fragment on the right handles the username and password EditTexts and also the login button. This fragment is built with a ViewAnimator and two LinearLayouts as children views, the first LinearLayout has the elements mentioned before and the second one has other stuff.

This is what happens on the UI when the login button is clicked:

    @Override
public void setUILoggingIn() {
    spinnerLogin.setVisibility(View.VISIBLE);
    loginButton.setEnabled(false);
    loginButton.setText(R.string.logging_in);
    usernameText.setEnabled(false);
    passwordText.setEnabled(false);
}

After authenticating with the server, this is how I handle the UI:

@Override
public void setUIAuthorized() {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            viewAnimator.showNext();
            // elements on the first child of the ViewAnimator
            loginButton.setEnabled(true);
            spinnerLogin.setVisibility(View.GONE);

            // elements on the second child of the ViewAnimator
            sendMessageButton.setEnabled(true); 
            chatInputText.setEnabled(true);
        }
    });
}

Espresso Test

ViewInteraction loginButton2 = onView(withId(R.id.login_button));
        loginButton2.perform(click());        // Test gets stuck here.
        loginButton2.check(doesNotExist());   // Never reached.

Does anyone know hot to successfully test on this kind of scenario? It would be enough to detect when the ViewAnimator changes, but the test gets stuck on the click.

Also: That was not the beginning of the test, before the code shown before, I run this test and it works like a dream:

ViewInteraction loginButton = onView(withId(R.id.login_button));

    /** Failed login **/

    loginButton.perform(click());

    ViewInteraction snackBarTextView = onView(
            allOf(withId(R.id.snackbar_text), withText("Login error"), isDisplayed()));
    snackBarTextView.check(matches(withText("Login error")));

Thank you all for your help and any advice

Alejandro H. Cruz
  • 507
  • 1
  • 7
  • 15

2 Answers2

16

I had a similar problem and in my case it was a progressbar that was loaded on a fragment that was not displayed. That progressbar was not dismissed by mistake and was blocking the tests. I had the same result with the tests failing after 45 seconds being stuck as you described. To sum up, check that there are no animations running on the UI thread as a progressbar or any other.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • Thank you for your answer @jeprubio. I tried removing the circular progressBar from the view, but the problem persists. I've also tried working with [Idling Resources] (https://www.youtube.com/watch?v=uCtzH0Rz5XU) but can't get it to work. Any other ideas? – Alejandro H. Cruz Jul 19 '16 at 22:39
  • I've solved it! There was another animation going on in a textview. Thank you again for your help. – Alejandro H. Cruz Jul 20 '16 at 17:42
1

I had the same issue while I was working with a ListView.

Interestingly, I noticed that the test would resume if I manually tried to scroll the screen. Adding this line solved the issue and resumed the test.

onView(withId(R.id.myListView)).perform(swipeUp());

  • 1
    Still the same problem with the latest Espresso, exactly as you described, swipeUp() really helps to unblock execution. Unfortunately, later I found, that if swipeUp() is used, following atPosition().perform(click()) can randomly fail to perform a click even if it does not give any exception, it just silently fails. – Radim Blazek Mar 17 '20 at 15:27