【问题标题】:Test text submit on SearchView when using Espresso使用 Espresso 时在 SearchView 上测试文本提交
【发布时间】:2019-10-03 23:12:45
【问题描述】:

如何在 androidTest 中向SearchView 提交一些文本?

onView(withId(R.id.search_src_text)).perform(typeText("text")) 对我不起作用 - 应用程序在此行崩溃

@Test
fun testSearchViewTextSubmit() {
    ActivityScenario.launch(MainActivity::class.java)

    onView(withId(R.id.search_action)).perform(click()) // ok 

    onView(withId(R.id.search_src_text)).perform(typeText("text")) // failed
}

androidx.test.espresso.NoMatchingViewException:层次结构中没有视图 找到匹配项:带有 id: com.example.test:id/search_src_text

【问题讨论】:

    标签: android junit android-espresso searchview android-junit


    【解决方案1】:

    当我尝试使用我的应用程序时,@LorenzoCamaione 的回答是成功的。

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

    Lorenzo answer in other SO thread

    【讨论】:

      【解决方案2】:

      你可以使用 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))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-10
        • 1970-01-01
        • 2020-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多