浓缩咖啡测试被后台线程阻止.应用程序不空闲异常:"AppNotIdleException". [英] Espresso test blocked with background thread. Application not idle Exception: "AppNotIdleException."

查看:53
本文介绍了浓缩咖啡测试被后台线程阻止.应用程序不空闲异常:"AppNotIdleException".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些后台线程不空闲,我的android espresso单元测试被阻止.我如何找出哪个线程正在阻止我的应用程序执行?

My android espresso unit test blocked due to some background thread not idle. How can i figure out which thread is blocking my application to execute?

        android.support.test.espresso.AppNotIdleException: Looped for 246 iterations over 60 SECONDS. The following Idle Conditions failed ASYNC_TASKS_HAVE_IDLED.
        at dalvik.system.VMStack.getThreadStackTrace(Native Method)
        at java.lang.Thread.getStackTrace(Thread.java:580)
        at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92)
        at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
        at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
        at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
        at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
        at android.support.test.espresso.Espresso.closeSoftKeyboard(Espresso.java:159)

推荐答案

开始时:

Espresso for Android是完美,快速的测试自动化框架,但是它有一个重要的限制-您只能操作在测试环境下的应用程序内部.

Espresso for Android is perfect and fast test automation framework, but it has one important limitation - you are allowed to operate only inside your app under test context.

发件人: http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

这意味着 Espresso 执行任何操作都需要在应用程序的主线程上进行操作.它检查UI线程何时为 idle(),如果不是,则等待直到UI线程再次空闲.

It means that Espresso for perform any action needs to operate on main thread of app. It checks when UI thread is idle() and if not it waits until UI thread would be idle again.

如果UI线程闲置时间不长,则会产生 Espressso IdlingResources 错误,例如

If UI Thread is not idle too long, it produces Espressso IdlingResources errors like AppNotIdleException, which means:

一个异常,指示即使在指定的持续时间后,应用程序仍未变为空闲状态.

An exception which indicates that the App has not become idle even after the specified duration.

要解决此问题,您需要创建自己的 IdlingResource 方法用于在UI线程为 idle()时说 Espresso .

To deal with this problem you need to create your own IdlingResource method to say Espresso when UI thread would be idle() for it.

让我们更深入地研究问题:

Let's take a look a bit deeper into problem:

Espresso引入了 IdlingResource 的概念,该概念很简单界面:

Espresso introduces the concept of IdlingResource, which is a simple interface that:

代表被测应用程序的资源这可能会导致在测试过程中发生异步后台工作执行

Represents a resource of an application under test which can cause asynchronous background work to happen during test execution

该界面定义了三种方法:

  • getName():必须返回标识空闲资源的非空字符串.
  • isIdleNow():返回空闲资源的当前空闲状态.如果返回true,则
    上的onTransitionToIdle()方法注册的ResourceCallback必须先前已被调用.
  • registerIdleTransitionCallback(IdlingResource.ResourceCallback回调):通常,此方法用于存储对
    的引用回调以通知它空闲状态的变化.

例如,一个空闲资源的实现,它等待完全加载到WebView中的页面将如下所示:

For example, an implementation for an idling resource that waits for a page to be fully loaded in a WebView will look something like this:

public class WebViewIdlingResource extends WebChromeClient implements IdlingResource {

    private static final int FINISHED = 100;

    private WebView webView;
    private ResourceCallback callback;

    private WebViewIdlingResource(WebView webView) {
        this.webView = checkNotNull(webView,
                String.format("Trying to instantiate a \'%s\' with a null WebView", getName())));
        // Shall we save the original client? Atm it's not used though.
        this.webView.setWebChromeClient(this);
    }

    @Override public void onProgressChanged(WebView view, int newProgress) {
        if (newProgress == FINISHED && view.getTitle() != null && callback != null) {
            callback.onTransitionToIdle();
        }
    }

    @Override public void onReceivedTitle(WebView view, String title) {
        if (webView.getProgress() == FINISHED && callback != null) {
            callback.onTransitionToIdle();
        }
    }

    @Override public String getName() {
        return "WebView idling resource";
    }

    @Override public boolean isIdleNow() {
        // The webView hasn't been injected yet, so we're idling
        if (webView == null) return true;
        return webView.getProgress() == FINISHED && webView.getTitle() != null;
    }

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

创建自己的自定义空闲资源后,需要通过致电向Espresso注册 Espresso.registerIdlingResource(webViewIdlingResource).

After creating your own custom idling resource, it needs to be registered with Espresso by calling Espresso.registerIdlingResource(webViewIdlingResource).

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

如果无法正常使用,请尝试与 Espresso 一起使用另一个名为[ uiatomator 的Google测试框架,这可能会帮助您解决此问题.

If it won't work try to use along with Espresso another Google test framework called [uiatomator, which may help you to deal with this problem.

希望这会有所帮助

这篇关于浓缩咖啡测试被后台线程阻止.应用程序不空闲异常:"AppNotIdleException".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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