Espresso test stuck/inactive after perform(click()) on button in ViewAnimator

前提是你 提交于 2020-01-03 08:25:06

问题


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 🌚😄


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/38445701/espresso-test-stuck-inactive-after-performclick-on-button-in-viewanimator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!