0

How can I submit some text to SearchView in androidTest?

onView(withId(R.id.search_src_text)).perform(typeText("text")) doesn't work for me - app crashes at this line

@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: No views in hierarchy found matching: with id: com.example.test:id/search_src_text

user924
  • 8,146
  • 7
  • 57
  • 139

2 Answers2

0

@LorenzoCamaione answer was success when I tried with my application.

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

Lorenzo answer in other SO thread

ponkape
  • 487
  • 8
  • 15
0

you can use Resource#getSystem to get the View

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))
Ahmed Garhy
  • 475
  • 3
  • 11