在AsyncTask运行时进行Espresso测试后退按钮 [英] Espresso test back button while AsyncTask is running

查看:70
本文介绍了在AsyncTask运行时进行Espresso测试后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Espresso为我的应用程序编写UI测试.我想测试以下事实:如果在服务器请求进行过程中单击后退"按钮,则应用程序必须保留在该位置.

I'm writing UI tests for my application using Espresso. I'd like to test the fact that if I click the back button while a server request is in progress the app must remain where it is.

由于espresso的体系结构似乎无法实现,如果某些后台操作(例如AsyncTask)已被触发,则使测试执行等待.

It seems not to be possible due to the espresso's architecture that makes the tests execution wait if some background operation (like AsyncTask) has been fired.

所以,我该如何测试以下情况:

So, how can I test the following scenario:

  • 单击触发AsyncTask的按钮
  • 测试任务正在运行并且我按后退"按钮,应用程序停留在那儿吗?

有可能吗?

谢谢

推荐答案

这很棘手.使用AsyncTasks,您将无法在任务运行时使用Espresso. 而且,如果您将其他东西用于后台工作,Espresso不会等待,并且测试会在后台工作之前完成.

That's tricky. With AsyncTasks you cannot use Espresso while the tasks are running. And if you would use something else for background work, Espresso does not wait, and the test finishes before the background job.

一个简单的解决方法是在任务运行时在没有Espresso的情况下按下"返回按钮.因此,启动任务,调用Activity.onBackPressed(),任务完成后使用Espresso检查Activity仍然可见:

A simple workaround would be to "press" the back button without Espresso while the task is running. So, start the task, call Activity.onBackPressed() and after the task finishes use Espresso to check that the Activity is still visible:

// Start the async task    
onView(withId(R.id.start_task_button)).perform(click());

// Then "press" the back button (in the ui thread of the app under test)
mActivityTestRule.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        mActivityTestRule.getActivity().onBackPressed();
    }
});

// Then check that the Activity is still visible
// (will be performed when the async task has finished)
onView(withId(R.id.any_view_on_activity)).check(matches(isDisplayed()));

这篇关于在AsyncTask运行时进行Espresso测试后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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