Click home icon with Espresso

2019-01-23 10:42发布

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?

12条回答
Deceive 欺骗
2楼-- · 2019-01-23 11:12
Espresso.pressBack();

Or

onView(withContentDescription("Navigate up")).perform(click());
查看更多
家丑人穷心不美
3楼-- · 2019-01-23 11:14

Use the withContentDescription() Matcher:

onView(withContentDescription("Navigate up")).perform(click());
查看更多
萌系小妹纸
4楼-- · 2019-01-23 11:20

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

Espresso.pressBack();
查看更多
相关推荐>>
5楼-- · 2019-01-23 11:21
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!

查看更多
贼婆χ
6楼-- · 2019-01-23 11:22

Maybe you can call:

pressKey(KeyEvent.KEYCODE_HOME);
查看更多
相关推荐>>
7楼-- · 2019-01-23 11:28

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

onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
查看更多
登录 后发表回答