简体   繁体   中英

android testing - EditText returning empty even when it has characters

I'm trying to perform a functional test on an EditText, sending text to it by first gaining focus on the field using the following snippet:

instrumentation.runOnMainSync(new Runnable() {
    @Override
    public void run() {
         editText.requestFocus();
    }
}

and then sending the string via:

instrumentation.waitForIdleSync();
instrumentation.sendStringSync("Some Text");
instrumentation.waitForIdleSync();

If I run the test and watch it, the text is really being input into the EditText, but when I try to get it for use in an assertion, the EditText returns an empty text:

assertFalse(editText.getText().toString().isEmpty());

I've tried logging the text, but it really returns an empty string, as if there is no text in the EditText. I have also tried running the assertion on a separate Runnable or after a certain delay to no avail.

As for the meantime, I'm passing the test by manually adding 至此,我通过手动添加通过了测试

editText.setText("Some Text");

before the assertion, though I think there could be a better or correct way.

Another work-around I've found is by using Android Espresso. After sending string to the EditText via the instrumentation, I can assert its validity via:

 onView(withText("Some Text")).check(matches(isDisplayed()));

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