【问题标题】:SearchView ui testing androidSearchView ui 测试android
【发布时间】:2020-04-01 14:40:24
【问题描述】:

我编写了代码来测试我的 SearchView 行为但失败了。标准场景只是我想在我的 searcview 中插入一些查询然后提交

这是我的代码

  onView(withId(R.id.search_match))
        .perform(click())


    onView(
        withId(R.id.search_src_text)
    ).perform(replaceText("Arsenal"), closeSoftKeyboard(), pressKey(KeyEvent.KEYCODE_ENTER)) //failed here

我遇到了这样的错误

androidx.test.espresso.PerformException: Error performing 'replace text(Arsenal)' on view 'with id: com.kuhaku.footballmatchschedule:id/search_src_text'.

我的xml代码

<androidx.cardview.widget.CardView
    app:cardCornerRadius="12dp"
    android:layout_marginHorizontal="8dp"
    app:cardElevation="8dp"
    android:id="@+id/card_search"
    app:cardUseCompatPadding="true"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <androidx.appcompat.widget.SearchView
        android:id="@+id/search_match"
        app:queryHint="Search Match"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</androidx.cardview.widget.CardView>

【问题讨论】:

  • 应用加载时R.id.search_src_text的文字内容是什么?
  • 什么都没有,只是空白
  • 它试图用一些东西替换“Arsenal”这个词,但它找不到要替换的文本。也许盒子里可以有那个词来代替,或者可以修改测试
  • 我尝试使用 'typeText("Arsenal)' 但仍然显示相同的错误

标签: android testing kotlin android-espresso searchview


【解决方案1】:

您可以使用资源 -> getSystem -> getIdentifier 访问搜索文本视图

Resources.getSystem().getIdentifier("search_src_text",
    "id", "android")

之后你可以执行 clearText(),然后输入你想要的文本并执行点击 keycode 输入

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

【讨论】:

    【解决方案2】:

    我正在阅读有关该内容的信息,并找到了有关该内容的 google example

    onView(withId(R.id.action_search)).perform(click());  //open the searchView
        
    onView(withId(R.id.search_src_text)).perform(typeText("Replace this text"));// the text was input
                
    onView(withId(R.id.action_search)).perform(pressKey(KeyEvent.KEYCODE_ENTER));  // starting the object  search 
    

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多