我正在使用 Espresso 来测试在搜索项目(例如自动完成)时出现的列表视图。直到用户在 SearchView 中输入内容后,列表视图才会出现。即我将 ListView 设置为View.VISIBLE
仅当用户在 SearchView 中输入内容时
当我尝试单击列表视图中的文本时,出现此错误。android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id:'
。使用 onData 不起作用。
添加人为延迟是可行的,但我不确定这是否是不好的做法,因为它似乎违背了诸如onData
etc.
我尝试过的:
My Code
这段代码可以工作,但我不想引入人为的延迟。
public pickSuggestion(int index){
/** artificial delay to allow list to appear.
This works but I shouldn't have to do this right? **/
SystemClock.sleep(1000);
onData(anything())
.inAdapterView(withId(R.id.list))
.atPosition(index)
.onChildView(withId(R.id.mTextView))
.perform(click());
}
添加人为延迟是可行的,但我不确定这是否不好
实践,因为它似乎违背了诸如此类的方法的目的
onData 等
Your error
设有Espresso
局限性。该框架需要在 UI 线程上运行,并且它会“等待”直到空闲。它不等待加载适配器数据,而是等待获取空闲资源
Check: http://dev.jimdo.com/2014/05/09/wait-for-it-a-deep-dive-into-espresso-s-idling-resources/ http://dev.jimdo.com/2014/05/09/wait-for-it-a-deep-dive-into-espresso-s-idling-resources/
空闲资源参考:https://developer.android.com/reference/android/support/test/espresso/IdlingResource.html https://developer.android.com/reference/android/support/test/espresso/IdlingResource.html
IdlingResource 文档:https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
计数空闲资源:https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource.html https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource.html
代码如下SystemClock.sleep(1000)
or Thread.sleep(1000)
这是一种不好的做法,因为更好的设备不需要这么多时间,而旧设备需要更多时间,因此您的代码可能不稳定,而不是快速和灵活。
解决方案是创建自己的Espresso IdlingResource
告诉 Espresso 何时可以执行测试而不会丢失数据和时间。
希望这会有所帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)