簡體   English   中英

如何在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)

它有什么問題? 我怎樣才能以其他方式測試它?

這對我有用:

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

解決方案

方法沒問題。 正如 Chiu-Ki Chan 在她的教程中所寫的那樣,您可以“確定一個特定的觀點”。 但是你必須確保你導入了正確的工具欄:

import  android.support.v7.widget.Toolbar;

而不是:

import android.widget.Toolbar;

這是另一種選擇(和更慣用的方法):

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

如果您使用的是操作欄,而不是工具欄,請使用以下命令:

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

注意:要快速添加這些方法的導入,請將閃爍的光標放在未解析的方法上,然后執行 Android Studio ➔幫助查找操作➔ 搜索"show context action""show intention action" ➔ 單擊結果選項 ➔將出現一個彈出窗口 ➔ 單擊"Import static method ..." 您還可以為“顯示上下文操作”分配鍵盤快捷鍵。 更多信息在這里 另一種方法是在設置中啟用"Add unambiguous imports on the fly" ”。

我不記得是我自己寫的,還是我在某處找到的,但這是我檢查工具欄標題的方式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM