Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 299
Nik Myers Avatar asked Jul 16 '26 14:07

Nik Myers


1 Answers

In this kind of situations I would reccomend you to write UI Tests (use Espresso for example with Espresso Test Recording: https://developer.android.com/studio/test/espresso-test-recorder.html).

like image 80
Leśniakiewicz Avatar answered Jul 19 '26 04:07

Leśniakiewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!