5

I have a SearchView in my ActionBar that is inflated from XML

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_menu_search"
        android:showAsAction="always|collapseActionView"
        android:icon="@drawable/action_bar_search_icon"
        android:actionViewClass="android.widget.SearchView"/>
</menu>

I inflate it in my Fragment this way:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.search, menu);

    final MenuItem item = menu.findItem(R.id.action_menu_search);
    searchView = (SearchView) item.getActionView();
    searchView.setOnQueryTextListener(this);
}

And my test:

onView(withId(R.id.catalog_filter_indicator_header_text)).perform(click());

//enter text and wait for suggestions
onView(withId(R.id.action_menu_search)).perform(click());
onViewisAssignableFrom(AutoCompleteTextView.class)).perform(click(), typeText("test"));

It looks like that field looses focus when it starts typing. And I don't know why.

All views are found and typeText statement passes without a hitch, but text does not appear in the field. I also tried that with simple EditText and custom android:actionLayout but with the same results.

Is there something I'm missing?

Martynas Jurkus
  • 9,231
  • 13
  • 59
  • 101

5 Answers5

20

This code works for the SerachView in my ActionBar, as the SearchView is the only EditText in my Fragment :

onView(withId(R.id.action_search)).perform(click());    
onView(isAssignableFrom(EditText.class)).perform(typeText("test"), pressKey(KeyEvent.KEYCODE_ENTER));

KeyEvent.KEYCODE_ENTER is the code for pressing the submit button.

ByteWelder
  • 5,464
  • 1
  • 38
  • 45
JensJensen
  • 1,017
  • 1
  • 12
  • 25
  • My code does the same thing as yours. Do you run your tests on emulator or genymotion or actual devices? – Martynas Jurkus Oct 22 '14 at 10:54
  • Did you try it with EditTex instead of AutoCompleteTextView. I think I have tried it with AutoCompleteTextView before and that didn't work for me... My tests are running on actual devices and emulator. – JensJensen Oct 22 '14 at 12:51
  • 4
    `pressKey(KeyEvent.KEYCODE_ENTER)` does the same thing and is a bit more readable IMO. – vaughandroid Apr 15 '15 at 11:02
  • if you want a more specific class because you have more EditTexts in your screen you can use SearchView.SearchAutoComplete.class – Yair Kukielka Jan 21 '16 at 20:59
12

Search view has default id. You can get it in Espresso in this way.

onView(withId(androidx.appcompat.R.id.search_src_text)).perform(typeText("example"), pressKey(KeyEvent.KEYCODE_ENTER));

Prior to AndroidX, the ID was android.support.design.R.id.search_src_text

Hope this was useful.

vaughandroid
  • 4,315
  • 1
  • 27
  • 33
Lorenzo Camaione
  • 505
  • 3
  • 15
2

The id of the EditText in SearchView is search_src_text

onView(withId(R.id.action_menu_search)).perform(click());
onView(withId(R.id.search_src_text)).perform(typeText("test"));
Jaydeep Solanki
  • 2,895
  • 5
  • 36
  • 50
2

Finally, I decided to create my own custom action because I wasn't be able to typeText work. Here you are my code:

public static ViewAction setText(final String text){
    return new ViewAction(){
        @Override
        public Matcher<View> getConstraints() {
            return allOf(isDisplayed(), isAssignableFrom(TextView.class));
        }

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

        @Override
        public void perform(UiController uiController, View view) {
            ((TextView) view).setText(text);
        }
    };
}

This code is useful to write code in all children from TextView. F. ex, EditText.

I took this code from https://stackoverflow.com/a/32850190/1360815

Community
  • 1
  • 1
emaleavil
  • 591
  • 7
  • 14
  • Is there a reason you didn't use [`UiController.injectKeyEvent()`](http://developer.android.com/reference/android/support/test/espresso/UiController.html#injectKeyEvent(android.view.KeyEvent)) in order to more closely simulate actual typing? – Code-Apprentice Feb 08 '16 at 22:19
1

Accepted answer is good but it is not necessary anymore since the official Espresso library included the same functionality with replaceText function.

You can use it like below without a custom implementation

onView(withId(R.id.search_src_text)).perform(replaceText("test"))
tasomaniac
  • 10,234
  • 6
  • 52
  • 84