Android Espresso 不等待片段加载 [英] Android Espresso doesn't wait for fragments to load

查看:23
本文介绍了Android Espresso 不等待片段加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Espresso 来测试 Android 应用,但遇到了一些问题.我有一个带有按钮的 Activity,该按钮以通常的方式替换片段:

I'm just getting started with Espresso for testing Android apps, but I'm having some trouble. I have an Activity with a button that replaces a fragment in the usual way:

public void onClick(View v) {
    final FragmentTransaction t = getFragmentManager().beginTransaction();
    t.setCustomAnimations(R.animator.fragment_slide_in_up, 0);
    t.replace(R.id.fragment_container,
        LogInFragment.newInstance(), LogInFragment.TAG)
        .addToBackStack(LogInFragment.TAG);
    t.commit();
}

在我的测试中,我点击了发送新片段的按钮,然后检查新片段是否可见.

In my test, I click the button that sends in the new fragment, and then just check that the new fragment is visible.

onView(withId(R.id.login_btn))
    .perform(click());
Thread.sleep(500);
onView(withId(R.id.email_input))
    .check(matches(isDisplayed()));

如果我从测试中删除 Thread.sleep(),即使我从活动代码中删除了 setCustomAnimations(),测试也会失败.

If I remove the Thread.sleep() from the test, the test fails, even if I remove the setCustomAnimations() from the Activity code.

按照说明,我的手机上的所有动画都已关闭.我的理解是 Espresso 知道 UI 线程状态,并且会等到一切准备就绪.但是如果我做一个 onView(withId(R.id.widgetOnNewFragment)) 它每次都会爆炸.每次显示新片段时,我都需要添加一个 Thread.sleep(500).

All animations are turned off on my phone, as per the instructions. My understanding is that Espresso knows about the UI thread status and will wait until everything is ready. But if I do a onView(withId(R.id.widgetOnNewFragment)) it blows up every time. I need to add a Thread.sleep(500) every time I show a new fragment.

我错过了什么吗?我想我不应该在我的测试代码中添加几十个 Thread.sleep().

Am I missing something? I think I should not have to add dozens of Thread.sleep() to my test code.

推荐答案

虽然 Espresso 框架等待"某些主 UI 线程活动,但动画需要 IdlingResource 方法(如 Honza 建议的那样).

While the Espresso framework 'waits' for certain main UI thread activities, animations require the IdlingResource approach (as Honza suggests).

有关于这个概念的很好的教程,甚至详细实现.

There are good tutorials on this concept, even detailed implementations.

实际上,由于动画很少会影响 UI 的功能测试,因此大多数开发人员都禁用了动画.IMO,这是一个反模式,因为它没有映射到真实的用户体验(大多数人购买手机并使用默认设置).这是您必须自己做出的决定.如果您想将动画作为功能要求的一部分,请查看 方式,但它要求您重新实现和重写您的测试.

In reality, because animations rarely factor in to functional testing of the UI, most developers disable animations. IMO, this is an anti-pattern because it doesn't map to the real user experience (most people buy a phone and use it with default settings). This is a decision you'll have to make yourself. If you want to include animations as part of your functional requirements, there are ways, but it requires you to reimplement and rewrite your tests.

这篇关于Android Espresso 不等待片段加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆