Android浮动操作按钮菜单项

时间:2016-11-28 11:45:54

标签: android android-espresso floating-action-button

我正在使用futureimple' Android Floating Action Button。我想使用Espresso创建与之交互的UI测试。 multiple_actions只是打开浮动操作按钮(FAB)菜单的按钮。 draw_fab是您点击multiple_actions时显示的浮动操作按钮之一。单击draw_fab将启动一个新的Android活动,在此活动中,我希望按下标识为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...
}

当我运行此测试时,我看到第一次单击工作,浮动菜单按钮显示。但是,点击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 FAB,isDisplayed传递并且所有日志输出都为真。然后在下一段Espresso中点击multiple_actions,所有输出都是真的。因此,就像执行点击multiple_actions以显示draw_fab点击对测试传递没有任何影响一样。这不应该是怎么回事?

我现在的预感是,我用于浮动动作按钮的回购根本不支持Espresso使用?那个或那里有一些关于FAB的特别之处或关于Espresso的一些基本信息我都不知道。这是什么?

1 个答案:

答案 0 :(得分:2)

如果我查看我的activity_home.xml活动我试图测试我有以下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"/>

所以我自然认为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不可见或不可见

相关问题