Android上的Espresso UI测试浮动操作按钮菜单项
我正在使用futuresimple的Android Floating Action Button。我想用Espresso来创建与之交互的UI测试。 multiple_actions
只是打开浮动操作按钮(FAB)菜单的按钮。 draw_fab
是单击multiple_actions
时出现的浮动操作按钮之一。点击draw_fab
开始一个新的android活动,在这个活动中,我希望按一个id为componentMenuButton
的标准按钮(它本身会显示一个菜单)。Android上的Espresso UI测试浮动操作按钮菜单项
@Test
public void simpleCircuitTest() {
onView(withId(R.id.multiple_actions)).perform(click());
onView(withId(R.id.draw_fab)).perform(click());
onView(withId(R.id.componentMenuButton)).perform(click());
// other stuff...
}
当我运行这个测试时,我看到第一次点击工作和浮动菜单按钮显示。然而,点击draw_fab
的呼叫似乎没有任何影响,因此在达到点击componentMenuButton
的呼叫时,出现No views in hierarchy found matching: with id...
错误。
这是我困惑的地方。
@Test
public void simpleCircuitTest() {
onView(withId(R.id.draw_fab)).check(matches(isDisplayed()));
View v = mActivityRule.getActivity().findViewById(R.id.draw_fab);
Log.d(TAG, String.valueOf(v.getVisibility()==v.VISIBLE));
Log.d(TAG, String.valueOf(v.isShown()));
Log.d(TAG, String.valueOf(v.isEnabled()));
onView(withId(R.id.multiple_actions)).perform(click());
onView(withId(R.id.draw_fab)).check(matches(isDisplayed()));
Log.d(TAG, String.valueOf(v.getVisibility()==v.VISIBLE));
Log.d(TAG, String.valueOf(v.isShown()));
Log.d(TAG, String.valueOf(v.isEnabled()));
}
以上就是我试图搞清楚是怎么回事。当我运行这个测试时,即使我还没有点击multiple_actions
FAB,isDisplayed
通过,并且所有的日志输出都是真的。然后在下一部分中,Espresso点击multiple_actions
并再次输出所有信息。所以就好像点击multiple_actions
来点击draw_fab
点击对测试通过没有任何影响。这不是应该如何?
我的预感现在是我用于浮动动作按钮的回购单不支持Espresso使用?这或那里有一些特别的关于FABs或关于Espresso的基本信息,我错过了。这是什么?
如果我看我activity_home.xml
为我想测试我对晶圆厂以下活动(浮动操作按钮)
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="@+id/multiple_actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
fab:fab_addButtonColorNormal="@color/colorPrimary"
fab:fab_addButtonColorPressed="@color/white_pressed"
fab:fab_addButtonPlusIconColor="@color/white"
fab:fab_labelStyle="@style/menu_labels_style"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/draw_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/colorPrimary"
fab:fab_title="Draw Circuit"
fab:fab_colorPressed="@color/white_pressed"
fab:fab_icon="@drawable/ic_draw"/>
所以我很自然地认为ID multiple_actions可以用来点击打开FAB菜单的FAB。然而,看着我的项目values.xml
我有一个被我用FAB库生成我看到
<item name="fab_expand_menu_button" type="id"/>
这可能来自here。使用这个ID(我没有写),所有东西都是固定的。我假设FAB ID是硬编码的*。
@Test
public void simpleCircuitTest() {
onView(withId(R.id.fab_expand_menu_button)).perform(click());
onView(withId(R.id.draw_fab)).perform(click());
onView(withId(R.id.componentMenuButton)).perform(click());
// other stuff...
}
更有趣的是,当我使用multiple_actions
我想通了,它在屏幕上的其他地方按下(没有,因为没有视觉反馈实现的,但后来我放在HorizontalScrollView
占用整个屏幕,果然我看到Espresso单击它),这就意味着为什么这些日志值总是返回true。这些值对于屏幕上的另一个元素而言是真实的,该元素不会被视为可见或不是通过单击FAB