Android Espresso typeText into EditText in ActionBar

梦想的初衷 提交于 2020-01-12 10:06:05

问题


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?


回答1:


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




回答2:


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.




回答3:


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

onView(withId(android.support.design.R.id.search_src_text)).perform(typeText("example"), pressKey(KeyEvent.KEYCODE_ENTER));

Hope this was useful.




回答4:


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


来源:https://stackoverflow.com/questions/24927365/android-espresso-typetext-into-edittext-in-actionbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!