我有以下Espresso测试:
@Test
fun testSearchItemFound() {
onView(withId(R.id.action_search)).perform(click())
onView(isAssignableFrom(EditText::class.java))
.perform(typeText("Harry Potter"),
pressImeActionButton())
// Check error message view is not displayed
onView(withId(R.id.error_msg)).check(matches(not<View>(isDisplayed())))
// Check the item we are looking for is in the search result list.
onData(allOf(`is`<Any>(instanceOf<Any>(String::class.java)),
withItemContent("Harry Potter")))
.check(matches(isDisplayed()))
}
第一行代码:
onView(withId(R.id.action_search)).perform(click())
在EspressoIdlingResource空闲时将正确执行。
但最后一行代码:
onData(allOf(`is`<Any>(instanceOf<Any>(String::class.java)),
withItemContent("Harry Potter")))
.check(matches(isDisplayed()))
当我们在MainFragment中有以下代码时将执行:
// The network request might be handled in a different thread so make sure Espresso knows
// that the app is busy until the response is handled.
EspressoIdlingResource.increment() // App is busy until further notice
model.movies.observe(this@MainFragment, Observer<PagedList<Movie>> {
// This callback may be called twice, once for the cache and once for loading
// the data from the server API, so we check before decrementing, otherwise
// it throws "Counter has been corrupted!" exception.
if (!EspressoIdlingResource.getIdlingResource().isIdleNow) {
EspressoIdlingResource.decrement() // Set app as idle.
}
adapter.submitList(it)
})
如果我禁用:
EspressoIdlingResource.increment() // App is busy until further notice
然后:
if (!EspressoIdlingResource.getIdlingResource().isIdleNow) {
EspressoIdlingResource.decrement() // Set app as idle.
}
第一行正常工作:
onView(withId(R.id.action_search)).perform(click())
有什么解决方案可以处理这种特殊情况吗?
答案 0 :(得分:0)
我添加了一种方法来检查IdealingResource是否应该增加:
if (shouldIncrementEspressoIdlingResource()) {
// The network request might be handled in a different thread so make sure Espresso knows
// that the app is busy until the response is handled.
EspressoIdlingResource.increment() // App is busy until further notice
}
在我的一种情况下,例如,我有以下实现:
override fun shouldIncrementEspressoIdlingResource() =
(activity as SearchActivity).search_view.query.isNotEmpty()