简体   繁体   中英

Android: Keyboard input in Unit Test

I want to implement user interaction in my Unit tests, i want user to input some info that i need in order to log him in the app, here's what i've written:

@Before
public void setup() {
    mActivity = Robolectric.buildActivity(MainActivity.class).create().get();
    mKeyboard = new Scanner(System.in);
}

@Test
public void userInteraction() {
    System.out.println("Enter Username and press Enter : ");
    mUsername = mKeyboard.nextLine();
    System.out.println("Enter Password and press Enter : ");
    mPassword = mKeyboard.nextLine();
    System.out.println("Trying to login...");
}

What i'm getting: i see the message "Enter Username and press Enter :" and it looks line app is actually waiting for user to input it, but, for some reason app is not reacting to keyboard at all. I'm using AndroidStudio if that can help somehow.

在这种情况下,我建议您编写UI测试(例如使用Espresso与Espresso Test Recording: https//developer.android.com/studio/test/espresso-test-recorder.html )。

Your mUsername = mKeyboard.nextLine(); is waiting for the user to enter something. Your problem is to simulate the user entries. A similar question has been answered here , and suggest to use something like that:

String data = "Users Input";
System.setIn(new ByteArrayInputStream(data.getBytes()));

I hope this will help.

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