androidandroid-testingandroid-espresso

Press delete (backspace) in EditText with espresso


I writing a test case in espresso that requires performing a delete (backspace). The code I came up is like this

onView(withId(R.id.testNumberEditText))
            .perform(replaceText("123"))
            .perform(pressKey(KeyEvent.KEYCODE_DEL))
            .check { view, _ ->
                assertEquals("12", (view as EditText).text.toString())
            }

But this code doesn't work and the text is still 123. Any idea what I'm doing wrong on what key should I press to get the desired outcome?


Solution

  • Change your code to this :

    onView(withId(R.id.etTesting))
                    .perform(replaceText("123"))
                    .perform(click())
                    .perform(pressKey(KeyEvent.KEYCODE_DEL))
                    .check(matches(withText("12")));