48

I am trying to click the home icon in some Espresso tests via:

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

This works fine for Android > 3.0 - but fails for older versions as appcompat does not seem to use this id for this element then. What is a good approach to do what I want to do?

4

14 回答 14

87

To not depend on the app locale, you can use the code from Matt Logan by replacing "Navigate up" with R.string.abc_action_bar_up_description:

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

This helped me a lot because I have an app in more than 5 languages and I had to act like this.

于 2016-02-17T16:50:55.413 回答
33

Use the withContentDescription() Matcher:

onView(withContentDescription("Navigate up")).perform(click());
于 2014-11-12T23:07:23.877 回答
21

I had trouble navigating back from one Activity to another, but then I found top-level actions:

Espresso.pressBack();
于 2015-02-12T09:37:18.227 回答
14

I found a real solution to this issue. By using the hierarchyviewer I found that the toolbar looks like this: hierarchyviewer screenshot

This means we could match the hamburger icon (not back button) like this:

onView(withContentDescription("Open navigation")).perform(click());

But a better solution to me was to find out that the hamburger icon is the only ImageButton and a direct child view of the v7 Toolbar. So I wrote a helper method to match it:

public static Matcher<View> androidHomeMatcher() {
    return allOf(
        withParent(withClassName(is(Toolbar.class.getName()))),
        withClassName(anyOf(
            is(ImageButton.class.getName()),
            is(AppCompatImageButton.class.getName())
    )));
}

@Test
public void clickHamburgerIcon() throws Exception {
    onView(androidHomeMatcher()).perform(click());
    // ...
}

This solution is better because it should match the view no matter which locale you use in your test. :-)

EDIT: Note that Toolbar might be android.support.v7.widget.Toolbar or android.widget.Toolbar - depending on your use case!

EDIT: The support lib version 24.2.0 uses AppCompatImageButton instead of ImageButton, so I added it, too.

EDIT: You have to import the correct methods to get this to work. Here are the imports used:

import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;
于 2016-01-07T15:21:49.457 回答
5
Espresso.pressBack();

Or

onView(withContentDescription("Navigate up")).perform(click());
于 2016-01-06T04:20:06.177 回答
5

I was having problems with "Navigate up" in an emulator, this worked for me:

onView(isRoot()).perform(ViewActions.pressMenuKey());
于 2016-06-08T14:52:32.290 回答
4
public static Matcher<View> navigationIconMatcher() {
    return allOf(
            isAssignableFrom(ImageButton.class),
            withParent(isAssignableFrom(Toolbar.class)));
}

@Test
public void clickHamburgerIcon() throws Exception {
onView(navigationIconMatcher()).perform(click());
// ...
}

this works always!

于 2017-02-21T13:14:02.390 回答
3

If your intention is opening / closing the drawer, I recommend using the Espresso contrib library:

onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
于 2019-01-02T16:25:08.830 回答
2

To press back View:

onView(isRoot()).perform(pressBack());
于 2015-03-27T16:39:29.357 回答
1

Update: I couldnt find R.string.abc_action_bar_up_description, maybe it has to do with androidx. I'm not sure about this. Instead I used R.string.nav_app_bar_navigate_up_description The code looks like this:

onView(withContentDescription(R.string.nav_app_bar_navigate_up_description)).perform(click())
于 2020-07-03T14:13:05.623 回答
0
//click on the navigation up button to go back to the list
onView(withContentDescription(getToolbarNavigationContentDescription())).perform(click());

Methods:

private String getToolbarNavigationContentDescription() {
    return TestUtils.getToolbarNavigationContentDescription(
            activityTestRule.getActivity(), R.id.toolbar);
}

public static String getToolbarNavigationContentDescription(
        @NonNull Activity activity, @IdRes int toolbarId) {
    Toolbar toolbar = activity.findViewById(toolbarId);
    if (toolbar != null) {
        return (String) toolbar.getNavigationContentDescription();
    } else {
        throw new RuntimeException("No toolbar found.");
    }
}
于 2018-11-14T14:59:34.480 回答
0

onView(withContentDescription("Open navigation drawer")).perform(click())

this helped me

于 2020-04-20T19:50:45.503 回答
-2

Maybe you can call:

pressKey(KeyEvent.KEYCODE_HOME);
于 2017-06-29T10:07:25.813 回答
-3

Add onbackpress in your activity, and use:

onView(withContentDescription("Navigate up")).perform(click());
于 2016-08-11T04:06:50.183 回答