Java – espresso click menu item

I have a menu in the actionbar I created:

@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;
}

And menu_ main. The XML is as follows:

<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>

When testing in espresso, I want to click the "add" icon (menuid 99) I tried

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

But if nomatchingviewexception fails (setting item, which is defined directly in XML, I can click with the same code.)

This is for targetsdkversion 23 and appcompatactivity Toolbar related behaviors:

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

And tool_ bar. The XML is as follows:

<?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>

Solution

Update: I didn't see the last line, ignored my previous reply, I tried to fix it, I made a mistake I really don't know the answer

...setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)

Your question is explained here by the espresso team:

// Click on the icon - we can find it by the r.Id.
  onView(withId(R.id.action_save))
    .perform(click());
// 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());

So I understand that both options are correct, but it depends on your specific situation If you test a button, you usually know whether it is visible

In your case, the button is visible because there is space. Use withid like @ L_ 403_ 1 @ is correct:

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

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

But this overflow is also correct:

Now my question is, what should you do when you test on different devices, and you don't know when there will be a project space I don't know the response, maybe a combination of two solutions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>