简体   繁体   English

如何在android仪器测试中检查工具栏标题?

[英]How to check Toolbar title in android instrumental test?

I found great instrumental test tutorial on YT Advanced Android Espresso .我在 YT Advanced Android Espresso上找到了很棒的乐器测试教程。 I took code from there with small adjustment to my needs.我从那里获取了代码,并根据我的需要进行了小幅调整。

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)));
}

Unfortunatelly it does not work for me.不幸的是,它对我不起作用。 Test failed with:测试失败:

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)

What is wrong with it?它有什么问题? How can I test it in other way?我怎样才能以其他方式测试它?

This works for me:这对我有用:

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

SOLUTION解决方案

The method is fine.方法没问题。 As Chiu-Ki Chan wrote in her tutorial you can "pinpoint that one particular view".正如 Chiu-Ki Chan 在她的教程中所写的那样,您可以“确定一个特定的观点”。 BUT you have to make sure you imported proper Toolbar:但是你必须确保你导入了正确的工具栏:

import  android.support.v7.widget.Toolbar;

instead of:而不是:

import android.widget.Toolbar;

这是另一种选择(和更惯用的方法):

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

If you're using an ActionBar, not a Toolbar, use this:如果您使用的是操作栏,而不是工具栏,请使用以下命令:

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

Note: To quickly add the imports for these methods, put the blinking cursor on the unresolved method, then do Android Studio ➔ HelpFind Action ➔ search for "show context action" or "show intention action" ➔ click on the result option ➔ A popup window will appear ➔ click on "Import static method ..." .注意:要快速添加这些方法的导入,请将闪烁的光标放在未解析的方法上,然后执行 Android Studio ➔帮助查找操作➔ 搜索"show context action""show intention action" ➔ 单击结果选项 ➔将出现一个弹出窗口 ➔ 单击"Import static method ..." You can also assign a keyboard shortcut to "Show Context Actions".您还可以为“显示上下文操作”分配键盘快捷键。 More info here . 更多信息在这里 Another way is to enable "Add unambiguous imports on the fly" in the Settings.另一种方法是在设置中启用"Add unambiguous imports on the fly" ”。

I don't remember if I wrote this myself, or if I found it somewhere, but this is how I check toolbar titles:我不记得是我自己写的,还是我在某处找到的,但这是我检查工具栏标题的方式:

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);
        }
    };
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM