Source:
private void handleCredentials(final ActivityMonitor monitor, String username, String password, boolean federated, String federatedPageUrl) throws InterruptedException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { final TextView textViewStatus = (TextView)activity.findViewById(R.id.textViewStatus); Thread.sleep(1000); Log.v(TAG, "testAcquireTokenAfterReset status text:" + textViewStatus.getText().toString()); final String startText = textViewStatus.getText().toString(); assertEquals("Token action", "Getting token...", startText); AuthenticationActivity startedActivity = (AuthenticationActivity)monitor .waitForActivityWithTimeout(5000); assertNotNull(startedActivity); Log.v(TAG, "Sleeping until it gets the login page"); sleepUntilLoginDisplays(startedActivity); Log.v(TAG, "Entering credentials to login page"); enterCredentials(startedActivity, username, password); if (federated) { Log.v(TAG, "Sleep for redirect"); sleepUntilFederatedPageDisplays(federatedPageUrl); Log.v(TAG, "Sleeping until it gets login page"); sleepUntilLoginDisplays(startedActivity); Log.v(TAG, "Entering credentials to login page"); enterCredentials(startedActivity, username, password); } waitUntil(new ResponseVerifier() { @Override public boolean hasCondition() throws IllegalArgumentException, NoSuchFieldException, IllegalAccessException { ActivityResult result = monitor.getResult(); return result != null; } }); waitUntil(new ResponseVerifier() { @Override public boolean hasCondition() throws IllegalArgumentException, NoSuchFieldException, IllegalAccessException { String tokenMsg = (String)textViewStatus.getText(); return tokenMsg != startText; } }); if(!startedActivity.isFinishing()){ Log.w(TAG, "AuthenticationActivity was not closed"); startedActivity.finish(); } }

Target:
private void handleCredentials(final ActivityMonitor monitor, String username, String password, boolean federated, String federatedPageUrl) throws InterruptedException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { final TextView textViewStatus = (TextView)activity.findViewById(R.id.textViewStatus); waitUntil(PAGE_LOAD_TIMEOUT, new ResponseVerifier() { @Override public boolean hasCondition() throws IllegalArgumentException, NoSuchFieldException, IllegalAccessException { String tokenMsg = (String)textViewStatus.getText(); return tokenMsg == MainActivity.GETTING_TOKEN; } }); final String startText = textViewStatus.getText().toString(); assertEquals("Token action", MainActivity.GETTING_TOKEN, startText); AuthenticationActivity startedActivity = (AuthenticationActivity)monitor .waitForActivityWithTimeout(ACTIVITY_WAIT_TIMEOUT); assertNotNull(startedActivity); Log.v(TAG, "Sleeping until it gets the login page"); sleepUntilLoginDisplays(startedActivity); Log.v(TAG, "Entering credentials to login page"); enterCredentials(startedActivity, username, password); if (federated) { Log.v(TAG, "Sleep for redirect"); sleepUntilFederatedPageDisplays(federatedPageUrl); Log.v(TAG, "Sleeping until it gets login page"); sleepUntilLoginDisplays(startedActivity); Log.v(TAG, "Entering credentials to login page"); enterCredentials(startedActivity, username, password); } Log.v(TAG, "Wait for the page to set the result"); waitUntil(PAGE_LOAD_TIMEOUT, new ResponseVerifier() { @Override public boolean hasCondition() throws IllegalArgumentException, NoSuchFieldException, IllegalAccessException { String tokenMsg = (String)textViewStatus.getText(); return tokenMsg != startText; } }); if (!startedActivity.isFinishing()) { Log.w(TAG, "AuthenticationActivity was not closed"); startedActivity.finish(); } }