我正在使用 futuresimple 的 Android Floating Action Button .我想使用 Espresso 创建与之交互的 UI 测试。 multiple_actions
只是打开 float 操作按钮 (FAB) 菜单的按钮。 draw_fab
是您单击 multiple_actions
时出现的 float 操作按钮之一.点击 draw_fab
启动一个新的 android Activity ,在这个 Activity 中,我希望按下一个带有 id componentMenuButton
的标准按钮。 (它本身会弹出一个菜单)。
@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...
}
当我运行这个测试时,我看到第一次点击有效并且显示了 float 菜单按钮。然而调用点击draw_fab
似乎影响为零,因此我得到了 No views in hierarchy found matching: with id...
调用单击时出错 componentMenuButton
达到了。
这就是我感到困惑的地方。
@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
晶圆厂,isDisplayed
通过并且所有日志输出为真。然后在下一段 Espresso 中点击 multiple_actions
再次所有输出为真。所以就好像在执行点击 multiple_actions
调出draw_fab
单击对测试通过的影响为零。这不是应该的吗?
我现在的预感是,我用于 float 操作按钮的存储库根本不支持使用 Espresso?那或者有一些关于 FAB 的特别之处,或者我缺少的关于 Espresso 的一些基本知识。是哪个?
最佳答案
如果我查看我的 activity_home.xml
我正在尝试测试的 Activity ,我有以下 FAB( float 操作按钮)
<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可以用来点击打开FABs菜单的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 相关联
关于android - Android float 操作按钮菜单项上的 Espresso UI 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40843527/