Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 425
Stefan Ionescu Avatar asked Nov 08 '22 19:11

Stefan Ionescu


1 Answers

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

 }
like image 109
Stefan Ionescu Avatar answered Nov 15 '22 07:11

Stefan Ionescu