Espresso test stuck/inactive after perform(click()

2019-06-16 12:03发布

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

2条回答
我想做一个坏孩纸
2楼-- · 2019-06-16 12:13

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());

查看更多
孤傲高冷的网名
3楼-- · 2019-06-16 12:33

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.

查看更多
登录 后发表回答