2

对于使用 Google Espresso 测试框架测试软件,我有以下问题:

在程序启动时,启动屏幕会启动并初始化整个应用程序。在此之后,我启动了一个请求输入的活动。

在 Espresso 中,应用程序启动并且测试从以下代码开始: onView(withId(R.id.chooseBookTitle)).perform(click());

这会崩溃,因为显示屏仍然显示初始屏幕,并且选择书标题仅在之后可见。如何防止 Google-Espresso 在该键出现之前点击它?

(我不想插入等待循环,但要让它保持事件驱动。在更糟糕的情况下,我会回到 Robotium)

4

2 回答 2

1

请检查 IdlingResource 接口:

正如 Stefano Dacchille 所提到的(参见下面的示例),您必须创建一个等待变为空闲的空闲资源实现:

public class WaitForSomethingResource implements IdlingResource {

ResourceCallback mResourceCallback;

private boolean isIdle;

@Override
public String getName() {
    return WaitForSomethingResource.class.getName();
}

@Override
public void registerIdleTransitionCallback(
        ResourceCallback resourceCallback) {
    mResourceCallback = resourceCallback;
}

@Override
public boolean isIdleNow() {
    return isIdle;
}

/**
 * Register an listener, use an event bus or something 
 * else to get notified about any change you want to track.
 */
public void onProgressChanged() {
    isIdle = true;

    if (isIdle && mResourceCallback != null) {
        mResourceCallback.onTransitionToIdle();
    }
}

}

之后,您必须在测试 setUp() 或 @before 方法中注册 IdlingResource 实现,方法是:

Espresso.registerIdlingResource(waitForSomethingResource)

示例: http ://dev.jimdo.com/2014/05/09/wait-for-it-a-deep-dive-into-espresso-s-idling-resources/

API 文档: http: //developer.android.com/reference/android/support/test/espresso/IdlingResource.html#isIdleNow()

于 2015-06-23T07:11:45.273 回答
0

From what I see, you should open directly the activity that contains R.id.chooseBookTitle, otherwise your view will never be on the screen so your test will always fail because Espresso can't find the specified view.

Otherwise, I use Thread.sleep(...) before I do some tests to make the tests wait. Try it before calling onView(withId(R.id.chooseBookTitle)).perform(click()); and see if it's working.

Good luck!

于 2016-02-17T16:45:22.097 回答