如何使用浓缩咖啡在SearchView上键入文本 [英] How to type text on a SearchView using espresso

查看:78
本文介绍了如何使用浓缩咖啡在SearchView上键入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TypeText似乎不适用于SearchView.

onView(withId(R.id.yt_search_box))
            .perform(typeText("how is the weather?"));

给出错误:

在具有id:../yt_search_box"的视图上执行键入文本(天气如何)"错误

Error performing 'type text(how is the weather?)' on view 'with id:../yt_search_box'

推荐答案

对于同样遇到此问题的任何人,解决方案是为SearchView类型编写ViewAction,因为typeText仅支持TextEditView

For anyone that bump into this problem too, the solution is to write a ViewAction for the type SearchView, since the typeText only supports TextEditView

这是我的解决方法:

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

这篇关于如何使用浓缩咖啡在SearchView上键入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆