Sushil
Sushil

Reputation: 8488

Testing searchview in android - Instrumentation testing

I have a searchview in my action bar, where I can press on search button in action bar, write some text, then click on the search button on softkeyboard and then it should display the list of results in recycler view.

I want to test this to see if the results in recyclerview contains the same word as I searched for (The result is list of movies).

For example, if I search for "love" and the result is all movies with names which contains word "love" in it like "P.S. I love you"

I am trying this:

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

I see that the text is getting typed in searchview but beyond that test fails.

Can someone help me with this

Upvotes: 0

Views: 380

Answers (1)

Joe Dow
Joe Dow

Reputation: 1163

In 2022 I do this for my case:

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

Upvotes: 1

Related Questions