android 工具测试中如何查看工具栏标题?

How to check Toolbar title in android instrumental test?

我在 YT Advanced Android Espresso 上找到了很棒的器测教程。我从那里获取代码,并根据我的需要进行了小幅调整。

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withChild;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.AllOf.allOf;

...

@Test
public void checkToolbarTitle() {
    String toolbarTitile = getInstrumentation().getTargetContext().getString(R.string.my_bus_stops);
    onView(allOf(isAssignableFrom(TextView.class), withParent(isAssignableFrom(Toolbar.class)))).check(matches(withText(toolbarTitile)));
}

不幸的是,它对我不起作用。测试失败:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (is assignable from class: class android.widget.TextView and has parent matching: is assignable from class: class android.widget.Toolbar)

有什么问题吗?我怎样才能用其他方式测试它?

解决方案

方法不错。正如 Chiu-Ki Chan 在她的 tutorial 中所写,你可以 "pinpoint that one particular view"。 但是您必须确保导入了正确的工具栏:

import  android.support.v7.widget.Toolbar;

而不是:

import android.widget.Toolbar;

这对我有用:

onView(allOf(instanceOf(TextView.class), withParent(withId(R.id.toolbar))))
    .check(matches(withText("toolbarTitile")));

我不记得这是我自己写的,还是我在某处找到的,但这是我检查工具栏标题的方式:

public static Matcher<View> withToolbarTitle(CharSequence title) {
    return withToolbarTitle(is(title));
}

public static Matcher<View> withToolbarTitle(final Matcher<CharSequence> textMatcher) {
    return new BoundedMatcher<View, Toolbar>(Toolbar.class) {
        @Override
        public boolean matchesSafely(Toolbar toolbar) {
            return textMatcher.matches(toolbar.getTitle());
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with toolbar title: ");
            textMatcher.describeTo(description);
        }
    };
}

这适用于所有情况。示例断言:onView(withId(R.id.toolbar)).check(matches(withToolbarTitle("title")));

如果您使用的是 ActionBar 而不是 Toolbar,请使用:

onView(allOf(instanceOf(TextView.class), 
     withParent(withResourceName("action_bar"))))
        .check(matches(withText("My ActionBar title")));

注意:要快速添加这些方法的导入,请将闪烁的光标放在未解析的方法上,然后执行 Android Studio ➔ HelpFind Action ➔ 搜索 "show context action""show intention action" ➔ 点击结果选项 ➔ 会出现一个弹出窗口 window ➔ 点击 "Import static method ...".您还可以为 "Show Context Actions" 分配键盘快捷键。 More info here。另一种方法是在设置中启用 "Add unambiguous imports on the fly"

这是一种替代方法(也是更惯用的方法):

onView(withId(R.id.toolbar)).check(matches(hasDescendant(withText(toolbarTitle))))