Stefan Ionescu
Stefan Ionescu

Reputation: 129

Android Espresso how to get EditText from custom view and then typeText

I'm trying to write an Espresso test that will typeText in a custom SearchView. The custom SearchView is here:

https://github.com/MiguelCatalan/MaterialSearchView/tree/develop/library/src/main/res/layout

I'm using it in my MainActivity. If I directly call 'typeText' on this SearchView, it throws an error because I actually need to get the EditText from this SearchBar.

The EditText is in search_view.xml

I think the Espresso command should look like:

onView(get EditText from SearchView).perform(typeText("chicken"));

How should I get the EditText reference?

Upvotes: 5

Views: 905

Answers (1)

Stefan Ionescu
Stefan Ionescu

Reputation: 129

Doesn't matter, I found a solution.

I got my 'search' view in the Unit test and then I started looping through its children.

When I found the EditText, then I called onView.

Here's the code. Hope it will help someone:

search = (SearchBox) mActivityRule.getActivity().findViewById(R.id.searchbox);

    for( int i = 0; i < search.getChildCount(); i++ )

        if( search.getChildAt( i ) instanceof EditText) {


        onView(withId(search.getChildAt(i).getId())).perform(typeText("soup" + '\n'));

 }

Upvotes: 1

Related Questions