Espresso UI 测试 Android 浮动操作按钮菜单项
Espresso UI test on Android Floating Action Button menu items
我正在使用 futuresimple 的 Android Floating Action Button。我想使用 Espresso 创建与之交互的 UI 测试。 multiple_actions
只是打开浮动操作按钮 (FAB) 菜单的按钮。 draw_fab
是单击 multiple_actions
时出现的浮动操作按钮之一。单击 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...
}
当我 运行 这个测试时,我看到第一次点击有效并且显示了浮动菜单按钮。但是,调用 click draw_fab
的影响似乎为零,因此当调用 click 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?那或者有一些关于 FAB 的特别之处,或者我缺少的关于 Espresso 的一些基本知识。是哪个?
如果我查看 activity_home.xml
的 activity 我正在尝试测试我有以下 FAB(浮动操作按钮)
<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"/>
于是我很自然地想到idmultiple_actions可以用来点击打开FAB菜单的FAB。但是,查看我使用的 FAB 库生成的项目中的 values.xml
,我看到
<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。屏幕上的另一个元素的值是真实的,该元素与可见或不可见无关
我正在使用 futuresimple 的 Android Floating Action Button。我想使用 Espresso 创建与之交互的 UI 测试。 multiple_actions
只是打开浮动操作按钮 (FAB) 菜单的按钮。 draw_fab
是单击 multiple_actions
时出现的浮动操作按钮之一。单击 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...
}
当我 运行 这个测试时,我看到第一次点击有效并且显示了浮动菜单按钮。但是,调用 click draw_fab
的影响似乎为零,因此当调用 click 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?那或者有一些关于 FAB 的特别之处,或者我缺少的关于 Espresso 的一些基本知识。是哪个?
如果我查看 activity_home.xml
的 activity 我正在尝试测试我有以下 FAB(浮动操作按钮)
<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"/>
于是我很自然地想到idmultiple_actions可以用来点击打开FAB菜单的FAB。但是,查看我使用的 FAB 库生成的项目中的 values.xml
,我看到
<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。屏幕上的另一个元素的值是真实的,该元素与可见或不可见无关