java – Espresso单击菜单项

前端之家收集整理的这篇文章主要介绍了java – Espresso单击菜单项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我创建的actionbar中有一个菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    menu.add(Menu.NONE,98,Menu.NONE,R.string.filter).setIcon(R.drawable.ic_filter_list_white_48dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    menu.add(Menu.NONE,99,R.string.add).setIcon(R.drawable.ic_add_white_48dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);


    getMenuInflater().inflate(R.menu.menu_main,menu);

    return true;
}

而menu_main.xml如下所示:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never"
        android:icon="@drawable/ic_settings_white_48dp"/>
</menu>

在Espresso中进行测试时,我想点击“添加”图标(menuId 99).我试过了

@Test
public void testAdd() {
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
    onView(withText(R.string.add)).perform(click());
}

但是如果NoMatchingViewException失败. (设置项目,这是直接在xml中定义的,我可以用相同的代码点击.)

这是针对targetSdkVersion 23和AppCompatActivity.工具栏的相关行为:

Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
if( getSupportActionBar() != null ) {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

而tool_bar.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:background="@color/ColorPrimary"
    android:elevation="4dp"
    tools:ignore="UnusedAttribute">
</android.support.v7.widget.Toolbar>

解决方法

更新:我没有看到最后一行,忽略我以前的回复,我试图修复它,我做错了.我真的不知道答案.
...setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)

您的问题由Espresso团队here解释:

Matching visible icons:

// Click on the icon - we can find it by the r.Id.
  onView(withId(R.id.action_save))
    .perform(click());

Clicking on items in the overflow menu is a bit trickier for the
normal action bar as some devices have a hardware overflow menu button
(they will open the overflowing items in an options menu) and some
devices have a software overflow menu button (they will open a normal
overflow menu). Luckily,Espresso handles that for us.

// Open the overflow menu OR open the options menu,// depending on if the device has a hardware or software overflow menu button.
  openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());

  // Click the item.
  onView(withText("World"))
    .perform(click());

所以我明白,两种选择都是正确的,但取决于你的具体情况.如果您测试一个按钮,通常知道它是否可见.

在你的情况下,按钮是可见的,因为有空间,使用withId像here是正确的:

import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;

@Test
public void testClickInsertItem() {
    openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
    onView(withId(R.id.action_insert)).perform(click());
}

this溢出物品也是正确的:

Yes,this is how it works in Espresso. The problem here is,that in
Android,the View representing the menu item doesn’t have the ID of
the menu item. So onView(withId(X)) just fails to find a View. I don’t
have a better recommendation than just using withText(). If you have
multiple views with the same text,using hierarchy for distinction
works.

现在我的问题是,当您在不同的设备上测试时,该怎么办,而且您不知道什么时候会有一个项目的空间.我不知道回应,也许结合了两个解决方案.

原文链接:https://www.f2er.com/java/126118.html

猜你在找的Java相关文章