在仪器测试中测试搜索视图



我在我的动作栏中有一个搜索视图,我可以在其中按搜索按钮在动作栏中,写一些文本,然后单击软键板上的搜索按钮,然后应该显示列表回收器视图中的结果。

我想对此进行测试,以查看RecyClerview中的结果是否包含与我搜索的单词相同的单词(结果是电影列表)。

例如,如果我搜索"爱",结果是所有的电影,其中包含单词" love"的名字,例如" P.S.我爱你"

我正在尝试:

@Test
    public void testSearchToolbar() throws Exception {
        Instrumentation inst = new Instrumentation();
        onView(withId(R.id.action_search)).perform(click());
        onView(withId(android.support.design.R.id.search_src_text)).perform(typeText("Seven Samurai"));
        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_SEARCH);
        onData(withId(R.id.movie_title)).check(matches(isDisplayed()));
    }

我看到文本正在键入搜索视图中,但是除了该测试之外,该文本失败了。

有人可以帮助我这个

在2022年,我为我的情况这样做:

        onView(withId(R.id.action_search))
            .perform(click())
        onView(withId(R.id.searchView))
            .check(matches(isDisplayed()))
        onView(isAssignableFrom(EditText::class.java))
            .perform(typeText(query), pressKey(KeyEvent.KEYCODE_ENTER))
        Espresso.onView(ViewMatchers.withId(resId))
                .check(ViewAssertions.matches(CustomMatchers().recyclerViewSizeMatch(count)))

最新更新