简体   繁体   中英

SearchView ui testing android

i have write code to test my SearchView behavior but failed. standard scenario just i want to insert some query into my searcview and then submit

this is my code

  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

i got error like this

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

my xml code

<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>

you can reach the search text view by using the resources -> getSystem -> getIdentifier

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

after that you can perform clearText(), then type the text that you want and perform the click on the keycode enter

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

I was reading about that and find a google example about that's

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM