Android Espresso startActivity测试

时间:2017-02-20 10:01:23

标签: android android-espresso

我有:

        onView(withId(R.id.login_email_textview)).perform(clearText(), closeSoftKeyboard());
        onView(withId(R.id.password_edittext)).perform(clearText(), closeSoftKeyboard());
        onView(withId(R.id.login_email_textview)).perform(typeText(email), closeSoftKeyboard());
        onView(withId(R.id.password_edittext)).perform(typeText(password), closeSoftKeyboard());
        onView(withId(R.id.email_sign_in_button)).perform(click());
        intended(hasComponent(new ComponentName(getTargetContext(), MainActivity.class)));

按下登录按钮后,将显示“加载”对话框,Async任务会调用服务器以检查凭据。在响应LoginActivity后,我测试了finish()并启动了MainActivity。

按下登录按钮后测试挂起。 还使用:

@Rule
public IntentsTestRule<LoginActivity> mActivityRule = new IntentsTestRule<>(LoginActivity.class);

同样在I / m调用startActivity()后的代码中,我在LoginActivity上调用了finish()。

在sign_in_button上我打电话给Volley请求。

2 个答案:

答案 0 :(得分:1)

如何解决您的问题

我认为,LoadingDialog正在显示动画ProgressBar

一起使用ProgressBarVolley实际上并不是一个坏主意。 ProgressBar会强制Espresso等待Volley在后​​台完成工作。

我认为,您的测试“挂起”,因为您不会忽略LoadingDialog。因此,要解决此问题,请在收到LoadingDialog的回复后撤消Volley。但请勿使用Espresso来解除LoadingDialog

更多信息

单独使用ProgressBarVolley时存在“已知问题”:

ProgressBar

的问题

如果LoadingDialog显示动画ProgressBar,则动画会阻止Espresso。动画使应用程序的主线程保持忙碌状态,Espresso等待它超时。

See here for workarounds

Volley

的问题

Espresso不了解Volley。如果Volley在后​​台线程中执行请求(我希望这样做),Espresso不会等待,并且您的测试可能会在Volley返回响应之前结束。

可以使用IdlingResource使Espresso等待任何事情。

答案 1 :(得分:0)

所以这是我提出的解决方案:

自定义IdlingResource:

public class VolleyIdlingResource implements IdlingResource {
private static final String TAG = "VolleyIdlingResource";
private final String resourceName;

// written from main thread, read from any thread.
private volatile ResourceCallback resourceCallback;

private Field requests;
private RequestQueue requestQueue;

public VolleyIdlingResource(String resourceName, Context context) throws SecurityException, NoSuchFieldException {
    this.resourceName = checkNotNull(resourceName);

    requestQueue = MyApplication.get(context).getRequestQueue();

    requests = RequestQueue.class.getDeclaredField("requests");
    requests.setAccessible(true);
}

@Override
public String getName() {
    return resourceName;
}

@Override
public boolean isIdleNow() {
    try {
        Set<Request> set = (Set<Request>) requests.get(requestQueue);
        int count = set.size();
        if (set != null) {

            if (count == 0) {
                resourceCallback.onTransitionToIdle();
            } else {
            }
            return count == 0;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}

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

然后在测试中:

VolleyIdlingResource volleyResources;
        try {
            volleyResources = new VolleyIdlingResource("VolleyCalls", mActivityRule.getActivity());
            registerIdlingResources(volleyResources);
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        intended(hasComponent(new ComponentName(getTargetContext(), MainActivity.class)));

基于https://github.com/bolhoso/espresso-volley-tests/blob/master/EspressoTest/src/com/example/espressovolley/test/MyTest.java