【问题标题】:espresso onView inconsistent performanceespresso onView 性能不一致
【发布时间】:2016-02-17 15:20:55
【问题描述】:

我正在使用 Junit4 和 Espresso 进行测试。我在 espresso 测试中遇到了一个奇怪的问题 - 当我调用 onView 时,有时一切都按预期执行,但有时我的测试冻结,60 秒后我得到类似“android.support.test.espresso.AppNotIdleException: Looped 63 次迭代超过 60 秒。以下空闲条件失败 ASYNC_TASKS_HAVE_IDLED" 例如:

    onView(withId(R.id.zone_button_continue)).perform(click());
    onView(withId(R.id.loginButton)).check(matches(isDisplayed()));
    onView(withId(R.id.number)).check(matches(isDisplayed()));
    onView(withId(R.id.password)).check(matches(isDisplayed()));
    onView(withId(R.id.number)).perform(typeText(phoneNumber));
    onView(withId(R.id.password)).perform(typeText(password));
    onView(withId(R.id.loginButton)).perform(click());

一切都正确执行,除了最后一行 - 由于某种原因它找不到那个 loginButton,但它 100% 存在。我可以的

activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            activity.findViewById(R.id.loginButton).performClick();
             }
    });

这行得通。在我的大部分活动中,我都有类似的问题 - 有时 onView 工作有时不工作。

什么会导致这种不一致?我一直在与这个问题作斗争,这让我的测试非常沮丧和困难。到目前为止,我的结论是我的 gradle 依赖项有问题,或者我的应用程序中存在某种错误,导致 espresso 无法正常工作。但奇怪的是,在上面的示例中,除了最后一行之外,所有使用 onView 的东西都在工作,而且一切都发生在一个活动中,这让我怀疑这个错误是否在我的应用程序中。 这是我的 gradle 依赖项:

compile files('src/main/libs/guice-3.0-no_aop.jar')
compile files('src/main/libs/javax.inject-1.jar')
compile files('src/main/libs/roboguice-2.0.jar')
compile files('src/main/libs/junit-4.11.jar')
compile files('src/main/libs/hamcrest-core-1.3.jar')
compile files('src/main/libs/GeoLib.jar')
compile files('src/main/libs/GeoPolygons.jar')
compile files('src/main/libs/universal-image-loader-1.9.4.jar')
compile files('src/main/libs/javax.annotation-3.2-b06-sources.jar')
compile ('uk.co.chrisjenx:calligraphy:2.1.0'){
    exclude group:'com.android.support'
}
compile 'com.squareup:otto:1.3.5'
compile ('com.google.android.gms:play-services:6.5.87'){
    exclude group:'com.android.support'
}
compile 'com.android.support:support-annotations:22.2.1'
compile ('com.android.support:appcompat-v7:22.2.0'){
    exclude group:'com.android.support'
}
compile ('com.android.support:support-v4:22.2.0'){
    exclude group:'com.android.support', module:'support-annotations'
}
compile ('com.android.support:palette-v7:22.2.0'){
    exclude group:'com.android.support'
}
compile 'com.google.code.findbugs:jsr305:2.0.1'

compile 'com.nineoldandroids:library:2.4.0'
compile 'pl.charmas.android:android-reactive-location:0.4@aar'
compile 'io.reactivex:rxjava:1.0.3'
compile files('src/main/libs/FlurryAnalytics-6.1.0.jar')
compile 'com.github.castorflex.smoothprogressbar:library:1.1.0'
//    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile('com.android.support.test:runner:0.4.1') {
    exclude group: 'com.android.support', module: 'support-annotations'
}
// Set this dependency to use JUnit 4 rules
androidTestCompile('com.android.support.test:rules:0.4.1') {
    exclude group: 'com.android.support', module: 'support-annotations'
}
// Set this dependency to build and run Espresso tests
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.1') {
    exclude group: 'com.android.support', module: 'support-annotations'
}

androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2.1') {
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test.espresso:espresso-web:2.2.1') {
    // PackagingOptions modified to make this work
    exclude group: 'com.android.support', module: 'support-annotations'
}
testCompile 'junit:junit:4.11'
testCompile ('org.mockito:mockito-core:1.9.5'){
    exclude group: 'org.hamcrest'
}
compile 'org.mod4j.org.apache.commons:lang:2.1.0'

请帮我解决这个问题,我完全没有想法并坚持下去。

【问题讨论】:

  • 如果您的应用程序冻结,那么它的某些部分是否有可能处于无限循环中?你可以在调试模式下运行测试,看看它挂在哪里
  • 它挂在最后一行“onView(withId(R.id.login_button)).perform(click());”上,在此行执行后什么都没有,它在 60 秒后完成测试与:“android.support.test.espresso.AppNotIdleException:循环 63 次迭代超过 60 秒。以下空闲条件失败 ASYNC_TASKS_HAVE_IDLED”

标签: android junit4 android-testing android-espresso


【解决方案1】:

了解为什么您会收到带有消息“以下空闲条件失败 ASYNC_TASKS_HAVE_IDLED”的 AppNotIdleException 的最简单方法是:

  • 在 Android Studio 中以调试模式运行测试
  • 等到测试挂起
  • 在(默认)60 秒过去之前,暂停测试
  • 查看“线程”列表并查看名为“AsyncTask #1”的线程,依此类推。展开处于 RUNNING 状态的那个。在那里你可以看到代码的哪一部分让应用程序忙碌

这是我的案例的结果,所以我不得不为我的测试禁用 Facebook SDK 以使其再次稳定:

【讨论】:

  • 谢谢您,先生。这个对我有帮助。
  • 抛出异常还有其他原因吗?因为在我的情况下,所有 AsyncTask 都处于 WAIT 状态,但我仍然得到异常。
  • @EmilS。抱歉,我不知道,因为这是我正在处理的有关此错误的唯一案例。
  • 非常感谢!它帮助我停止浪费时间
【解决方案2】:

遇到了同样的问题,也是 facebook 库的错误。 切换到不同的仿真设备,一切正常!

【讨论】:

  • 您能否让我知道哪个模拟器设备为您工作,我的意思是 API 级别和设备类型。谢谢。
  • 当然,以下是完整规格:Nexus 5 API 27 Android 8.1 (Google Play) ABI: x86
猜你喜欢
  • 2021-08-16
  • 1970-01-01
  • 2017-08-01
  • 2012-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多