问题
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> ...
回答1:
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.
回答2:
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
回答3:
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
}
|
回答4:
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.
来源:https://stackoverflow.com/questions/27527988/how-do-i-test-the-home-button-on-the-action-bar-with-espresso