3

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.

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Sorry, it's not, i actually need user to TYPE something, is it possible?
The idea of a unit test is to be done with any user. Because of that I'm not sure of what your trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.