在 ViewAnimator 中的按钮上执行(单击())后,Espresso 测试 stuck/inactive

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

问题: 我在 运行ning 我的 Espresso 测试时遇到问题,在登录按钮上调用 perform(click()) 方法后,测试保持 运行ning 但不会继续,直到45 秒过去,测试自动失败。同时,登录正常进行。

上下文: 我有一个 Activity 并排有两个片段,右边的片段处理用户名和密码 EditTexts 以及登录按钮。这个片段是用一个 ViewAnimator 和两个 LinearLayouts 作为子视图构建的,第一个 LinearLayout 有前面提到的元素,第二个有其他东西。

这是单击登录按钮时 UI 上发生的情况:

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

通过服务器验证后,我是这样处理 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);
        }
    });
}

浓缩咖啡测试

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

有谁知道如何在这种情况下成功测试?检测 ViewAnimator 何时更改就足够了,但测试会卡在点击上。

另外:那不是测试的开始,在之前显示的代码之前,我运行这个测试,它像梦一样工作:

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

感谢大家的帮助和任何建议

我有一个类似的问题,在我的例子中,它是一个加载到未显示的片段上的进度条。那个进度条没有被错误地关闭并且阻止了测试。正如您所描述的那样,在卡住 45 秒后测试失败,我得到了相同的结果。 总而言之,检查 UI 线程上是否没有动画 运行 作为进度条或任何其他。

我在使用 ListView 时遇到了同样的问题。

有趣的是,我注意到如果我手动尝试滚动屏幕​​,测试将会继续。添加此行解决了问题并恢复了测试。

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