【问题标题】:Espresso UI test on Android Floating Action Button menu itemsAndroid Floating Action Button 菜单项上的 Espresso UI 测试
【发布时间】:2016-11-28 11:45:53
【问题描述】:

我正在使用futuresimple 的Android Floating Action Button。我想使用 Espresso 创建与之交互的 UI 测试。 multiple_actions 只是打开浮动操作按钮 (FAB) 菜单的按钮。 draw_fab 是单击multiple_actions 时出现的浮动操作按钮之一。单击draw_fab 会启动一个新的android 活动,在这个活动中我希望按下一个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...
}

当我运行此测试时,我看到第一次单击有效并且显示了浮动菜单按钮。然而,点击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 使用?那或者我缺少关于 FAB 的一些特别之处或关于 Espresso 的一些基本内容。是哪个?

【问题讨论】:

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


    【解决方案1】:

    如果我查看我的 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来点击打开FABs菜单的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。对于屏幕上另一个与可见或不通过单击 FAB 相关的元素,这些值是 true

    【讨论】:

      猜你喜欢
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 2020-10-02
      相关资源
      最近更新 更多