How to press on searchview the action button in espresso

Issue

Advise, how to press search button using espresso on the keyboard for after typing text request

//Fragment class
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.search_menu, menu);
    searchMenuItem  menu.findItem(R.id.action_search);
    if (searchMenuItem ! null) {
        searchView  (SearchView) MenuItemCompat.getActionView(searchMenuItem);
        searchView.setOnQueryTextListener(this);
        searchView.setQueryHint("Search");
    }
    super.onCreateOptionsMenu(menu, inflater);
}

class from androidTest is:

public class GridFragmentTest {

    private final static String TAG  GridFragmentTest.class.getSimpleName();

    @Rule
    public ActivityTestRule activityTestRule  new ActivityTestRule(MainActivity.class);

    @Test
    public void GridFragmentRecycleViewTest(){
        IdlingResource componentIdlingResource  getIdlingResource();
        Espresso.registerIdlingResources(componentIdlingResource);
        Log.d(TAG, "GridFragmentRecycleViewTest()");
        onView(withId(R.id.action_search)).perform(click());
        onView(isAssignableFrom(SearchView.class)).perform(typeSearchViewText("lord")).perform(pressImeActionButton());
        EspressoIdlingResource.increment();
        onView(withId(R.id.recycler_view)).check(new RecyclerViewItemCountAssertion(10));
    }

    public static ViewAction typeSearchViewText(final String text){
        return new ViewAction(){
            @Override
            public Matcher<View> getConstraints() {
                //Ensure that only apply if it is a SearchView and if it is visible.
                return allOf(isDisplayed(), isAssignableFrom(SearchView.class));
            }

            @Override
            public String getDescription() {
                return "Change view text";
            }

            @Override
            public void perform(UiController uiController, View view) {
                ((SearchView) view).setQuery(text,false);
            }


        };
    }
}

The is a PerformException for the code:

   .perform(pressImeActionButton()

Exception text is:

androidx.test.espresso.PerformException: Error performing 'androidx.test.espresso.action.EditorAction@9c9ab29' on view 'is assignable from class: class androidx.appcompat.widget.SearchView'.
at androidx.test.espresso.PerformException$Builder.build(PerformException.java:84)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:85)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:57)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:316)
at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:177)
at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:118)

Solution

to get the view, you can use the resources.getSystem().getIdentifier method to get the search text field, after that apply the following methods

clearText() typeText(“bla bla bla”)

onView(withId(Resources.getSystem().getIdentifier("search_src_text",
"id", "android"))).perform(clearText(),typeText("enter the text"))
.perform(pressKey(KeyEvent.KEYCODE_ENTER))

Answered By – Ahmed Garhy

Leave a Comment