无法与咖啡
问题描述:
找到视图底部片我有一个包含CoordinatorLayout
有两个孩子一个Fragmant:一个MapView
(谷歌地图API)&一个RelativeLayout
,这最后的布局包含一些按钮,我想用Espresso
测试。无法与咖啡
因此,这里是我的测试:
@Test
fun randomButtonTest() {
try {
Thread.sleep(2000)
} catch (e: InterruptedException) {
e.printStackTrace()
}
// Find the button and perform a click
val appCompatImageButton = onView(
allOf(withId(R.id.random_poi), withContentDescription("Random Point of Interest"),
withParent(withId(R.id.design_bottom_sheet)),
isDisplayed()))
appCompatImageButton.perform(click())
}
这里是我的布局(我已删除的知名度所有的布局相关的参数):
推出我的仪器测试时,我得到以下错误:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with id: kade.com.accio:id/random_poi and with content description: is "Random Point of Interest" and has parent matching: with id: kade.com.accio:id/design_bottom_sheet and is displayed on the screen to the user)
是因为我的按钮位于底部工作表?为什么我找不到这个观点?
答
首先你必须加载片段(否则是没有测试片段),所以有两种方法
1)使用实例您FragmentManager片段对象和负载像
@Before // you can do it later
void setup(){
fragment = ...
getActivity().getFragmentManager().beginTransaction().add(id,farment).commit()
// or activityTestRule.getActivity()
// .getSupportFragmentManager().beginTransaction()
}
注意:如果你的意见外出屏幕的话,可以用withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)
代替isDisplayed()
这似乎是一个步骤上,谢谢,但我得到了以下错误: >错误执行“一次点击”视图上“动画或者在目标设备上启用转换。 用您共享的内容替换'isDsplayed()'后。 – Christopher
@Christopher确保遵循[设置您的测试环境](https://developer.android.com/training/testing/espresso/setup.html#set-up-environment)到'在您的设备上,在设置>开发人员选项,禁用设置:',并且当您的目标视图在屏幕上不可见时,您需要使用'withEffectiveVisibility' –