12

I have enabled the home button to return to the previous view. Simply, doing this:

getActionBar().setDisplayHomeAsUpEnabled(true);

I'm using the last version of com.android.support:appcompat-v7:21.0.2. However, when I use the below code it doesn't work throwing a Exception.

Espresso.onView(ViewMatchers.withId(android.R.id.home)).perform(ViewActions.click()); Espresso.onView(ViewMatchers.withId(R.id.home)).perform(ViewActions.click());

Exception:

com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: is <2131296261> ...

Jesús Castro
  • 2,061
  • 1
  • 22
  • 26
  • Hmm, interesting. I use the same code to perform an action on the home button: onView(withId(android.R.id.home)).perform(click()); Unfortunately we are still using appcompat-v7:20.0.+. Maybe this has something to do with new android toolbar? What Id do you retrieve, if you will inspect the view with Android Monitor? – Christopher Dec 17 '14 at 15:15
  • 2
    Apparently, there is no id with the new Android Toolbar, instead of we use "Navigate up" :(. – Jesús Castro Dec 17 '14 at 15:23

4 Answers4

18

It's possible to click on the button when you use the description from the resources.

onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click());

Works fine with appcompat-v7:23.1.1

Peter Fortuin
  • 5,041
  • 8
  • 41
  • 69
  • Keep in mind that `R.string.abc_action_bar_up_description` is private and can be changed anytime. But for the time being, this was the best option I could find. – Mokkun Aug 30 '16 at 02:38
  • I found solution without workaround. Here: val upButton = onView(allOf(instanceOf(ImageButton::class.java), withParent(withId(toolBar)))) – Alexei Jun 06 '19 at 08:04
6

I did the following workaround:

private String getString(int resId){
    return getInstrumentation().getTargetContext().getString(resId);
}

public void testUI() {   
    onView(withContentDescription(getString(R.string.navigation_drawer_open))).perform(click());
    onView(withContentDescription(getString(R.string.navigation_drawer_close))).perform(click());
}

Basically I use the content description attribute instead of the view id.

Christopher
  • 9,682
  • 7
  • 47
  • 76
0

I answer myself. According to the post on click home icon with espresso, It is not possible to specify an Id for the Home button in ActionBar, at least the with version 7 of the Support library, so we should use "Navigate up". But why?

This is the reason thanks to the Espresso error trace:

--------> ActionMenuView
          {
            id=-1, visibility=VISIBLE, width=144, height=168, has-focus=false, 
            has-focusable=true, has-window-focus=true, is-clickable=false, 
            is-enabled=true, is-focused=false, is-focusable=false, 
            is-layout-requested=false, is-selected=false, root-is-layout-requested=false,
            has-input-connection=false, x=936.0, y=0.0, child-count=1
          }
|
--------> ActionMenuItemView
          {
            id=2131296554, res-name=general, desc=, visibility=VISIBLE, 
            width=144, height=144, has-focus=false, has-focusable=true, 
            has-window-focus=true, is-clickable=true, is-enabled=true, 
            is-focused=false, is-focusable=true, is-layout-requested=false, 
            is-selected=false, root-is-layout-requested=false, 
            has-input-connection=false, x=0.0, y=12.0, text=, input-type=0, ime-target=false
          }
|
--------> ImageButton
          {
            id=-1, desc=Navigate up, visibility=VISIBLE, width=168, height=168,
            has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, 
            is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, 
            is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0
          }
|
Community
  • 1
  • 1
Jesús Castro
  • 2,061
  • 1
  • 22
  • 26
-1

I didn't know there was such a big difference using appCompat's action bar. As you guys wrote in the comments the "navigate up" really works. For those who, as myself, couldn't reckon how to do it using espresso, here is the way:

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;

onView(withContentDescription("Navigate up")).perform(click());

Depracated for AppCompat v7

In my project I am using Robotium and Espresso together. For home button click I use Robotiums Solo#clickOnActionBarHomeButton. It's more than 1 line like in click home icon with espresso but you don't have to specify the title. (It may be useful in some special cases...). Anyways I decide which one to use according to SDK version:

public static void actionBarBack(Instrumentation instrumentation, final Solo solo, String actionBarText) {
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
        onView(allOf(withText(actionBarText), isDisplayed())).perform(click());
    }
    else {
        instrumentation.runOnMainSync(new Runnable() {
            @Override
            public void run() {
                solo.clickOnActionBarHomeButton();
            }
        });
    }
}

Here is the usage:

 Compatibility.actionBarBack(getInstrumentation(), solo, R.string.any);

It has to be wrapped in runOnMainSync because Robotium testing framework is a little bit lazier and if you have asserts, e.g. for action bar title, right after the statement it will still not be back. But may try only with solo.clickOnActionBarHomeButton();. It could probably work for you.

Community
  • 1
  • 1
Amio.io
  • 20,677
  • 15
  • 82
  • 117