【问题标题】:How to type text on a SearchView using espresso如何使用 espresso 在 SearchView 上键入文本
【发布时间】:2018-06-10 17:56:54
【问题描述】:

TypeText 似乎不适用于SearchView

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

给出错误:

在视图“id:../yt_search_box”上执行“输入文本(天气怎么样?)”时出错

【问题讨论】:

    标签: android searchview android-espresso


    【解决方案1】:

    对于遇到这个问题的人来说,解决方案是为类型 SearchView 编写一个 ViewAction,因为 typeText 只支持 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 文本框中的文本不可见,但会触发查询。至少你给了我一个自动化的方法来测试 SearchView。谢谢。
    • 如果要触发搜索,改成setQuery(text, true);
    【解决方案2】:

    @MiguelSlv 以上回答,转换为 kotlin

    fun typeSearchViewText(text: String): ViewAction {
        return object : ViewAction {
            override fun getDescription(): String {
                return "Change view text"
            }
    
            override fun getConstraints(): Matcher<View> {
                return allOf(isDisplayed(), isAssignableFrom(SearchView::class.java))
            }
    
            override fun perform(uiController: UiController?, view: View?) {
                (view as SearchView).setQuery(text, false)
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      你可以使用 Resource#getSystem 来获取视图

      Resources.getSystem().getIdentifier("search_src_text",
          "id", "android")
      
      
      onView(withId(Resources.getSystem().getIdentifier("search_src_text",
      "id", "android"))).perform(clearText(),typeText("enter the text"))
      .perform(pressKey(KeyEvent.KEYCODE_ENTER))
      

      【讨论】:

        【解决方案4】:

        这对我有用:

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

        【讨论】:

        • 下次您应该给出更多解释为什么您的解决方案应该有效。乍一看,我认为它与问题中的代码相同,但不是:D。密钥是使用的 ID R.id.search_src_text 访问通过单击您自己的视图打开的视图。
        【解决方案5】:

        基于 César Noreña respose 我设法在搜索字段中插入文本。 首先我点击了我的视图,然后在 android 搜索视图 id 上输入了文本。

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

        SearchView 的 X 按钮也有 ID search_close_btn

        onView(withId(R.id.search_close_btn)).perform(click()); // first time erase the content
        onView(withId(R.id.search_close_btn)).perform(click()); // second time close the SearchView
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多