【发布时间】: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